UserContoller.java 7.43 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
package com.lyms.talkonlineweb.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.annotation.TokenRequired;
import com.lyms.talkonlineweb.domain.LymsPermission;
import com.lyms.talkonlineweb.domain.LymsUser;
import com.lyms.talkonlineweb.domain.UserroleInfo;
import com.lyms.talkonlineweb.mapper.LymsUserMapper;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.result.CheckResult;
import com.lyms.talkonlineweb.service.LymsUserService;
import com.lyms.talkonlineweb.service.UserroleInfoService;
import com.lyms.talkonlineweb.util.Constant;
import com.lyms.talkonlineweb.util.HXService;
import com.lyms.talkonlineweb.util.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@Log4j2
public class UserContoller {
// public static Cache<String, Object> cache = CacheBuilder.newBuilder()
// .expireAfterAccess(20, TimeUnit.HOURS)
// .build();
@Autowired
private LymsUserService lymsUserService;

@Autowired
private LymsUserMapper lymsUserMapper;

@Autowired
private HXService hxService;

@Autowired
private UserroleInfoService userroleInfoService;//视图

@Value("${hx.APPKEY}")
private String appKey;
@Value("${hx.ClientID}")
private String clientId;
@Value("${hx.ClientSecret}")
private String clientSecret;

@RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
public BaseResponse login(String username, String passwd){
// QueryWrapper queryWrapper= Wrappers.query(LymsUser.class);
log.info(">>>>>");
BaseResponse baseResponse=new BaseResponse();
Map<String,Object> tMap=new HashMap<>();
tMap.put("token","");

List<LymsUser> uLst=lymsUserMapper.sltUser(username,DigestUtils.md5DigestAsHex(passwd.getBytes()));

if(uLst.size()>0){
LymsUser user=uLst.get(0);
String JWT = JwtUtils.createJWT("1", user.getLogin(), Constant.JWT_TTL);
tMap.put("token",JWT);

if(user.getHxid()==null && user.getHid()==null){
JSONObject json=hxService.addUser(user.getLogin(),Constant.COMMON_PASSWD);
JSONArray rArr=json.getJSONArray("entities");
if(rArr.size()>0){
user.setHxid(rArr.getJSONObject(0).getString("uuid"));
}
}
lymsUserService.saveOrUpdate(user);
user.setPasswd(null);
tMap.put("user",user);
}
baseResponse.setObject(tMap);
baseResponse.setErrorcode(0);



return baseResponse;
}

/**
* 得到用户权限
* @return
*/
@PostMapping("getPermission")
public BaseResponse getPermission(@RequestHeader String authorization){

BaseResponse baseResponse=new BaseResponse();
try {
Claims claims =JwtUtils.parseJWT(authorization);
String username=claims.getSubject();

List<LymsPermission> permiss=lymsUserService.getPermissionByUsername(username);
baseResponse.setObject(permiss);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
baseResponse.setErrorcode(1);
baseResponse.setErrormsg(e.getMessage());
}

return baseResponse;
}

/**
* 添加用户
* @param user
* @param authorization
* @return
*/
@RequestMapping("saveUser")
public BaseResponse saveUser(@Validated LymsUser user, BindingResult result, @RequestHeader String authorization){
BaseResponse baseResponse=new BaseResponse();
baseResponse.setErrormsg("");
try {
LymsUser curUser=lymsUserService.getUserByToken(authorization);
user.setCreatedby(curUser.getUid());
user.setCreatedtime(new Date());
if(user.getUid()==null){
user.setPasswd(DigestUtils.md5DigestAsHex("123456".getBytes()));
}

if(result.hasErrors()){
result.getAllErrors().forEach(e->{
baseResponse.setErrormsg(baseResponse.getErrormsg()+e.getDefaultMessage()+";");
});
baseResponse.setErrorcode(1);
return baseResponse;
}

boolean f=lymsUserService.saveOrUpdate(user);
baseResponse.setErrorcode(f==true?0:1);
// hxAddUser(user);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
baseResponse.setErrorcode(1);
baseResponse.setErrormsg(e.getMessage());
}

return baseResponse;
}

// public void hxAddUser(LymsUser user) {
// EMUser emUser=emService.user().get(user.getLogin()).block();
//
// }

/**
* 删除用户
* @param uid
* @return
*/
@PostMapping("delUser")
public BaseResponse delUser(int uid){
BaseResponse baseResponse=new BaseResponse();
boolean f=lymsUserService.removeById(uid);
baseResponse.setErrorcode(f==true?0:1);
if(!f){
baseResponse.setErrormsg("不存在改用户!");
}
return baseResponse;
}

/**
* 查询用户列表
* @param user
* @param current
* @param size
* @return
*/
@GetMapping("sltUser")
public BaseResponse sltUser(LymsUser user,int current,int size){
BaseResponse baseResponse=new BaseResponse();
Page<LymsUser> page=new Page<>(current,size);
Page<LymsUser> userIPage=lymsUserService.page(page,Wrappers.query(user).orderByDesc("updated_time","createdtime"));
baseResponse.setObject(userIPage);

return baseResponse;
}

/**
* 添加用户角色关系
* @param uid
* @param rids
* @return
*/
@PostMapping("addUserRoles")
public BaseResponse addUserRoles(int uid,String rids){
BaseResponse baseResponse=new BaseResponse();
String[] ridArr=rids.split(",");

int cnt=lymsUserService.delUserByRole(uid);//删除用户角色关系

for (int i = 0; i < ridArr.length; i++) {
cnt=lymsUserService.addUserAndRole(uid,Integer.parseInt(ridArr[i]));
}

return baseResponse;
}

/**
* 获取用户和角色列表
* @param user
* @param current
* @param size
* @return
*/
@GetMapping("getUserRoleLst")
public BaseResponse getUserRoleLst(UserroleInfo user, int current, int size){
BaseResponse baseResponse=new BaseResponse();
Page<UserroleInfo> page=new Page<>(current,size);
Page<UserroleInfo> userIPage=userroleInfoService.page(page,Wrappers.query(user).orderByDesc("updated_time","createdtime"));
baseResponse.setObject(userIPage);
return baseResponse;
}

}