diff --git a/agileboot-admin/src/main/java/com/agileboot/admin/controller/ab98/Ab98UserController.java b/agileboot-admin/src/main/java/com/agileboot/admin/controller/ab98/Ab98UserController.java index e3de83c..1dfacc1 100644 --- a/agileboot-admin/src/main/java/com/agileboot/admin/controller/ab98/Ab98UserController.java +++ b/agileboot-admin/src/main/java/com/agileboot/admin/controller/ab98/Ab98UserController.java @@ -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 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())); + } + } } \ No newline at end of file diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java index e5e2332..211708a 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java @@ -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());