调整微信工具

This commit is contained in:
TinyAnts 2023-03-20 18:22:23 +08:00
parent 07bed0ab0d
commit a8025c2f24
5 changed files with 152 additions and 29 deletions

View File

@ -0,0 +1,98 @@
package com.mdd.common.config.wechat;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.service.WxPayService;
import com.mdd.common.entity.system.SystemConfig;
import com.mdd.common.mapper.system.SystemConfigMapper;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author panweiliang
*/
@Configuration
@ConditionalOnClass(WxPayService.class)
@AllArgsConstructor
public class WxMnpConfiguration {
@Resource
private final SystemConfigMapper systemConfigMapper;
/**
* 微信小程序配置
*
* @author fzr
* @return WxMaService
*/
@Bean
@ConditionalOnMissingBean
public WxMaService wxMnpService() {
Map<String, String> config = this.getChannelConfig("mp_channel");
WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
wxConfig.setAppid(config.getOrDefault("appId", ""));
wxConfig.setSecret(config.getOrDefault("appSecret", ""));
WxMaService wxService = new WxMaServiceImpl();
wxService.setWxMaConfig(wxConfig);
return wxService;
}
/**
* 微信公众号配置
*
* @author zr
* @return WxMpService
*/
@Bean
@ConditionalOnMissingBean
public WxMpService wxOaService() {
Map<String, String> config = this.getChannelConfig("oa_channel");
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
wxMpDefaultConfig.setAppId(config.getOrDefault("appId", "").trim());
wxMpDefaultConfig.setSecret(config.getOrDefault("appSecret", "").trim());
wxMpDefaultConfig.setToken(config.getOrDefault("token", "").trim());
wxMpDefaultConfig.setAesKey(config.getOrDefault("encodingAesKey", "").trim());
WxMpService wxService = new WxMpServiceImpl();
wxService.setWxMpConfigStorage(wxMpDefaultConfig);
return wxService;
}
/**
* 配置读取
*
* @author fzr
* @param type 类型
* @return Map<String, String>
*/
private Map<String, String> getChannelConfig(String type) {
List<SystemConfig> configs = systemConfigMapper.selectList(
new QueryWrapper<SystemConfig>()
.select("id", "type", "name", "value")
.eq("type", type));
Map<String, String> map = new LinkedHashMap<>();
for (SystemConfig config : configs) {
map.put(config.getName(), config.getValue());
}
return map;
}
}

View File

@ -1,15 +1,41 @@
package com.mdd.common.util;
package com.mdd.common.plugin.wechat;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.mdd.common.util.ConfigUtils;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
public class WeChatUtils {
/**
* 微信基础驱动
*/
@Component
public class WxMnpDriver {
private static WxMaService wxMaService;
private static WxMpService wxMpService;
/**
* 微信小程序依赖注入
*/
@Resource
public void setWxMaService(WxMaService wxMaService) {
WxMnpDriver.wxMaService = wxMaService;
}
/**
* 微信公众号依赖注入
*/
@Resource
public void setWxOaService(WxMpService wxMpService) {
WxMnpDriver.wxMpService = wxMpService;
}
/**
* 微信小程序
@ -20,14 +46,13 @@ public class WeChatUtils {
public static WxMaService mnp() {
Map<String, String> config = ConfigUtils.get("mp_channel");
WxMaService service = new WxMaServiceImpl();
WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
wxConfig.setAppid(config.getOrDefault("appId", ""));
wxConfig.setSecret(config.getOrDefault("appSecret", ""));
service.setWxMaConfig(wxConfig);
return service;
}
wxMaService.setWxMaConfig(wxConfig);
return wxMaService;
}
/**
* 微信公众号
@ -35,7 +60,7 @@ public class WeChatUtils {
* @author fzr
* @return WxMpService
*/
public static WxMpService official() {
public static WxMpService oa() {
Map<String, String> config = ConfigUtils.get("oa_channel");
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
@ -43,10 +68,8 @@ public class WeChatUtils {
wxMpDefaultConfig.setSecret(config.getOrDefault("appSecret", "").trim());
wxMpDefaultConfig.setToken(config.getOrDefault("token", "").trim());
wxMpDefaultConfig.setAesKey(config.getOrDefault("encodingAesKey", "").trim());
wxMpService.setWxMpConfigStorage(wxMpDefaultConfig);
WxMpService service = new WxMpServiceImpl();
service.setWxMpConfigStorage(wxMpDefaultConfig);
return service;
return wxMpService;
}
}

View File

@ -14,6 +14,9 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
/**
* 微信支付驱动
*/
@Component
public class WxPayDriver {

View File

@ -13,6 +13,7 @@ import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.user.UserAuthMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.plugin.notice.NoticeCheck;
import com.mdd.common.plugin.wechat.WxMnpDriver;
import com.mdd.common.util.*;
import com.mdd.front.cache.ScanLoginCache;
import com.mdd.front.service.ILoginService;
@ -146,7 +147,7 @@ public class LoginServiceImpl implements ILoginService {
@Transactional
public LoginTokenVo mnpLogin(String code, Integer terminal) {
try {
WxMaService wxMaService = WeChatUtils.mnp();
WxMaService wxMaService = WxMnpDriver.mnp();
WxMaJscode2SessionResult sessionResult = wxMaService.getUserService().getSessionInfo(code);
String openId = sessionResult.getOpenid();
String uniId = sessionResult.getUnionid();
@ -169,7 +170,7 @@ public class LoginServiceImpl implements ILoginService {
@Override
public LoginTokenVo officeLogin(String code, Integer terminal) {
try {
WxMpService wxMpService = WeChatUtils.official();
WxMpService wxMpService = WxMnpDriver.oa();
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
String uniId = wxOAuth2AccessToken.getUnionId();
String openId = wxOAuth2AccessToken.getOpenId();
@ -189,7 +190,7 @@ public class LoginServiceImpl implements ILoginService {
*/
@Override
public String oaCodeUrl(String url) {
WxMpService wxMpService = WeChatUtils.official();
WxMpService wxMpService = WxMnpDriver.oa();
WxMpOAuth2ServiceImpl wxMpOAuth2Service = new WxMpOAuth2ServiceImpl(wxMpService);
return wxMpOAuth2Service.buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
}

View File

@ -15,6 +15,7 @@ import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.user.UserAuthMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.plugin.notice.NoticeCheck;
import com.mdd.common.plugin.wechat.WxMnpDriver;
import com.mdd.common.util.*;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IUserService;
@ -331,19 +332,18 @@ public class UserServiceImpl implements IUserService {
userMapper.updateById(user);
}
/**
* 绑定小程序
*
* @param bindMnpValidate
* @param userId
* @param bindMnpValidate 参数
* @param userId 用户ID
*/
@Override
public void bindMnp(UserBindWechatValidate bindMnpValidate, Integer userId) {
try {
// 通过code获取微信信息
String code = bindMnpValidate.getCode();
WxMaService wxMaService = WeChatUtils.mnp();
WxMaService wxMaService = WxMnpDriver.mnp();
WxMaJscode2SessionResult sessionResult = wxMaService.getUserService().getSessionInfo(code);
String openId = sessionResult.getOpenid();
String uniId = sessionResult.getUnionid();
@ -357,18 +357,17 @@ public class UserServiceImpl implements IUserService {
}
}
/**
* 绑定公众号
*
* @param bindOaValidate
* @param userId
* @param bindOaValidate 参数
* @param userId 用户ID
*/
@Override
public void bindOa(UserBindWechatValidate bindOaValidate, Integer userId) {
try {
// 通过code获取微信信息
WxMpService wxMpService = WeChatUtils.official();
WxMpService wxMpService = WxMnpDriver.oa();
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(bindOaValidate.getCode());
String uniId = wxOAuth2AccessToken.getUnionId();
String openId = wxOAuth2AccessToken.getOpenId();
@ -382,14 +381,13 @@ public class UserServiceImpl implements IUserService {
}
}
/**
* 绑定微信授权
*
* @param openId
* @param unionId
* @param terminal
* @param userId
* @param openId openId
* @param unionId unionId
* @param terminal 客户端端
* @param userId 用户ID
*/
public void bindWechatAuth(String openId, String unionId, Integer terminal, Integer userId) {
// 授权表中查找授权记录