槽点太多,哈哈
@
Override public Response login(String params) { // 入参改成 VO
try {
JSONObject jsonObject = JSON.parseObject(params); // 这块干掉
// 解密手机号码
AesNewUtil aes = new AesNewUtil();
String decMobile = aes.decrypt(jsonObject.getString("mobile")); // 改成静态方法
// 获取 openId
String openId = jsonObject.getString("openId");
if (StringUtils.isEmpty(openId)) { // 入参时校验
return Response.fail(RespCode.PARAM_NULL);
}
logger.info("openId : {}", openId);
// 查询代客用户表
ValetUser valetUser = selectUserByMobile(decMobile);
// 用户是否存在于白名单中
if (null == valetUser) { // 入参时校验,了解一下自定义注解验证
return Response.fail(RespCode.NO_GET_INFO);
} else {
String userId = valetUser.getUserId();
// 判断用户信息是否存在
ValetUserInfo valetUserInfo = valetUserInfoMapper.selectByPrimaryKey(userId);
if (null == valetUserInfo) {
// 保存用户信息
valetUserInfo = new ValetUserInfo();
valetUserInfo.setUserId(userId);
valetUserInfo.setOpenId(openId);
valetUserInfo.setCreateTime(DateUtils.getNowDate());
valetUserInfo.setUpdateTime(DateUtils.getNowDate());
valetUserInfoMapper.insertSelective(valetUserInfo);
}
// 获取 token
String token = TokenUtil.generateToken(userId, decMobile, "app");
cacheOpt.getOpt(Constants.CACHE_STR).opsForValue().set(Constants.USER_AUTH_PRIFIX + userId, token, 7, TimeUnit.DAYS);
// cacheOpt.getOpt(Constants.CACHE_STR).opsForValue().set(Constants.LOGIN_MOBILE_AUTH_PRIFIX + userId, decMobile, 7, TimeUnit.DAYS);
//
logger.info((String) cacheOpt.getOpt(Constants.CACHE_STR).opsForValue().get(Constants.USER_AUTH_PRIFIX + valetUser.getUserId()));
Map<String, Object> result = new HashMap<>(); //返回改成 dto
// 用户类型:0 无效,1 网格,2 客户经理,3 泛渠道,4 其他
String userType = String.valueOf(valetUser.getUserType()); // 这里改成枚举+find 方法
if ("0".equals(userType)) {
userType = "无效";
} else if ("1".equals(userType)) {
userType = "网格";
} else if ("2".equals(userType)) {
userType = "客户经理";
} else if ("3".equals(userType)) {
userType = "泛渠道";
} else {
userType = "其他";
}
result.put("mobile", valetUser.getMobile()); // 这里用 beanutil 或者 mapstruct 优化,一行完事
result.put("userName", valetUser.getUserName());
result.put("cusName", valetUser.getCusName());
result.put("userType", userType);
result.put("operId", valetUser.getOperId());
result.put("orgId", valetUser.getOrgId());
result.put("merchantNum", valetUser.getMerchantNum());
result.put("token", token);
// 更新用户表登录时间
valetUserMapper.updateLoginTimeByUserId(DateUtils.getNowDate(), userId);
logger.info("登录成功 {}", decMobile);
return Response.success("登录成功", result);
}
} catch (ValetappException e) { //改成全局异常处理
logger.error("登录失败 : " + e.getMessage());
return Response.fail(RespCode.FAIL);
}
}
改完差不多这样
@
Override public Response login(@Valid UserVO user) {
logger.info("openId : {}", openId);
String decMobile = AesNewUtil.decrypt(user.getMobile());
String userId = valetUser.getUserId();
ValetUser valetUser = selectUserByMobile(decMobile);
// 判断用户信息是否存在
ValetUserInfo valetUserInfo = valetUserInfoMapper.selectByPrimaryKey(userId);
if (Objects.isNull(valetUserInfo)) {
// 保存用户信息
valetUserInfo = ValetUserInfoMapper.convert(user);
valetUserInfoMapper.insertSelective(valetUserInfo);
}
String token = TokenUtil.generateToken(userId, decMobile, "app");
cacheOpt.getOpt(Constants.CACHE_STR).opsForValue().set(Constants.USER_AUTH_PRIFIX + userId, token, 7, TimeUnit.DAYS);
String userType = UserType.find(valetUser.getUserType());
TokenInfo token = tokenmapper.convert(valetUser,userType);
valetUserMapper.updateLoginTimeByUserId(DateUtils.getNowDate(), userId);
logger.info("登录成功 {}", decMobile);
return Response.success("登录成功", token);
}