From 420a305e6e877d3c37cd6abdbe05096ac5e731d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Tue, 11 Mar 2025 11:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E8=8E=B7=E5=8F=96=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E5=8A=A0=E8=A7=A3=E5=AF=86=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/ShopInfoController.java | 8 +-- .../impl/UserAuthorizationServiceImpl.java | 63 ++++++++++--------- .../service/account/util/WechatAuthUtil.java | 45 ++++++++----- 3 files changed, 66 insertions(+), 50 deletions(-) diff --git a/cash-api/account-server/src/main/java/com/czg/controller/admin/ShopInfoController.java b/cash-api/account-server/src/main/java/com/czg/controller/admin/ShopInfoController.java index d3d6f0a8c..d21140224 100644 --- a/cash-api/account-server/src/main/java/com/czg/controller/admin/ShopInfoController.java +++ b/cash-api/account-server/src/main/java/com/czg/controller/admin/ShopInfoController.java @@ -33,7 +33,7 @@ public class ShopInfoController { * 权限标识: shopInfo:list */ @SaAdminCheckRole("管理员") - @SaAdminCheckPermission("shopInfo:list") + @SaAdminCheckPermission(value = "shopInfo:list", name = "店铺列表") @GetMapping public CzgResult> get(PageDTO pageDTO, String shopName, Integer status) { return CzgResult.success(shopInfoService.get(pageDTO, shopName, status)); @@ -57,7 +57,7 @@ public class ShopInfoController { * 权限标识: shopInfo:add */ @SaAdminCheckRole("管理员") - @SaAdminCheckPermission("shopInfo:add") + @SaAdminCheckPermission(value = "shopInfo:add", name = "店铺添加") @PostMapping public CzgResult add(@RequestBody @Validated ShopInfoAddDTO shopInfoAddDTO) { return CzgResult.success(shopInfoService.add(shopInfoAddDTO)); @@ -68,7 +68,7 @@ public class ShopInfoController { * 权限标识: shopInfo:edit */ @SaAdminCheckRole("管理员") - @SaAdminCheckPermission("shopInfo:edit") + @SaAdminCheckPermission(value = "shopInfo:edit", name = "店铺编辑") @PutMapping public CzgResult edit(@RequestBody @Validated ShopInfoEditDTO shopInfoEditDTO) { return CzgResult.success(shopInfoService.edit(shopInfoEditDTO)); @@ -79,7 +79,7 @@ public class ShopInfoController { * 权限标识: shopInfo:del */ @SaAdminCheckRole("管理员") - @SaAdminCheckPermission("shopInfo:del") + @SaAdminCheckPermission(value = "shopInfo:del", name = "店铺删除") @DeleteMapping public CzgResult delete(@RequestParam Integer shopId) { return CzgResult.success(shopInfoService.remove(new QueryWrapper().eq(ShopInfo::getId, shopId))); diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/UserAuthorizationServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/UserAuthorizationServiceImpl.java index 170affa10..03a552649 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/UserAuthorizationServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/UserAuthorizationServiceImpl.java @@ -1,8 +1,10 @@ package com.czg.service.account.service.impl; //import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; +import cn.hutool.core.codec.Base64; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.symmetric.AES; import com.alibaba.fastjson2.JSONObject; import com.czg.account.dto.auth.GetPhoneDTO; import com.czg.account.dto.auth.LoginTokenDTO; @@ -51,37 +53,36 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService { @Override public String getPhone(GetPhoneDTO phoneDTO) { -// String mobile; -// String openId; -// UserInfo userInfo = null; -// if (UserAuthSourceEnum.ALIPAY.getValue().equals(phoneDTO.getSource())) { -// openId = alipayUtil.getOpenId(phoneDTO.getCode(), true); -// mobile = alipayUtil.getMobile(phoneDTO.getEncryptedData()); -// if (StrUtil.isNotBlank(openId)) { -// userInfo = userInfoService.queryChain().eq(UserInfo::getWechatOpenId, openId).one(); -// } -// } else { -// JSONObject session = wechatAuthUtil.getSession(phoneDTO.getCode()); -// String sessionKey = session.getString("session_key"); -// openId = session.getString("openid"); -// if (StrUtil.isNotBlank(openId)) { -// userInfo = userInfoService.queryChain().eq(UserInfo::getAlipayOpenId, openId).one(); -// } -// String data = WxMaCryptUtils.decrypt(sessionKey, phoneDTO.getEncryptedData(), phoneDTO.getIv()); -// JSONObject jsonObject = JSONObject.parseObject(data); -// if (jsonObject.containsKey("phoneNumber")) { -// mobile = jsonObject.getString("phoneNumber"); -// }else { -// throw new ApiNotPrintException("手机号获取失败"); -// } -// } -// -// if (userInfo != null) { -// userInfo.setPhone(mobile); -// userInfoService.updateById(userInfo); -// } -// return mobile; - return "123"; + String mobile; + String openId; + UserInfo userInfo = null; + if (UserAuthSourceEnum.ALIPAY.getValue().equals(phoneDTO.getSource())) { + openId = alipayUtil.getOpenId(phoneDTO.getCode(), true); + mobile = alipayUtil.getMobile(phoneDTO.getEncryptedData()); + if (StrUtil.isNotBlank(openId)) { + userInfo = userInfoService.queryChain().eq(UserInfo::getWechatOpenId, openId).one(); + } + } else { + JSONObject session = wechatAuthUtil.getSession(phoneDTO.getCode()); + String sessionKey = session.getString("session_key"); + openId = session.getString("openid"); + if (StrUtil.isNotBlank(openId)) { + userInfo = userInfoService.queryChain().eq(UserInfo::getAlipayOpenId, openId).one(); + } + String data = WechatAuthUtil.decrypt(sessionKey, phoneDTO.getEncryptedData(), phoneDTO.getIv()); + JSONObject jsonObject = JSONObject.parseObject(data); + if (jsonObject.containsKey("phoneNumber")) { + mobile = jsonObject.getString("phoneNumber"); + }else { + throw new ApiNotPrintException("手机号获取失败"); + } + } + + if (userInfo != null) { + userInfo.setPhone(mobile); + userInfoService.updateById(userInfo); + } + return mobile; } @Override diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java b/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java index bc31f4738..17fef0e75 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java @@ -1,6 +1,8 @@ package com.czg.service.account.util; +import cn.hutool.core.codec.Base64; import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.symmetric.AES; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; @@ -8,6 +10,7 @@ import com.czg.resp.CzgResult; import com.czg.system.dto.SysParamsDTO; import com.czg.system.service.SysParamsService; import jakarta.annotation.PostConstruct; +import jakarta.validation.constraints.NotBlank; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Component; @@ -25,31 +28,30 @@ public class WechatAuthUtil { @DubboReference(check = false) private SysParamsService sysParamsService; -// @Value("${wx.appId}") + // @Value("${wx.appId}") private String appId; -// @Value("${wx.secrete}") + // @Value("${wx.secrete}") private String secrete; -// @Value("${wx.account.appId}") + // @Value("${wx.account.appId}") private String accountAppId; -// @Value("${wx.account.secrete}") + // @Value("${wx.account.secrete}") private String accountSecrete; - - static LinkedHashMap linkedHashMap=new LinkedHashMap<>(); + static LinkedHashMap linkedHashMap = new LinkedHashMap<>(); static { - linkedHashMap.put("40001","获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口"); - linkedHashMap.put("40003","不合法的 OpenID ,请开发者确认 OpenID (该用户)是否已关注公众号,或是否是其他公众号的 OpenID"); - linkedHashMap.put("40014","不合法的 access_token ,请开发者认真比对 access_token 的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口"); - linkedHashMap.put("40037","不合法的 template_id"); - linkedHashMap.put("43101","用户未订阅消息"); - linkedHashMap.put("43107","订阅消息能力封禁"); - linkedHashMap.put("43108","并发下发消息给同一个粉丝"); - linkedHashMap.put("45168","命中敏感词"); - linkedHashMap.put("47003","参数错误"); + linkedHashMap.put("40001", "获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口"); + linkedHashMap.put("40003", "不合法的 OpenID ,请开发者确认 OpenID (该用户)是否已关注公众号,或是否是其他公众号的 OpenID"); + linkedHashMap.put("40014", "不合法的 access_token ,请开发者认真比对 access_token 的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口"); + linkedHashMap.put("40037", "不合法的 template_id"); + linkedHashMap.put("43101", "用户未订阅消息"); + linkedHashMap.put("43107", "订阅消息能力封禁"); + linkedHashMap.put("43108", "并发下发消息给同一个粉丝"); + linkedHashMap.put("45168", "命中敏感词"); + linkedHashMap.put("47003", "参数错误"); } @@ -116,4 +118,17 @@ public class WechatAuthUtil { public String getSessionKeyOrOpenId(String code, boolean isAccount) { return getSessionKey(code, "openid"); } + + public static String decrypt(String sessionKey, @NotBlank(message = "数据不能为空") String encryptedData, String iv) { + // Base64 解码 + byte[] keyBytes = Base64.decode(sessionKey); + byte[] encryptedBytes = Base64.decode(encryptedData); + byte[] ivBytes = Base64.decode(iv); + + // 使用 Hutool 进行 AES-CBC 解密 + AES aes = new AES("CBC", "PKCS5Padding", keyBytes, ivBytes); + byte[] decryptedBytes = aes.decrypt(encryptedBytes); + + return new String(decryptedBytes, java.nio.charset.StandardCharsets.UTF_8); + } }