提交 41fed900 作者: zhenjie

Merge branch 'feature/optimize0' into develop

package com.mmc.iuav.user.auth;
import com.alibaba.fastjson2.JSONObject;
import com.mmc.iuav.auth.JwtConstant;
import com.mmc.iuav.auth.JwtUtil;
import com.mmc.iuav.user.dao.RoleDao;
import com.mmc.iuav.user.entity.RoleInfoDO;
import com.mmc.iuav.user.enums.UserTypeEnums;
import com.mmc.iuav.user.model.dto.LoginSuccessDTO;
import com.mmc.iuav.user.model.vo.AppUserSucVO;
import com.mmc.iuav.user.model.vo.BackUserAccountVO;
import com.mmc.iuav.user.model.vo.CompanyInfoVO;
import com.mmc.iuav.user.model.vo.UserAccountVO;
import com.mmc.iuav.user.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author: zj
* @Date: 2023/8/14 16:48
*/
@Component
public class AuthHandler {
@Autowired
private RoleDao roleDao;
@Autowired
private CompanyService companyService;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public AppUserSucVO addAppLoginCache(UserAccountVO userAccountVO) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(JwtConstant.USER_ACCOUNT_ID, userAccountVO.getId());
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.IUAV_TOKEN);
String token = JwtUtil.createJwt(map);
LoginSuccessDTO loginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(userAccountVO.getId()).uid(userAccountVO.getId() + "")
.userName(userAccountVO.getUserName()).nickName(userAccountVO.getNickName()).phoneNum(userAccountVO.getPhoneNum()).portType(UserTypeEnums.APP.getType()).build();
stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(loginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
AppUserSucVO appUserSucVO = AppUserSucVO.builder().token(token).uid(userAccountVO.getUid()).phoneNum(userAccountVO.getPhoneNum())
.nickName(userAccountVO.getNickName()).userAccountId(userAccountVO.getId()).portType(UserTypeEnums.APP.getType()).build();
return appUserSucVO;
}
public LoginSuccessDTO addPcLoginCache(BackUserAccountVO user) {
// 查询单位信息
CompanyInfoVO companyInfoVO = companyService.getCompanyInfoByBackUserAccountId(user.getId());
companyInfoVO.setLeader(1);
// 角色信息
RoleInfoDO roleInfoDO = roleDao.getRoleInfoByUserId(user.getId());
Map<String, Object> map = new HashMap<String, Object>();
map.put(JwtConstant.USER_ACCOUNT_ID, user.getId());
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.M_TOKEN);
String token = JwtUtil.createJwt(map);
LoginSuccessDTO loginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(user.getId()).accountNo(user.getAccountNo()).uid(user.getId() + "")
.userName(user.getUserName()).nickName(user.getUserName()).phoneNum(user.getPhoneNum()).portType(UserTypeEnums.PC.getType())
.companyInfoVO(companyInfoVO == null ? null : companyInfoVO)
.roleInfo(roleInfoDO.buildRoleInfoDTO()).build();
stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(loginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
loginSuccessDTO.setToken(token);
return loginSuccessDTO;
}
}
package com.mmc.iuav.user.enums;
/**
* @author: zj
* @Date: 2023/8/14 16:52
*/
public enum UserTypeEnums {
PC(0, "后台用户"), APP(100, "小程序用户");
UserTypeEnums(Integer type, String typeName) {
this.type = type;
this.typeName = typeName;
}
public Integer getType() {
return type;
}
public String getTypeName() {
return typeName;
}
private Integer type;
private String typeName;
}
...@@ -3,9 +3,9 @@ package com.mmc.iuav.user.service.impl; ...@@ -3,9 +3,9 @@ package com.mmc.iuav.user.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.mmc.iuav.auth.JwtConstant; import com.mmc.iuav.auth.JwtConstant;
import com.mmc.iuav.auth.JwtUtil;
import com.mmc.iuav.response.ResultBody; import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.response.ResultEnum; import com.mmc.iuav.response.ResultEnum;
import com.mmc.iuav.user.auth.AuthHandler;
import com.mmc.iuav.user.auth.PwdUtil; import com.mmc.iuav.user.auth.PwdUtil;
import com.mmc.iuav.user.client.PayClient; import com.mmc.iuav.user.client.PayClient;
import com.mmc.iuav.user.constant.WxConstant; import com.mmc.iuav.user.constant.WxConstant;
...@@ -26,8 +26,6 @@ import org.springframework.stereotype.Service; ...@@ -26,8 +26,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
...@@ -65,6 +63,9 @@ public class AuthServiceImpl implements AuthService { ...@@ -65,6 +63,9 @@ public class AuthServiceImpl implements AuthService {
@Autowired @Autowired
private PayClient payClient; private PayClient payClient;
@Autowired
private AuthHandler authHandler;
@Transactional @Transactional
@Override @Override
public ResultBody appletLogin(WxLoginVO wxLoginVO) { public ResultBody appletLogin(WxLoginVO wxLoginVO) {
...@@ -112,20 +113,10 @@ public class AuthServiceImpl implements AuthService { ...@@ -112,20 +113,10 @@ public class AuthServiceImpl implements AuthService {
CompanyAuthDO companyAuthDO = companyAuthDao.getCompanyAuth(userAccountVO.getId()); CompanyAuthDO companyAuthDO = companyAuthDao.getCompanyAuth(userAccountVO.getId());
companyAuthStatus = companyAuthDO == null ? 0 : 1; companyAuthStatus = companyAuthDO == null ? 0 : 1;
} }
AppUserSucVO appUserSucVO = authHandler.addAppLoginCache(userAccountVO);
Map<String, Object> map = new HashMap<String, Object>(); appUserSucVO.setAuthStatus(companyAuthStatus);
map.put(JwtConstant.USER_ACCOUNT_ID, userAccountVO.getId()); appUserSucVO.setSessionKey(sessionKey);
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.IUAV_TOKEN); return ResultBody.success(appUserSucVO);
String token = JwtUtil.createJwt(map);
LoginSuccessDTO loginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(userAccountVO.getId()).uid(userAccountVO.getId() + "")
.userName(userAccountVO.getUserName()).nickName(userAccountVO.getNickName()).phoneNum(userAccountVO.getPhoneNum()).portType(100).build();
stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(loginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
return ResultBody.success(AppUserSucVO.builder().token(token).uid(userAccountVO.getUid()).phoneNum(userAccountVO.getPhoneNum())
.nickName(userAccountVO.getNickName()).userAccountId(userAccountVO.getId()).sessionKey(sessionKey).portType(100).authStatus(companyAuthStatus).build());
} }
@Override @Override
...@@ -147,22 +138,8 @@ public class AuthServiceImpl implements AuthService { ...@@ -147,22 +138,8 @@ public class AuthServiceImpl implements AuthService {
if (!loginPwd.equals(user.getPassword())) { if (!loginPwd.equals(user.getPassword())) {
return ResultBody.error(ResultEnum.LOGIN_PASSWORD_ERROR); return ResultBody.error(ResultEnum.LOGIN_PASSWORD_ERROR);
} }
// 查询单位信息 BackUserAccountVO backUserAccountVO = user.buildBackUserAccountVO();
CompanyInfoVO companyInfoVO = companyService.getCompanyInfoByBackUserAccountId(user.getId()); LoginSuccessDTO loginSuccessDTO = authHandler.addPcLoginCache(backUserAccountVO);
companyInfoVO.setLeader(1);
// 角色信息
RoleInfoDO roleInfoDO = roleDao.getRoleInfoByUserId(user.getId());
Map<String, Object> map = new HashMap<String, Object>();
map.put(JwtConstant.USER_ACCOUNT_ID, user.getId());
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.M_TOKEN);
String token = JwtUtil.createJwt(map);
LoginSuccessDTO loginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(user.getId()).accountNo(user.getAccountNo()).uid(user.getId() + "")
.userName(user.getUserName()).nickName(user.getUserName()).phoneNum(user.getPhoneNum()).portType(0).companyInfoVO(companyInfoVO == null ? null : companyInfoVO)
.roleInfo(roleInfoDO.buildRoleInfoDTO()).build();
stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(loginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
loginSuccessDTO.setToken(token);
return ResultBody.success(loginSuccessDTO); return ResultBody.success(loginSuccessDTO);
} }
...@@ -170,20 +147,8 @@ public class AuthServiceImpl implements AuthService { ...@@ -170,20 +147,8 @@ public class AuthServiceImpl implements AuthService {
public ResultBody testAppletLogin(String unionId) { public ResultBody testAppletLogin(String unionId) {
UserAccountVO userAccountVO = userAccountService.getUserAccountInfoByUnionId(unionId); UserAccountVO userAccountVO = userAccountService.getUserAccountInfoByUnionId(unionId);
if (userAccountVO != null) { if (userAccountVO != null) {
Map<String, Object> map = new HashMap<String, Object>(); AppUserSucVO appUserSucVO = authHandler.addAppLoginCache(userAccountVO);
map.put(JwtConstant.USER_ACCOUNT_ID, userAccountVO.getId()); return ResultBody.success(appUserSucVO);
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.IUAV_TOKEN);
CompanyAuthDO companyAuthDO = companyAuthDao.getCompanyAuth(userAccountVO.getId());
Integer companyAuthStatus = companyAuthDO == null ? 0 : 1;
String token = JwtUtil.createJwt(map);
LoginSuccessDTO loginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(userAccountVO.getId()).uid(userAccountVO.getId() + "")
.userName(userAccountVO.getUserName()).nickName(userAccountVO.getNickName()).phoneNum(userAccountVO.getPhoneNum()).portType(100).build();
stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(loginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
return ResultBody.success(AppUserSucVO.builder().token(token).uid(userAccountVO.getUid()).phoneNum(userAccountVO.getPhoneNum())
.nickName(userAccountVO.getNickName()).userAccountId(userAccountVO.getId()).portType(100).authStatus(companyAuthStatus).build());
} }
return ResultBody.error(ResultEnum.APPLET_LOGIN_ERROR); return ResultBody.error(ResultEnum.APPLET_LOGIN_ERROR);
} }
...@@ -206,27 +171,18 @@ public class AuthServiceImpl implements AuthService { ...@@ -206,27 +171,18 @@ public class AuthServiceImpl implements AuthService {
if (backUserAccountVO.getDisable().equals(1)) { if (backUserAccountVO.getDisable().equals(1)) {
return ResultBody.error(ResultEnum.LOGIN_ACCOUNT_NOT_EXIT_ERROR); return ResultBody.error(ResultEnum.LOGIN_ACCOUNT_NOT_EXIT_ERROR);
} }
// 设置用户角色 LoginSuccessDTO backLoginSuccessDTO = authHandler.addPcLoginCache(backUserAccountVO);
RoleInfoDO roleInfoDO = roleDao.getRoleInfoByUserId(companyBackUserDO.getBackUserAccountId()); backLoginSuccessDTO.setAppUserAccountId(loginSuccessDTO.getUserAccountId());
// 设置用户是否是管理员
Map<String, Object> map = new HashMap<String, Object>(); backLoginSuccessDTO.getCompanyInfoVO().setLeader(companyInfoVO.getLeader());
map.put(JwtConstant.USER_ACCOUNT_ID, backUserAccountVO.getId());
map.put(JwtConstant.TOKEN_TYPE, JwtConstant.M_TOKEN);
String token = JwtUtil.createJwt(map);
LoginSuccessDTO backLoginSuccessDTO = LoginSuccessDTO.builder().token(token).userAccountId(backUserAccountVO.getId())
.accountNo(backUserAccountVO.getAccountNo()).uid(backUserAccountVO.getId() + "").userName(backUserAccountVO.getUserName())
.nickName(backUserAccountVO.getUserName()).phoneNum(backUserAccountVO.getPhoneNum()).portType(0).companyInfoVO(companyInfoVO == null ? null : companyInfoVO)
.roleInfo(roleInfoDO.buildRoleInfoDTO()).appUserAccountId(loginSuccessDTO.getUserAccountId()).token(token).build();
// 设置后台账号token,key为randomLoginCode // 设置后台账号token,key为randomLoginCode
stringRedisTemplate.opsForValue().set( stringRedisTemplate.opsForValue().set(
randomLoginCode, JSONObject.toJSONString(backLoginSuccessDTO), randomLoginCode, JSONObject.toJSONString(backLoginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS); JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
loginSuccessDTO.setToken(token); // 重新设置后台账号token
// 设置后台账号token,key为randomLoginCode
stringRedisTemplate.opsForValue().set( stringRedisTemplate.opsForValue().set(
token, JSONObject.toJSONString(backLoginSuccessDTO), backLoginSuccessDTO.getToken(), JSONObject.toJSONString(backLoginSuccessDTO),
JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS); JwtConstant.EXPIRATION, TimeUnit.MILLISECONDS);
loginSuccessDTO.setToken(token);
} else { } else {
stringRedisTemplate.opsForValue().set( stringRedisTemplate.opsForValue().set(
randomLoginCode, JSONObject.toJSONString(loginSuccessDTO), randomLoginCode, JSONObject.toJSONString(loginSuccessDTO),
...@@ -242,6 +198,8 @@ public class AuthServiceImpl implements AuthService { ...@@ -242,6 +198,8 @@ public class AuthServiceImpl implements AuthService {
return ResultBody.error(ResultEnum.APPLET_LOGIN_ERROR); return ResultBody.error(ResultEnum.APPLET_LOGIN_ERROR);
} }
LoginSuccessDTO loginSuccessDTO = JSONObject.parseObject(json, LoginSuccessDTO.class); LoginSuccessDTO loginSuccessDTO = JSONObject.parseObject(json, LoginSuccessDTO.class);
// 获取成功后删除
stringRedisTemplate.delete(randomLoginCode);
return ResultBody.success(loginSuccessDTO); return ResultBody.success(loginSuccessDTO);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论