.
This commit is contained in:
108
src/main/java/net/tccn/user/User.java
Normal file
108
src/main/java/net/tccn/user/User.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package net.tccn.user;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.arango.Doc;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.ConvertType;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/22 17:37.
|
||||
*/
|
||||
@Table(name = "sys_user", catalog = "db_dev")
|
||||
public class User extends Doc<User> {
|
||||
public static User dao = dao(User.class);
|
||||
|
||||
private String username;
|
||||
private String pwd;
|
||||
private Long createTime;
|
||||
private Long loginTime;
|
||||
private Integer status;
|
||||
private String sessionid;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String sessionid) {
|
||||
this.sessionid = sessionid;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@ConvertColumn(ignore = true,type = ConvertType.JSON)
|
||||
public String getPwd() {
|
||||
return pwd;
|
||||
}
|
||||
|
||||
public void setPwd(String pwd) {
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
public Long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Long getLoginTime() {
|
||||
return loginTime;
|
||||
}
|
||||
|
||||
public void setLoginTime(Long loginTime) {
|
||||
this.loginTime = loginTime;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getSessionid() {
|
||||
return sessionid;
|
||||
}
|
||||
|
||||
public void setSessionid(String sessionid) {
|
||||
this.sessionid = sessionid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
|
||||
public static String md5IfNeed(String password){
|
||||
if (password == null || password.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return Utility.md5Hex(password);
|
||||
}
|
||||
|
||||
public JBean checkLogin(String pwd) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
|
||||
if (!this.pwd.equalsIgnoreCase(md5IfNeed(pwd))) {
|
||||
jBean.set(-1, "密码错误");
|
||||
} else if (status != 1) {
|
||||
jBean.set(-1, "登陆失败,限制登陆。");
|
||||
}
|
||||
jBean.setBody(this);
|
||||
return jBean;
|
||||
}
|
||||
|
||||
}
|
||||
56
src/main/java/net/tccn/user/UserService.java
Normal file
56
src/main/java/net/tccn/user/UserService.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package net.tccn.user;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.service.BaseService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.net.http.RestSessionid;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/22 17:16.
|
||||
*/
|
||||
@RestService(name = "user", automapping = true, comment = "用户服务")
|
||||
public class UserService extends BaseService {
|
||||
|
||||
@RestMapping(name = "login", auth = false, comment = "登陆验证")
|
||||
public JBean login(@RestSessionid String sessionid,
|
||||
String username,
|
||||
String pwd) {
|
||||
User bean = new User();
|
||||
bean.setUsername(username);
|
||||
|
||||
User user = User.dao.findFirst(bean);
|
||||
if (user == null) {
|
||||
return JBean.by(-1, "登陆失败:账号无效");
|
||||
}
|
||||
|
||||
JBean jBean = user.checkLogin(pwd);
|
||||
if (jBean.getCode() == 0) {
|
||||
cacheSource.set(30 * 60 * 2,sessionid, User.class, user);
|
||||
|
||||
user.setSessionid(sessionid);
|
||||
user.setLoginTime(System.currentTimeMillis());
|
||||
user.update();
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "current")
|
||||
public User current(@RestSessionid String sessionid) {
|
||||
return getT("user_" + sessionid, User.class, () -> User.dao.findFirst(new User(sessionid)));
|
||||
}
|
||||
|
||||
@RestMapping(name = "logout", comment = "退出登陆")
|
||||
public JBean logout(@RestSessionid String sessionid) {
|
||||
User user = User.dao.findFirst(new User(sessionid));
|
||||
if (user != null) {
|
||||
user.setSessionid("");
|
||||
user.update();
|
||||
}
|
||||
cacheSource.removeAsync("user_" + sessionid);
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user