Created
March 10, 2025 08:38
-
-
Save yyancy/c0794435558e86c525e992508c3fb807 to your computer and use it in GitHub Desktop.
refactor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
@Transactional | |
public Map<String, Object> loginAuth(CustomerLoginInfoDTO CustomerLoginInfoDTO) { | |
String token = IdUtils.fastUUID(); | |
try { | |
//解析微信授权后的用户信息 | |
WxUserInfoVO wxUserInfoVO = WxSignUtil.decrypt(CustomerLoginInfoDTO.getSessionKey(), CustomerLoginInfoDTO.getEncryptedData(), CustomerLoginInfoDTO.getIv()); | |
//检索用户是否存在 | |
QueryWrapper<Customer> queryWrapper = new QueryWrapper<>(); | |
// UPDATE: 添加部位NULL判断 | |
if (wxUserInfoVO == null){ | |
throw new ServiceException("微信授权失败"); | |
} | |
if (null != wxUserInfoVO.getPhoneNumber()) { | |
queryWrapper.eq("tel", wxUserInfoVO.getPhoneNumber()); | |
} | |
queryWrapper.eq("openid", CustomerLoginInfoDTO.getOpenid()); | |
List<Customer> Customers = CustomerMapper.selectList(queryWrapper); | |
CustomerLoginInfoDTO.setLoginTime(new Date().getTime()); | |
// 获取小程序传来的标签的描述 | |
if (CustomerLoginInfoDTO.getCustomerLabel() != null) { | |
SysDictData sysUserTag = getSysUserTag(CustomerLoginInfoDTO.getCustomerLabel()); | |
if (sysUserTag != null) { | |
// 设置描述 | |
CustomerLoginInfoDTO.setCustomerLabelDesc(sysUserTag.getDictLabel()); | |
} | |
} | |
Map<String, Object> claimsMap = new HashMap<String, Object>(); | |
LoginUser loginUser = new LoginUser(); | |
loginUser.setToken(token); | |
if (CollectionUtils.isEmpty(Customers)) { | |
createCustomer(CustomerLoginInfoDTO, wxUserInfoVO, loginUser, claimsMap, token); | |
} else { | |
updateCustomer(CustomerLoginInfoDTO, Customers, wxUserInfoVO, claimsMap, token, loginUser); | |
} | |
//刷新token | |
refreshToken(loginUser); | |
// 接口返回信息 | |
Map<String, Object> rspMap = new HashMap<String, Object>(); | |
rspMap.put("access_token", JwtUtils.createToken(claimsMap)); | |
rspMap.put("expires_in", expireTime); | |
return rspMap; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
log.error("授权登录失败: {}", e.getMessage()); | |
throw new ServiceException("授权登录失败"); | |
} | |
} | |
private static void updateCustomer(CustomerLoginInfoDTO CustomerLoginInfoDTO, List<Customer> Customers, WxUserInfoVO wxUserInfoVO, Map<String, Object> claimsMap, String token, LoginUser loginUser) { | |
UpdateWrapper<Customer> updateWrapper = new UpdateWrapper<>(); | |
if (StringUtils.isEmpty(Customers.get(0).getAvatar())) { | |
updateWrapper.set("avatar", CustomerLoginInfoDTO.getAvatar()); | |
} | |
if (null == Customers.get(0).getTel() && null != wxUserInfoVO.getPhoneNumber()) { | |
updateWrapper.set("tel", wxUserInfoVO.getPhoneNumber()); | |
} | |
updateWrapper.set("update_time", new Date()); | |
// 给客户打标签 | |
if (CustomerLoginInfoDTO.getCustomerLabel() != null) { | |
// 如果是扫描带有标签的二维码进来,接口参数CustomerLabel不为空 | |
Customer currentCustomer = Customers.get(0); | |
if (currentCustomer.getCustomerLabel() != null) { | |
// 如果当前的用户已被打过 | |
boolean contains = Arrays.asList(currentCustomer.getCustomerLabel().split(",")).contains(CustomerLoginInfoDTO.getCustomerLabel()); | |
if (!contains) { | |
// 如果客户已有的标签中不包含 传过来的标签 | |
updateWrapper.set("customer_label", currentCustomer.getCustomerLabel() + "," + CustomerLoginInfoDTO.getCustomerLabel()); | |
updateWrapper.set("customer_label_desc", currentCustomer.getCustomerLabelDesc() + "," + CustomerLoginInfoDTO.getCustomerLabelDesc()); | |
} | |
} else { | |
// 如果用户还没被打过标签 | |
updateWrapper.set("customer_label", CustomerLoginInfoDTO.getCustomerLabel()); | |
updateWrapper.set("customer_label_desc", CustomerLoginInfoDTO.getCustomerLabelDesc()); | |
} | |
} | |
updateWrapper.eq("customer_id", Customers.get(0).getCustomerId()); | |
CustomerMapper.update(updateWrapper); | |
// Jwt存储信息 | |
claimsMap.put(SecurityConstants.USER_KEY, token); | |
claimsMap.put(SecurityConstants.DETAILS_USER_ID, Customers.get(0).getCustomerId()); | |
claimsMap.put(SecurityConstants.DETAILS_USERNAME, Customers.get(0).getNickName()); | |
loginUser.setCustomerId(Customers.get(0).getCustomerId()); | |
loginUser.setNickName(Customers.get(0).getNickName()); | |
if (null == Customers.get(0).getTel()) { | |
loginUser.setTel(wxUserInfoVO.getPhoneNumber()); | |
} | |
} | |
private void createCustomer(CustomerLoginInfoDTO CustomerLoginInfoDTO, WxUserInfoVO wxUserInfoVO, LoginUser loginUser, Map<String, Object> claimsMap, String token) { | |
CustomerLoginInfoDTO.setTel(wxUserInfoVO.getPhoneNumber()); | |
//创建用户 | |
Customer Customer = insertCustomer(CustomerLoginInfoDTO); | |
loginUser.setCustomerId(Customer.getCustomerId()); | |
loginUser.setNickName(CustomerLoginInfoDTO.getNickName()); | |
loginUser.setTel(wxUserInfoVO.getPhoneNumber()); | |
// Jwt存储信息 | |
claimsMap.put(SecurityConstants.USER_KEY, token); | |
claimsMap.put(SecurityConstants.DETAILS_USER_ID, Customer.getCustomerId()); | |
claimsMap.put(SecurityConstants.DETAILS_USERNAME, Customer.getNickName()); | |
//自动获取新人优惠券 | |
pikeUpOneCoupon(Customer.getCustomerId(), CouponTypeEnum.NEW_CUSTOMER_COUPON); | |
//如果是邀请注册时建立邀请关系 | |
if (null != CustomerLoginInfoDTO.getInviteCustomerId()) { | |
registerInviteFriends(CustomerLoginInfoDTO, Customer); | |
} | |
} | |
public SysDictData getSysUserTag(String value) { | |
//根据tag查询字典 | |
return new SysDictData(); | |
} | |
private Customer insertCustomer(CustomerLoginInfoDTO customerLoginInfoDTO) { | |
Customer customer = new Customer(); | |
customer.setPassword(SecurityUtils.encryptPassword("123456")); | |
if (StringUtils.isNotEmpty(customerLoginInfoDTO.getNickName())) { | |
customer.setNickName(customerLoginInfoDTO.getNickName()); | |
} else { | |
customer.setNickName("微信用户"); | |
} | |
customer.setMemberLevel(PkbMemberLevelEnum.LEVEL_ONE.getCode()); | |
if (null != customerLoginInfoDTO.getSex()) { | |
customer.setSex(customerLoginInfoDTO.getSex().toString()); | |
} | |
customer.setInviteCustomerId(customerLoginInfoDTO.getInviteCustomerId()); | |
customer.setTel(customerLoginInfoDTO.getTel()); | |
customer.setRegisterTime(new Date()); | |
customer.setAvatar(customerLoginInfoDTO.getAvatar()); | |
customer.setCreateTime(new Date()); | |
customer.setOpenid(customerLoginInfoDTO.getOpenid()); | |
customer.setStatus(CommonStatusEnum.ENABLE.getCode()); | |
customer.setDelFlag(DeleteFlagEnum.OK.getCode()); | |
customer.setCreateBy(customerLoginInfoDTO.getNickName()); | |
// 设置用户标签 | |
if (customerLoginInfoDTO.getCustomerLabel() != null) { | |
customer.setCustomerLabel(customerLoginInfoDTO.getCustomerLabel()); | |
} | |
if (customerLoginInfoDTO.getCustomerLabelDesc() != null) { | |
customer.setCustomerLabelDesc(customerLoginInfoDTO.getCustomerLabelDesc()); | |
} | |
customerMapper.insert(customer); | |
return customer; | |
} | |
//邀请注册 | |
private void registerInviteFriends(CustomerLoginInfoDTO customerLoginInfoDTO, Customer customer) { | |
//1、检索是否存在该新用户 | |
QueryWrapper<InviteFriends> inviteFriendsQueryWrapper = new QueryWrapper<>(); | |
inviteFriendsQueryWrapper.lambda().eq(InviteFriends::getNewCustomerId, customer.getCustomerId()) | |
.eq(InviteFriends::getDelFlag, DeleteFlagEnum.OK.getCode()); | |
InviteFriends inviteFriends = inviteFriendsMapper.selectOne(inviteFriendsQueryWrapper); | |
// 此用户已被邀请过 | |
if (null != inviteFriends) { | |
return; | |
} | |
inviteFriends = new InviteFriends(); | |
inviteFriends.setCustomerId(customerLoginInfoDTO.getInviteCustomerId()); | |
inviteFriends.setInviteDate(customerLoginInfoDTO.getInviteDate()); | |
inviteFriends.setRegisterDate(new Date()); | |
inviteFriends.setCreateTime(new Date()); | |
inviteFriends.setCreateBy(customerLoginInfoDTO.getNickName()); | |
inviteFriends.setUpdateTime(new Date()); | |
inviteFriends.setStatus(InviteFriendsStatusEnum.REGISTERED.getCode()); | |
inviteFriends.setInviteDate(new Date()); | |
inviteFriends.setDelFlag(DeleteFlagEnum.OK.getCode()); | |
inviteFriends.setUpdateBy(customerLoginInfoDTO.getNickName()); | |
inviteFriends.setNewCustomerId(customer.getCustomerId()); | |
// inviteFriends.setNewCustomerCouponId(couponId); | |
inviteFriendsService.insert(inviteFriends); | |
} | |
// 自动获取新人优惠券 | |
private Long pikeUpOneCoupon(long customerId, CouponTypeEnum couponTypeEnum) { | |
//此处省略 | |
} | |
public void refreshToken(LoginUser loginUser) { | |
loginUser.setLoginTime(System.currentTimeMillis()); | |
loginUser.setExpireTime(new Date().getTime() + expireTime * MILLIS_MINUTE); | |
// 根据uuid将loginUser缓存 | |
String userKey = getTokenKey(loginUser.getToken()); | |
redisService.setCacheObject(userKey, loginUser); | |
} | |
private String getTokenKey(String token) { | |
return ACCESS_TOKEN + token; | |
} | |
@Data | |
public class WxUserInfoVO { | |
/** | |
* 用户昵称 | |
*/ | |
private String nickName; | |
/** | |
* 用户性别 | |
*/ | |
private String gender; | |
/** | |
* 语言 | |
*/ | |
private String language; | |
/** | |
* 城市 | |
*/ | |
private String city; | |
/** | |
* 省 | |
*/ | |
private String province; | |
private String country; | |
/** | |
* 头像 | |
*/ | |
private String avatarUrl; | |
/** | |
* 手机号 | |
*/ | |
private String phoneNumber; | |
/** | |
* 客户编号 | |
*/ | |
@JsonSerialize(using = LongJsonSerializer.class) | |
private Long customerId; | |
} | |
@Data | |
public class CustomerLoginInfoDTO { | |
/** | |
* 邀请人id | |
*/ | |
@JsonSerialize(using = LongJsonSerializer.class) | |
private Long inviteCustomerId; | |
/** | |
* 邀请信息id | |
*/ | |
@JsonSerialize(using = LongJsonSerializer.class) | |
private Long inviteId; | |
/** | |
* 昵称 | |
*/ | |
private String nickName; | |
/** | |
* 头像 | |
*/ | |
private String avatar; | |
/** | |
* 性别 | |
*/ | |
private Integer sex; | |
/** | |
* 手机号 | |
*/ | |
private String tel; | |
/** | |
* 登录认证 | |
*/ | |
private String token; | |
/** | |
* 用户唯一标识 | |
*/ | |
private String openid; | |
/** | |
* 邀请发起时间 | |
*/ | |
private Date inviteDate; | |
/** | |
* 登录时间 | |
*/ | |
private Long loginTime; | |
/** | |
* 过期时间 | |
*/ | |
private Long expireTime; | |
/** | |
* 包括敏感数据在内的完整用户信息的加密数据,详见 用户数据的签名验证和加解密 | |
*/ | |
private String encryptedData; | |
/** | |
* 加密算法的初始向量,详见 用户数据的签名验证和加解密 | |
*/ | |
private String iv; | |
/** | |
* 使用 sha1(rawData + sessionkey)得到字符串,用于校验用户信息,详见 用户数据的签名验证和加解密 | |
*/ | |
private String signature; | |
/** | |
* 不包括敏感信息的原始数据字符串,用于计算签名 | |
*/ | |
private String rawData; | |
/** | |
* sessionKey | |
*/ | |
private String sessionKey; | |
/** | |
* 客户标签,用于客户扫描二维码后,授权时,给客户打标签 | |
*/ | |
private String customerLabel; | |
/** | |
* 客户标签描述,用于客户扫描二维码后,授权时,给客户打标签 | |
*/ | |
private String customerLabelDesc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment