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

新增微信小程序用户绑定到汇邦云的功能,包括:
1. 在WxUserApplicationService中添加带openid参数的绑定方法
2. 在Ab98UserController中添加绑定接口,通过动态码获取openid并调用绑定服务
3. 添加相关参数校验和错误处理逻辑
This commit is contained in:
dzq 2025-11-26 11:30:31 +08:00
parent 63229243fc
commit f1956d28b1
2 changed files with 72 additions and 0 deletions

View File

@ -1,6 +1,10 @@
package com.agileboot.admin.controller.ab98;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.constant.DomainConstants;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
@ -8,6 +12,7 @@ import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.ab98.user.command.BindQyUserCommand;
import com.agileboot.domain.ab98.user.command.BindWxMpUserCommand;
import com.agileboot.domain.ab98.user.dto.Ab98UserDetailDTO;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.ab98.user.Ab98UserApplicationService;
@ -16,10 +21,13 @@ import com.agileboot.domain.ab98.user.command.UpdateAb98UserCommand;
import com.agileboot.domain.ab98.user.db.Ab98UserEntity;
import com.agileboot.domain.ab98.user.dto.Ab98UserDTO;
import com.agileboot.domain.ab98.user.query.SearchAb98UserQuery;
import com.agileboot.domain.wx.user.WxUserApplicationService;
import com.agileboot.domain.wx.utils.DynamicCodeGenerator;
import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -31,6 +39,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/ab98/users")
@RequiredArgsConstructor
@ -38,6 +47,7 @@ import org.springframework.web.bind.annotation.RestController;
public class Ab98UserController extends BaseController {
private final Ab98UserApplicationService userApplicationService;
private final WxUserApplicationService wxUserApplicationService;
@Operation(summary = "用户列表")
@GetMapping
@ -88,4 +98,47 @@ public class Ab98UserController extends BaseController {
userApplicationService.bindQyUser(command);
return ResponseDTO.ok();
}
@Operation(summary = "绑定微信小程序用户到汇邦云")
@AccessLog(title = "ab98用户管理", businessType = BusinessTypeEnum.MODIFY)
@PostMapping("/bindWxMpUser")
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 {
// 从API服务实例获取动态码对应的openid
String cacheKey = DynamicCodeGenerator.buildCacheKey(command.getDynamicCode());
String url = DomainConstants.API_SERVICE_DOMAIN + "/api/caffeine/cache/dynamicCodeCache/data/" + cacheKey;
log.info("正在从API服务获取动态码缓存URL: {}", url);
String response = HttpUtil.get(url);
log.info("API服务返回动态码缓存响应: {}", response);
JSONObject jsonResponse = JSONUtil.parseObj(response);
Object valueObj = jsonResponse.get("value");
if (valueObj == null || StringUtils.isBlank(valueObj.toString())) {
log.error("动态码无效或已过期dynamicCode: {}", command.getDynamicCode());
return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, "动态码无效或已过期"));
}
String openid = valueObj.toString();
log.info("从动态码缓存中获取到openid: {}", openid);
// 调用WxUserApplicationService的bindWxMpUserWithOpenid方法执行绑定逻辑传入openid
boolean success = wxUserApplicationService.bindWxMpUserWithOpenid(command, openid);
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

@ -272,6 +272,25 @@ public class WxUserApplicationService {
return false;
}
// 调用重载方法执行绑定逻辑
return bindWxMpUserWithOpenid(command, openid);
}
/**
* 绑定微信小程序用户到汇邦云 openid 参数
*
* @param command 绑定命令包含动态码姓名身份证
* @param openid 微信 openid
* @return 绑定是否成功
*/
@Transactional(rollbackFor = Exception.class)
public boolean bindWxMpUserWithOpenid(BindWxMpUserCommand command, String openid) {
if (command == null || StringUtils.isBlank(openid)
|| StringUtils.isBlank(command.getName())
|| StringUtils.isBlank(command.getIdNum())) {
return false;
}
// 查询汇邦云用户通过身份证
Ab98UserEntity ab98UserEntity = ab98UserService.getByIdnum(command.getIdNum());