package com.medic.rest.province.base.home;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import com.medic.rest.province.BaseRestObject;
import com.medic.rest.province.RedisTool;
import com.medic.rest.province.base.management.UserRoleModel;
import com.zdzsoft.rest.RestContext;
import com.zdzsoft.rest.annotation.RestMethod;
import com.zdzsoft.rest.annotation.RestObject;
import com.zdzsoft.rest.annotation.RestParam;
import com.zdzsoft.rest.annotation.RestReturn;
import com.zdzsoft.rest.model.QueryDao;
@RestObject(name = "UserSecurity", alia = "", desc = "", path = "/province/base/home/User")
public class UserSecurity extends BaseRestObject {
private static Random rand = new Random(System.currentTimeMillis());
private static int token_seed = 0;
@RestMethod(name = "", alia = "登录", desc = "")
@RestReturn(name = "user", alia = "", type = "[<UserRole>]")
public Object login(RestContext context) {
QueryDao dao = getQueryDao();
dao.addWhereCondition(context, UserRoleModel.class);
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("token", "");
result.put("user", "");
result.put("role", "");
List<?> list = dao.query(UserRoleModel.class, 0, 1);
if (list == null || list.isEmpty()) {
return result;
}
UserRoleModel user = (UserRoleModel) list.get(0);
String token = getNextToken();
String value = user.getUser_name() + ";" + user.getRole();
RedisTool.get().set(token, value, 15 * 60);
result.put("token", token);
result.put("user", user.getUser_name());
result.put("role", user.getRole());
return result;
}
@RestMethod(name = "", alia = "测试", desc = "")
@RestReturn(name = "user", alia = "", type = "[<UserRole>]")
public Object test(RestContext context,
@RestParam(name = "token", validate = "notnull") String token) {
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("token", "");
result.put("user", "");
result.put("role", "");
if (token != null && token.length() > 0) {
String value = RedisTool.get().get(token);
if (value != null && value.length() > 0) {
String[] str = value.split(";");
if (str.length == 2) {
String name = str[0];
String role = str[1];
result.put("token", token);
result.put("user", name);
result.put("role", role);
RedisTool.get().expire(token, 15*60);
}
}
}
return result;
}
@RestMethod(name = "", alia = "退出", desc = "")
@RestReturn(name = "flag", alia = "执行结果", type = "")
public boolean logout(RestContext context,
@RestParam(name = "token", validate = "notnull") String token) {
if (token != null && token.length() > 0) {
RedisTool.get().delete(token);
}
return true;
}
public static boolean check(String token) {
if (token != null && token.length() > 0) {
String value = RedisTool.get().get(token);
if (value != null && value.length() > 0) {
return true;
}
}
return false;
}
public String getNextToken() {
String token = toString(rand.nextInt(9999));
synchronized (this) {
token += toString(token_seed);
token_seed = (token_seed + 1) % 9999;
}
return token;
}
private String toString(int value) {
String str = "" + value;
while (str.length() < 4) {
str = '0' + str;
}
return str;
}
}