feat(微信登录): 添加微信小程序用户绑定功能

实现微信小程序用户通过动态码、姓名和身份证绑定到汇邦云的功能。包括:
1. 在WxLoginController中添加绑定接口
2. 在WxUserApplicationService中实现绑定逻辑
3. 处理动态码验证、用户信息匹配和数据库更新
This commit is contained in:
dzq 2025-11-06 17:13:49 +08:00
parent 23153a8672
commit 8a02234dcc
3 changed files with 141 additions and 3 deletions

View File

@ -33,7 +33,9 @@
"Bash(cat \"/e/code/智柜宝/shop-back-end/agileboot-domain/src/main/java/com/agileboot/domain/system/user/model/UserModelFactory.java\")",
"Bash(cat \"/e/code/智柜宝/shop-back-end/agileboot-domain/src/main/java/com/agileboot/domain/shop/order/OrderApplicationService.java\")",
"Bash(cat \"/e/code/智柜宝/shop-back-end/agileboot-domain/src/main/java/com/agileboot/domain/system/user/dto/UserDTO.java\")",
"Bash(cat \"/e/code/智柜宝/shop-back-end/agileboot-domain/src/main/java/com/agileboot/domain/system/user/query/SearchUserQuery.java\")"
"Bash(cat \"/e/code/智柜宝/shop-back-end/agileboot-domain/src/main/java/com/agileboot/domain/system/user/query/SearchUserQuery.java\")",
"Bash(./mvnw clean compile -pl agileboot-api -am -DskipTests)",
"Bash(./mvnw clean compile -pl agileboot-api,agileboot-domain -am -DskipTests)"
],
"deny": [],
"ask": []

View File

@ -1,23 +1,25 @@
package com.agileboot.api.controller;
import cn.hutool.json.JSONUtil;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.ab98.api.Ab98ApiUtil;
import com.agileboot.domain.ab98.api.SsoLoginUserinfo;
import com.agileboot.domain.ab98.user.Ab98UserApplicationService;
import com.agileboot.domain.ab98.user.command.BindQyUserCommand;
import com.agileboot.domain.ab98.user.command.BindWxMpUserCommand;
import com.agileboot.domain.ab98.user.db.Ab98UserEntity;
import com.agileboot.domain.qywx.user.QyUserApplicationService;
import com.agileboot.domain.wx.user.WxUserApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.agileboot.domain.ab98.api.Ab98ApiUtil;
import javax.validation.constraints.NotBlank;
import java.util.Map;
@Slf4j
@RestController
@ -29,6 +31,7 @@ public class WxLoginController {
private final QyUserApplicationService qyUserApplicationService;
private final Ab98UserApplicationService ab98UserApplicationService;
private final Ab98UserApplicationService userApplicationService;
private final WxUserApplicationService wxUserApplicationService;
@PostMapping("/logout")
@ApiOperation(value = "用户退出登录")
@ -166,4 +169,25 @@ public class WxLoginController {
Ab98UserEntity ab98User = userApplicationService.bindQyUser(command);
return ResponseDTO.ok(ab98User);
}
@PostMapping("/bindWxMpUser")
@ApiOperation(value = "绑定微信小程序用户到汇邦云", notes = "通过动态码、姓名、身份证绑定微信小程序用户到汇邦云")
public ResponseDTO<String> bindWxMpUser(@RequestBody BindWxMpUserCommand command) {
if (command == null || StringUtils.isBlank(command.getDynamicCode()) || StringUtils.isBlank(command.getName()) || StringUtils.isBlank(command.getIdNum())) {
log.error("绑定微信小程序用户到汇邦云参数错误: {}", JSONUtil.toJsonStr(command));
return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, "参数错误"));
}
try {
boolean success = wxUserApplicationService.bindWxMpUser(command);
if (success) {
return ResponseDTO.ok("绑定成功");
} else {
return ResponseDTO.fail(new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "绑定失败"));
}
} catch (Exception e) {
log.error("绑定微信小程序用户到汇邦云失败: ", e);
return ResponseDTO.fail(new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "绑定失败: " + e.getMessage()));
}
}
}

View File

@ -1,7 +1,17 @@
package com.agileboot.domain.wx.user;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.ab98.api.Ab98ApiUtil;
import com.agileboot.domain.ab98.api.Ab98UserDto;
import com.agileboot.domain.ab98.user.Ab98UserApplicationService;
import com.agileboot.domain.ab98.user.command.AddAb98UserCommand;
import com.agileboot.domain.ab98.user.command.BindWxMpUserCommand;
import com.agileboot.domain.ab98.user.command.UpdateAb98UserCommand;
import com.agileboot.domain.ab98.user.db.Ab98UserEntity;
import com.agileboot.domain.ab98.user.db.Ab98UserService;
import com.agileboot.domain.common.cache.CacheCenter;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.wx.utils.DynamicCodeGenerator;
import com.agileboot.domain.wx.utils.WxNicknameGenerator;
import com.agileboot.domain.wx.user.command.AddWxUserCommand;
import com.agileboot.domain.wx.user.command.UpdateWxUserCommand;
@ -16,6 +26,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -33,6 +44,10 @@ public class WxUserApplicationService {
private final WxUserModelFactory modelFactory;
private final Ab98UserService ab98UserService;
private final Ab98UserApplicationService ab98UserApplicationService;
/**
* 获取微信用户列表分页
*/
@ -202,4 +217,101 @@ public class WxUserApplicationService {
return userDTO;
}
/**
* 绑定微信小程序用户到汇邦云
*
* @param command 绑定命令包含动态码姓名身份证
* @return 绑定是否成功
*/
@Transactional(rollbackFor = Exception.class)
public boolean bindWxMpUser(BindWxMpUserCommand command) {
if (command == null || StringUtils.isBlank(command.getDynamicCode())
|| StringUtils.isBlank(command.getName())
|| StringUtils.isBlank(command.getIdNum())) {
return false;
}
// 从动态码缓存中获取openid
String cacheKey = DynamicCodeGenerator.buildCacheKey(command.getDynamicCode());
String openid = CacheCenter.dynamicCodeCache.get(cacheKey);
if (StringUtils.isBlank(openid)) {
return false;
}
// 查询汇邦云用户通过身份证
Ab98UserEntity ab98UserEntity = ab98UserService.getByIdnum(command.getIdNum());
// 如果汇邦云用户存在且姓名匹配则直接绑定
if (ab98UserEntity != null && StringUtils.equals(ab98UserEntity.getName(), command.getName())) {
// 更新微信用户表的ab98UserId
WxUserEntity wxUserEntity = userService.getByOpenid(openid);
if (wxUserEntity != null) {
wxUserEntity.setAb98UserId(ab98UserEntity.getAb98UserId());
userService.updateById(wxUserEntity);
return true;
}
return false;
}
// 汇邦云用户不存在或姓名不匹配调用Ab98ApiUtil获取用户信息
Ab98UserDto ab98UserDto = Ab98ApiUtil.pullUserInfoByIdnum("wxshop", "34164e41f0c6694be6bbbba0dc50c14a", command.getIdNum());
if (ab98UserDto == null) {
return false;
}
// 验证姓名是否匹配
if (!StringUtils.equals(ab98UserDto.getRealName(), command.getName())) {
return false;
}
// 构建AddAb98UserCommand
AddAb98UserCommand addCommand = new AddAb98UserCommand();
addCommand.setUserid(ab98UserDto.getSsoUid());
addCommand.setName(ab98UserDto.getRealName());
addCommand.setTel(ab98UserDto.getPhone());
addCommand.setSex(ab98UserDto.getSex());
addCommand.setIdnum(ab98UserDto.getIdCardNo());
addCommand.setIdcardFront(ab98UserDto.getIdCardFront());
addCommand.setIdcardBack(ab98UserDto.getIdCardBack());
addCommand.setFaceImg(ab98UserDto.getFacePicture());
addCommand.setAddress(ab98UserDto.getAddress());
addCommand.setRegistered(true);
addCommand.initBaseEntity();
// 检查汇邦云用户是否已存在如果存在则更新否则创建
Ab98UserEntity existingUser = ab98UserService.getByIdnum(command.getIdNum());
if (existingUser != null) {
// 更新现有用户
UpdateAb98UserCommand updateCommand = new UpdateAb98UserCommand();
updateCommand.setAb98UserId(existingUser.getAb98UserId());
updateCommand.setUserid(ab98UserDto.getSsoUid());
updateCommand.setName(ab98UserDto.getRealName());
updateCommand.setTel(ab98UserDto.getPhone());
updateCommand.setSex(ab98UserDto.getSex());
updateCommand.setIdnum(ab98UserDto.getIdCardNo());
updateCommand.setIdcardFront(ab98UserDto.getIdCardFront());
updateCommand.setIdcardBack(ab98UserDto.getIdCardBack());
updateCommand.setFaceImg(ab98UserDto.getFacePicture());
updateCommand.setAddress(ab98UserDto.getAddress());
updateCommand.setRegistered(true);
ab98UserApplicationService.updateUser(updateCommand);
ab98UserEntity = existingUser;
} else {
// 创建新用户
ab98UserApplicationService.addUser(addCommand);
ab98UserEntity = ab98UserService.getByIdnum(command.getIdNum());
}
// 更新微信用户表的ab98UserId
WxUserEntity wxUserEntity = userService.getByOpenid(openid);
if (wxUserEntity != null) {
wxUserEntity.setAb98UserId(ab98UserEntity.getAb98UserId());
userService.updateById(wxUserEntity);
return true;
}
return false;
}
}