UserContoller.java 11 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
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
package com.lyms.talkonlineweb.controller;

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.*;
import com.lyms.talkonlineweb.mapper.LymsUserMapper;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.LymsUserService;
import com.lyms.talkonlineweb.service.LymsUserroleService;
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.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@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;//视图

@Autowired
private LymsUserroleService lymsUserroleService;//用户角色关系表

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

@Value("${hx.hxpasswd}")
private String hxpasswd;

@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();
baseResponse.setErrorcode(1);
baseResponse.setErrormsg("用户名或密码错误");
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,"sys_"+user.getUname());
// JSONArray rArr=json.getJSONArray("entities");
// if(rArr.size()>0){
// user.setHxid(rArr.getJSONObject(0).getString("uuid"));
// }
// }
// lymsUserService.saveOrUpdate(user);
if (JWT.length()>0){
baseResponse.setErrorcode(0);
baseResponse.setErrormsg("");
}
user.setPasswd(null);

Map<String,Object> hxMap=new HashMap<>();
hxMap.put("hxuser",hxuser);
hxMap.put("hxpasswd",hxpasswd);

tMap.put("hx",hxMap);
tMap.put("user",user);
}
baseResponse.setObject(tMap);




return baseResponse;
}

/**
* 得到用户权限
* @return
*/
@PostMapping("getPermission")
@TokenRequired
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")
@TokenRequired
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()));
}else{
if(user.getPasswd()!=null){
user.setPasswd(DigestUtils.md5DigestAsHex(user.getPasswd().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")
@TokenRequired
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;
// }
@TokenRequired
public BaseResponse sltUser(UserroleInfo user, int current, int size){
BaseResponse baseResponse=new BaseResponse();
if (StringUtils.isEmpty(user.getUname())){
user.setUname(null);
}
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;
}

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

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

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

return baseResponse;
}

/**
* 获取用户和角色列表
* @param user
* @param current
* @param size
* @return
*/
@GetMapping("getUserRoleLst")
@TokenRequired
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;
}

/**
* 添加用户包含的角色
* @param uid
* @param roles 如果是多个权限需要用","分割
* @return
*/
@PostMapping("addUserByRoles")
@TokenRequired
public BaseResponse addRoleByPerms(int uid,String roles){
BaseResponse baseResponse=new BaseResponse();
String[] pArr=roles.split(",");
int cnt=0;

cnt=lymsUserroleService.delRoleByUser(uid);//删除旧的关系
for (int i = 0; i < pArr.length; i++) {
cnt=lymsUserroleService.addRoleUser(uid,Integer.parseInt(pArr[i]));//添加用户和角色映射
}
return baseResponse;
}


/**
* 添加用户
* @param authorization
* @return
*/
@RequestMapping("getUserByToken")
@TokenRequired
public BaseResponse getUserByToken(@RequestHeader String authorization){
BaseResponse baseResponse=new BaseResponse();
baseResponse.setErrormsg("");
Map<String,Object> rs=new HashMap<>();
try {
LymsUser curUser=lymsUserService.getUserByToken(authorization);
Map<String,Object> hxMap=new HashMap<>();
hxMap.put("hxuser",hxuser);
hxMap.put("hxpasswd",hxpasswd);
rs.put("hx",hxMap);
rs.put("curUser",curUser);

baseResponse.setObject(rs);
baseResponse.setErrorcode(0);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
baseResponse.setErrorcode(1);
baseResponse.setErrormsg(e.getMessage());
}

return baseResponse;
}

/**
* PC-查询登陆账号是否存在
* @param login 账号
* @return true 可以使用,false已经存在
*/
@GetMapping("getLoginUserByLogin")
@TokenRequired
public BaseResponse getLoginUserByLogin(String login){
BaseResponse baseResponse=new BaseResponse();
try {
LymsUser user=lymsUserService.getOne(new QueryWrapper<LymsUser>().eq("login", login));
baseResponse.setObject(null==user?true:false);
baseResponse.setErrormsg("成功");
} catch (Exception e) {
baseResponse.setErrormsg("失败");
e.printStackTrace();
}
return baseResponse;
}

public static void main(String[] args) {
System.out.println(DigestUtils.md5DigestAsHex("passwd".getBytes()));;
}

}