通用注册登录方法
This commit is contained in:
parent
dd3957f45b
commit
8030ee5fe3
@ -0,0 +1,44 @@
|
||||
package cn.iocoder.yudao.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Bpm 消息的枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum SystemEnum {
|
||||
/**
|
||||
* 维修系统
|
||||
*/
|
||||
REPAIR("weixiu","维修系统"),
|
||||
/**
|
||||
* 救援系统
|
||||
*/
|
||||
RESCUE("jiuyuan","救援系统"),
|
||||
/**
|
||||
* 驾校系统
|
||||
*/
|
||||
SCHOOL("jiaxiao","驾校系统"),
|
||||
/**
|
||||
* 检测系统
|
||||
*/
|
||||
INSPECTION("jiance","检测系统"),
|
||||
/**
|
||||
* 保险系统
|
||||
*/
|
||||
INSURE("baoxian","保险系统");
|
||||
|
||||
/**
|
||||
* 系统标识
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 系统名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package cn.iocoder.yudao.module.app.wechat.service;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
|
||||
/**
|
||||
* @author vinjor-m
|
||||
@ -14,4 +15,17 @@ public interface WechatService {
|
||||
* @param map 参数
|
||||
**/
|
||||
CommonResult<?> loginByOpenId(String code);
|
||||
|
||||
/**
|
||||
* 微信授权登录-未注册自动注册,已登录的返回用户信息-
|
||||
* --目前维修系统使用,后续客户信息整合可以复用
|
||||
* @author vinjor-M
|
||||
* @date 15:14 2024/10/9
|
||||
* @param sysCode 系统标识
|
||||
* @param decryptResult 微信授权解密密文
|
||||
* @param openId 微信openId
|
||||
* @param inviteId 邀请者code
|
||||
* @return cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO
|
||||
**/
|
||||
AdminUserDO wechatLogin(String sysCode, String decryptResult, String openId, String inviteId);
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
package cn.iocoder.yudao.module.app.wechat.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.iocoder.yudao.common.SystemEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.app.wechat.service.WechatService;
|
||||
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.service.auth.AdminAuthService;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import cn.iocoder.yudao.util.WeChatLoginUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -31,6 +38,10 @@ public class WechatServiceImpl implements WechatService {
|
||||
private AdminUserApi adminUserApi;
|
||||
@Resource
|
||||
private AdminAuthService loginService;
|
||||
@Resource
|
||||
private AdminUserService userService;
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
/**
|
||||
* 微信自动登录
|
||||
*
|
||||
@ -88,4 +99,67 @@ public class WechatServiceImpl implements WechatService {
|
||||
throw new Exception(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信授权登录-未注册自动注册,已登录的返回用户信息-
|
||||
* --目前维修系统使用,后续客户信息整合可以复用
|
||||
*
|
||||
* @param sysCode 系统标识
|
||||
* @param decryptResult 微信授权解密密文
|
||||
* @param openId 微信openId
|
||||
* @param inviteId 邀请者code
|
||||
* @return cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO
|
||||
* @author vinjor-M
|
||||
* @date 15:14 2024/10/9
|
||||
**/
|
||||
@Override
|
||||
public AdminUserDO wechatLogin(String sysCode, String decryptResult, String openId, String inviteId) {
|
||||
//字符串转json
|
||||
JSONObject jsonObject = JSONUtil.parseObj(decryptResult);
|
||||
String phoneNumber = jsonObject.getStr("phoneNumber");
|
||||
//根据手机号判断数据库中是否有该用户
|
||||
AdminUserDO wxUser = userService.getUserByMobileWithoutTenant(phoneNumber);
|
||||
//如果查不到,则新增,查到了,则更新
|
||||
UserSaveReqVO user = new UserSaveReqVO();
|
||||
if (null == wxUser) {
|
||||
// 直接新注册一个账号
|
||||
user.setUsername(phoneNumber);
|
||||
user.setNickname(phoneNumber);
|
||||
user.setMobile(phoneNumber);
|
||||
user.setPassword(passwordEncoder.encode("123456"));
|
||||
//TODO 客户后期没有租户ID
|
||||
user.setTenantId(180L);
|
||||
user.setDeptId(100L);
|
||||
}else {
|
||||
//更新
|
||||
user.setId(wxUser.getId());
|
||||
}
|
||||
//设置微信openId
|
||||
if(SystemEnum.REPAIR.getCode().equals(sysCode)){
|
||||
//维修业务系统
|
||||
user.setRepairOpenId(openId);
|
||||
}else if(SystemEnum.INSPECTION.getCode().equals(sysCode)){
|
||||
//检测业务系统
|
||||
}else if(SystemEnum.SCHOOL.getCode().equals(sysCode)){
|
||||
//驾校业务系统
|
||||
}else if(SystemEnum.RESCUE.getCode().equals(sysCode)){
|
||||
//救援业务系统
|
||||
}else if(SystemEnum.INSURE.getCode().equals(sysCode)){
|
||||
//保险业务系统
|
||||
}else {
|
||||
//默认维修业务
|
||||
user.setRepairOpenId(openId);
|
||||
}
|
||||
if(null!=user.getId()){
|
||||
//更新
|
||||
userService.updateUser(user);
|
||||
}else{
|
||||
//插入
|
||||
Long uid = userService.createUser(user);
|
||||
wxUser = new AdminUserDO();
|
||||
wxUser.setId(uid);
|
||||
wxUser.setUsername(phoneNumber);
|
||||
}
|
||||
return wxUser;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,61 @@
|
||||
package cn.iocoder.yudao.util;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* AES加解密util
|
||||
* @author vinjor-M
|
||||
* @date 17:41 2024/10/10
|
||||
**/
|
||||
@Component
|
||||
public class AESUtil {
|
||||
/**
|
||||
* AES解密
|
||||
*/
|
||||
public String decrypt(String sessionKey,String encryptedIv,String encryptedData) throws Exception{
|
||||
// 转化为字节数组
|
||||
byte[] key = Base64.decode(sessionKey);
|
||||
byte[] iv = Base64.decode(encryptedIv);
|
||||
byte[] encData = Base64.decode(encryptedData);
|
||||
// 如果密钥不足16位,那么就补足
|
||||
int base =16;
|
||||
if (key.length % base !=0) {
|
||||
int groups = key.length / base +(key.length % base != 0 ? 1 : 0);
|
||||
byte[] temp = new byte[groups * base];
|
||||
Arrays.fill(temp,(byte) 0);
|
||||
System.arraycopy(key,0,temp,0,key.length);
|
||||
key = temp;
|
||||
}
|
||||
// 如果初始向量不足16位,也补足
|
||||
if (iv.length % base !=0) {
|
||||
int groups = iv.length / base +(iv.length % base != 0 ? 1 : 0);
|
||||
byte[] temp = new byte[groups * base];
|
||||
Arrays.fill(temp,(byte) 0);
|
||||
System.arraycopy(iv,0,temp,0,iv.length);
|
||||
iv = temp;
|
||||
}
|
||||
|
||||
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
String resultStr = null;
|
||||
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
|
||||
resultStr = new String(cipher.doFinal(encData),"UTF-8");
|
||||
} catch (Exception e){
|
||||
// logger.info("解析错误");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 解析加密后的字符串
|
||||
return resultStr;
|
||||
}
|
||||
}
|
@ -1,62 +1,34 @@
|
||||
package cn.iocoder.yudao.module.app.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.common.CommonErrorCodeConstants;
|
||||
import cn.iocoder.yudao.common.SystemEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
|
||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
||||
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
|
||||
import cn.iocoder.yudao.module.app.vo.WxLoginBody;
|
||||
import cn.iocoder.yudao.module.custom.entity.CustomerMain;
|
||||
import cn.iocoder.yudao.module.custom.service.CustomerMainService;
|
||||
import cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO;
|
||||
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
|
||||
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
|
||||
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
|
||||
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import cn.iocoder.yudao.module.app.wechat.service.WechatService;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.LoginBody;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.service.auth.AdminAuthService;
|
||||
import cn.iocoder.yudao.module.system.service.permission.MenuService;
|
||||
import cn.iocoder.yudao.module.system.service.permission.RoleService;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import cn.iocoder.yudao.util.AESUtil;
|
||||
import cn.iocoder.yudao.util.WechatPayConfig;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.*;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
@ -76,11 +48,20 @@ public class LoginController {
|
||||
@Resource
|
||||
private SecurityProperties securityProperties;
|
||||
@Autowired
|
||||
private CustomerMainService customerMainService;
|
||||
private WechatService wechatService;
|
||||
@Autowired
|
||||
private AESUtil aesUtil;
|
||||
|
||||
/**
|
||||
* 微信授权登录
|
||||
* @author vinjor-M
|
||||
* @date 17:43 2024/10/10
|
||||
* @param wxLoginBody 请求体
|
||||
* @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
|
||||
**/
|
||||
@PostMapping("/wxLogin")
|
||||
@TenantIgnore
|
||||
public CommonResult wxLogin(@RequestBody WxLoginBody wxLoginBody) {
|
||||
public CommonResult<?> wxLogin(@RequestBody WxLoginBody wxLoginBody) {
|
||||
String code = wxLoginBody.getCode();
|
||||
//秘钥
|
||||
String encryptedIv = wxLoginBody.getEncryptedIv();
|
||||
@ -97,29 +78,25 @@ public class LoginController {
|
||||
String decryptResult = "";
|
||||
try {
|
||||
//如果没有绑定微信开放平台,解析结果是没有unionid的。
|
||||
decryptResult = decrypt(sessionKey, encryptedIv, encryptedData);
|
||||
decryptResult = aesUtil.decrypt(sessionKey, encryptedIv, encryptedData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return error(500, "微信登录失败!");
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(decryptResult)) {
|
||||
//如果解析成功,获取token
|
||||
AuthLoginRespVO loginVO = loginService.wxLoginRepair(decryptResult,openId,wxLoginBody.getInviteId());
|
||||
//查用户(未注册的话自动注册)
|
||||
AdminUserDO adminUserDO = wechatService.wechatLogin(SystemEnum.REPAIR.getCode(),decryptResult,openId,wxLoginBody.getInviteId());
|
||||
//登录生成token
|
||||
AuthLoginRespVO loginVO = loginService.wxLoginByUserId(adminUserDO.getId(),adminUserDO.getUsername());
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("token", loginVO.getAccessToken());
|
||||
//查会员表里是否有数据
|
||||
CustomerMain customerMain = customerMainService.getCustomerByUserId(loginVO.getUserId());
|
||||
map.put("ifNeedFill", null==customerMain);
|
||||
return success(map);
|
||||
} else {
|
||||
return error(500, "微信登录失败!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@PostMapping("/logout")
|
||||
@Operation(summary = "登出系统")
|
||||
public CommonResult<Boolean> logout(HttpServletRequest request) {
|
||||
@ -130,48 +107,4 @@ public class LoginController {
|
||||
}
|
||||
return success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*/
|
||||
private String decrypt(String sessionKey,String encryptedIv,String encryptedData) throws Exception{
|
||||
// 转化为字节数组
|
||||
byte[] key = Base64.decode(sessionKey);
|
||||
byte[] iv = Base64.decode(encryptedIv);
|
||||
byte[] encData = Base64.decode(encryptedData);
|
||||
// 如果密钥不足16位,那么就补足
|
||||
int base =16;
|
||||
if (key.length % base !=0) {
|
||||
int groups = key.length / base +(key.length % base != 0 ? 1 : 0);
|
||||
byte[] temp = new byte[groups * base];
|
||||
Arrays.fill(temp,(byte) 0);
|
||||
System.arraycopy(key,0,temp,0,key.length);
|
||||
key = temp;
|
||||
}
|
||||
// 如果初始向量不足16位,也补足
|
||||
if (iv.length % base !=0) {
|
||||
int groups = iv.length / base +(iv.length % base != 0 ? 1 : 0);
|
||||
byte[] temp = new byte[groups * base];
|
||||
Arrays.fill(temp,(byte) 0);
|
||||
System.arraycopy(iv,0,temp,0,iv.length);
|
||||
iv = temp;
|
||||
}
|
||||
|
||||
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
String resultStr = null;
|
||||
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
|
||||
resultStr = new String(cipher.doFinal(encData),"UTF-8");
|
||||
} catch (Exception e){
|
||||
// logger.info("解析错误");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 解析加密后的字符串
|
||||
return resultStr;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package cn.iocoder.yudao.module.app.service;
|
||||
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
|
||||
/**
|
||||
* @author vinjor-m
|
||||
* @description 维修模块微信登录逻辑
|
||||
**/
|
||||
public interface WechatLoginService {
|
||||
/**
|
||||
* 维修系统-微信授权登录-未注册自动注册
|
||||
* @author vinjor-M
|
||||
* @date 15:14 2024/10/9
|
||||
* @param decryptResult TODO
|
||||
* @param openId TODO
|
||||
* @param inviteId TODO
|
||||
* @return cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO
|
||||
**/
|
||||
AdminUserDO wechatLogin(String decryptResult,String openId,String inviteId);
|
||||
}
|
@ -20,5 +20,5 @@ public class WxLoginBody {
|
||||
private String encryptedData;
|
||||
|
||||
//邀请码
|
||||
private Long inviteId;
|
||||
private String inviteId;
|
||||
}
|
||||
|
@ -408,7 +408,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Override
|
||||
public AuthLoginRespVO wxLoginByUserId(Long userId, String userName) {
|
||||
// 生成token
|
||||
return createTokenAfterLoginSuccess(userId, userName, LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
return createTokenAfterLoginSuccess(userId, userName, LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user