绑定微信收款账户

This commit is contained in:
墨大叔 2024-07-29 17:36:28 +08:00
parent acdd27e5e4
commit 52db6e2137
4 changed files with 62 additions and 14 deletions

View File

@ -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);
}
}

View File

@ -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);
}
/**
* 通过授权码获取微信用户手机号

View File

@ -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;

View File

@ -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));
}