From 52db6e21379b3e223e94fffec48aead151ba7d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Mon, 29 Jul 2024 17:36:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=BE=AE=E4=BF=A1=E6=94=B6?= =?UTF-8?q?=E6=AC=BE=E8=B4=A6=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/common/wx/WxService.java | 32 +++++++++++++++++++ .../web/service/SysLoginService.java | 19 +++-------- .../service/impl/AccountServiceImpl.java | 2 ++ .../controller/app/AppAccountController.java | 23 +++++++++++++ 4 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/wx/WxService.java diff --git a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/wx/WxService.java b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/wx/WxService.java new file mode 100644 index 00000000..7da5e1fa --- /dev/null +++ b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/wx/WxService.java @@ -0,0 +1,32 @@ +package com.ruoyi.common.wx; + +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import com.alibaba.fastjson2.JSON; +import com.ruoyi.common.config.WxConfig; +import com.ruoyi.common.utils.http.HttpUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author wjh + * 2024/7/29 + */ +@Service +public class WxService { + + + @Autowired + private WxConfig wxConfig; + + /** + * 使用登录码获取微信用户信息 + * @param jsCode 登录码,通过wx.login获得 + */ + public WxMaJscode2SessionResult wxJsCode2Session(String jsCode) { + String grantType = "authorization_code"; + String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=%s", wxConfig.getAppId(), wxConfig.getAppSecret(), jsCode, grantType); + String body = HttpUtils.sendGet(url); + return JSON.parseObject(body, WxMaJscode2SessionResult.class); + } + +} diff --git a/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index eccbf85a..7ec5db48 100644 --- a/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -13,6 +13,7 @@ import com.ruoyi.common.enums.UserStatus; import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.wx.AccessTokenUtil; +import com.ruoyi.common.wx.WxService; import com.ruoyi.ss.account.domain.Account; import com.ruoyi.ss.store.service.IStoreService; import com.ruoyi.ss.user.service.ISmUserService; @@ -75,15 +76,15 @@ public class SysLoginService @Autowired private ISmUserService smUserService; - @Autowired - private WxConfig wxConfig; - @Autowired private AccountService accountService; @Autowired private IStoreService smDeviceGroupService; + @Autowired + private WxService wxService; + /** * 登录验证 * @@ -257,7 +258,7 @@ public class SysLoginService public String wxLogin(WxLoginBody body) { // 通过登录授权码获取到用户信息 - WxMaJscode2SessionResult wxMaJscode2SessionResult = this.wxJsCode2Session(body.getLoginCode()); + WxMaJscode2SessionResult wxMaJscode2SessionResult = wxService.wxJsCode2Session(body.getLoginCode()); // 通过openId查询用户,判断是否微信用户 SmUser user = smUserService.selectSmUserByWxOpenId(wxMaJscode2SessionResult.getOpenid()); @@ -327,16 +328,6 @@ public class SysLoginService return newUser; } - /** - * 使用登录码获取微信用户信息 - * @param jsCode 登录码,通过wx.login获得 - */ - private WxMaJscode2SessionResult wxJsCode2Session(String jsCode) { - String grantType = "authorization_code"; - String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=%s", wxConfig.getAppId(), wxConfig.getAppSecret(), jsCode, grantType); - String body = HttpUtils.sendGet(url); - return JSON.parseObject(body, WxMaJscode2SessionResult.class); - } /** * 通过授权码获取微信用户手机号 diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/account/service/impl/AccountServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/account/service/impl/AccountServiceImpl.java index cd717b45..ff5738cc 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/account/service/impl/AccountServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/account/service/impl/AccountServiceImpl.java @@ -1,9 +1,11 @@ package com.ruoyi.ss.account.service.impl; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.common.valid.bank.BankCardInfo; import com.ruoyi.common.valid.bank.BankValidUtils; +import com.ruoyi.common.wx.WxService; import com.ruoyi.ss.account.domain.Account; import com.ruoyi.ss.account.domain.AccountQuery; import com.ruoyi.ss.account.domain.AccountVO; diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppAccountController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppAccountController.java index f83eefa5..6660cc44 100644 --- a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppAccountController.java +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppAccountController.java @@ -1,11 +1,16 @@ package com.ruoyi.web.controller.app; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.ValidGroup; +import com.ruoyi.common.utils.ServiceUtil; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.wx.WxService; import com.ruoyi.ss.account.domain.Account; import com.ruoyi.ss.account.domain.AccountQuery; import com.ruoyi.ss.account.domain.AccountVO; +import com.ruoyi.ss.account.domain.enums.AccountType; import com.ruoyi.ss.account.service.AccountService; import com.ruoyi.ss.account.service.AccountValidator; import com.ruoyi.web.core.annotation.MchRequired; @@ -29,9 +34,19 @@ public class AppAccountController extends BaseController { @Autowired private AccountValidator accountValidator; + @Autowired + private WxService wxService; + @ApiOperation("添加收款账户") @PostMapping public AjaxResult addAccount(@RequestBody @Validated({ValidGroup.FrontCreate.class}) Account data) { + // 微信,获取微信openId并替换数据 + if (AccountType.WECHAT.getType().equals(data.getAccountType())) { + WxMaJscode2SessionResult result = wxService.wxJsCode2Session(data.getAccountNo()); + ServiceUtil.assertion(result == null, "获取微信信息失败"); + data.setAccountNo(result.getOpenid()); + } + data.setUserId(getUserId()); return success(accountService.insertSmAccount(data)); } @@ -39,6 +54,14 @@ public class AppAccountController extends BaseController { @ApiOperation("修改收款账户") @PutMapping public AjaxResult updateAccount(@RequestBody @Validated({ValidGroup.FrontUpdate.class}) Account data) { + + // 微信,获取微信openId并替换数据 + if (AccountType.WECHAT.getType().equals(data.getAccountType()) && StringUtils.hasText(data.getAccountNo())) { + WxMaJscode2SessionResult result = wxService.wxJsCode2Session(data.getAccountNo()); + ServiceUtil.assertion(result == null, "获取微信信息失败"); + data.setAccountNo(result.getOpenid()); + } + data.setUserId(getUserId()); return success(accountService.updateSmAccount(data)); }