Commit 979b484d7fda952917390988869368f6d0e64e7e

Authored by YunaiV
1 parent 6f757e52

saas:支持社交应用的多租户配置(wx 小程序)

yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java
@@ -19,7 +19,6 @@ public interface ErrorCodeConstants { @@ -19,7 +19,6 @@ public interface ErrorCodeConstants {
19 ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确"); 19 ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确");
20 ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用"); 20 ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用");
21 ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_004_003_005, "未绑定账号,需要进行绑定"); 21 ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_004_003_005, "未绑定账号,需要进行绑定");
22 - ErrorCode AUTH_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_004_003_006, "获得手机号失败");  
23 ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用"); 22 ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用");
24 23
25 // ========== 用户收件地址 1-004-004-000 ========== 24 // ========== 用户收件地址 1-004-004-000 ==========
yudao-module-member/yudao-module-member-biz/pom.xml
@@ -43,10 +43,6 @@ @@ -43,10 +43,6 @@
43 <groupId>cn.iocoder.boot</groupId> 43 <groupId>cn.iocoder.boot</groupId>
44 <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId> 44 <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId>
45 </dependency> 45 </dependency>
46 - <dependency>  
47 - <groupId>cn.iocoder.boot</groupId>  
48 - <artifactId>yudao-spring-boot-starter-biz-weixin</artifactId>  
49 - </dependency>  
50 46
51 <!-- Web 相关 --> 47 <!-- Web 相关 -->
52 <dependency> 48 <dependency>
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
1 package cn.iocoder.yudao.module.member.service.auth; 1 package cn.iocoder.yudao.module.member.service.auth;
2 2
3 -import cn.binarywang.wx.miniapp.api.WxMaService;  
4 -import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;  
5 import cn.hutool.core.lang.Assert; 3 import cn.hutool.core.lang.Assert;
6 import cn.hutool.core.util.ObjectUtil; 4 import cn.hutool.core.util.ObjectUtil;
7 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 5 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
@@ -23,6 +21,7 @@ import cn.iocoder.yudao.module.system.api.social.SocialClientApi; @@ -23,6 +21,7 @@ import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
23 import cn.iocoder.yudao.module.system.api.social.SocialUserApi; 21 import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
24 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; 22 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
25 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO; 23 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
  24 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
26 import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; 25 import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
27 import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; 26 import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
28 import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants; 27 import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
@@ -61,9 +60,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { @@ -61,9 +60,6 @@ public class MemberAuthServiceImpl implements MemberAuthService {
61 @Resource 60 @Resource
62 private OAuth2TokenApi oauth2TokenApi; 61 private OAuth2TokenApi oauth2TokenApi;
63 62
64 - @Resource  
65 - private WxMaService wxMaService;  
66 -  
67 @Override 63 @Override
68 public AppAuthLoginRespVO login(AppAuthLoginReqVO reqVO) { 64 public AppAuthLoginRespVO login(AppAuthLoginReqVO reqVO) {
69 // 使用手机 + 密码,进行登录。 65 // 使用手机 + 密码,进行登录。
@@ -124,15 +120,13 @@ public class MemberAuthServiceImpl implements MemberAuthService { @@ -124,15 +120,13 @@ public class MemberAuthServiceImpl implements MemberAuthService {
124 @Override 120 @Override
125 public AppAuthLoginRespVO weixinMiniAppLogin(AppAuthWeixinMiniAppLoginReqVO reqVO) { 121 public AppAuthLoginRespVO weixinMiniAppLogin(AppAuthWeixinMiniAppLoginReqVO reqVO) {
126 // 获得对应的手机号信息 122 // 获得对应的手机号信息
127 - // TODO @芋艿:需要弱化微信小程序的依赖,通过 system 获取手机号  
128 - WxMaPhoneNumberInfo phoneNumberInfo;  
129 - try {  
130 - phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(reqVO.getPhoneCode());  
131 - } catch (Exception exception) {  
132 - throw exception(AUTH_WEIXIN_MINI_APP_PHONE_CODE_ERROR);  
133 - } 123 + SocialWxPhoneNumberInfoRespDTO phoneNumberInfo = socialClientApi.getWxMaPhoneNumberInfo(
  124 + UserTypeEnum.MEMBER.getValue(), reqVO.getPhoneCode());
  125 + Assert.notNull(phoneNumberInfo, "获得手机信息失败,结果为空");
  126 +
134 // 获得获得注册用户 127 // 获得获得注册用户
135 - MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(), getClientIP(), TerminalEnum.WECHAT_MINI_PROGRAM.getTerminal()); 128 + MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(),
  129 + getClientIP(), TerminalEnum.WECHAT_MINI_PROGRAM.getTerminal());
136 Assert.notNull(user, "获取用户失败,结果为空"); 130 Assert.notNull(user, "获取用户失败,结果为空");
137 131
138 // 绑定社交用户 132 // 绑定社交用户
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApi.java
1 package cn.iocoder.yudao.module.system.api.social; 1 package cn.iocoder.yudao.module.system.api.social;
2 2
3 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO; 3 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  4 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; 5 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
5 6
6 /** 7 /**
@@ -21,7 +22,7 @@ public interface SocialClientApi { @@ -21,7 +22,7 @@ public interface SocialClientApi {
21 String getAuthorizeUrl(Integer type, Integer userType, String redirectUri); 22 String getAuthorizeUrl(Integer type, Integer userType, String redirectUri);
22 23
23 /** 24 /**
24 - * 创建微信 JS SDK 初始化所需的签名 25 + * 创建微信公众号 JS SDK 初始化所需的签名
25 * 26 *
26 * @param userType 用户类型 27 * @param userType 用户类型
27 * @param url 访问的 URL 地址 28 * @param url 访问的 URL 地址
@@ -29,4 +30,13 @@ public interface SocialClientApi { @@ -29,4 +30,13 @@ public interface SocialClientApi {
29 */ 30 */
30 SocialWxJsapiSignatureRespDTO createWxMpJsapiSignature(Integer userType, String url); 31 SocialWxJsapiSignatureRespDTO createWxMpJsapiSignature(Integer userType, String url);
31 32
  33 + /**
  34 + * 获得微信小程序的手机信息
  35 + *
  36 + * @param userType 用户类型
  37 + * @param phoneCode 手机授权码
  38 + * @return 手机信息
  39 + */
  40 + SocialWxPhoneNumberInfoRespDTO getWxMaPhoneNumberInfo(Integer userType, String phoneCode);
  41 +
32 } 42 }
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/dto/SocialWxPhoneNumberInfoRespDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.api.social.dto;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * 微信小程序的手机信息 Response DTO
  7 + *
  8 + * @author 芋道源码
  9 + */
  10 +@Data
  11 +public class SocialWxPhoneNumberInfoRespDTO {
  12 +
  13 + /**
  14 + * 用户绑定的手机号(国外手机号会有区号)
  15 + */
  16 + private String phoneNumber;
  17 +
  18 + /**
  19 + * 没有区号的手机号
  20 + */
  21 + private String purePhoneNumber;
  22 + /**
  23 + * 区号
  24 + */
  25 + private String countryCode;
  26 +
  27 +}
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
@@ -119,6 +119,8 @@ public interface ErrorCodeConstants { @@ -119,6 +119,8 @@ public interface ErrorCodeConstants {
119 ErrorCode SOCIAL_USER_UNBIND_NOT_SELF = new ErrorCode(1_002_018_001, "社交解绑失败,非当前用户绑定"); 119 ErrorCode SOCIAL_USER_UNBIND_NOT_SELF = new ErrorCode(1_002_018_001, "社交解绑失败,非当前用户绑定");
120 ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1_002_018_002, "社交授权失败,找不到对应的用户"); 120 ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1_002_018_002, "社交授权失败,找不到对应的用户");
121 121
  122 + ErrorCode SOCIAL_APP_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_002_018_103, "获得手机号失败");
  123 +
122 // ========== 系统敏感词 1-002-019-000 ========= 124 // ========== 系统敏感词 1-002-019-000 =========
123 ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_002_019_000, "系统敏感词在所有标签中都不存在"); 125 ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_002_019_000, "系统敏感词在所有标签中都不存在");
124 ErrorCode SENSITIVE_WORD_EXISTS = new ErrorCode(1_002_019_001, "系统敏感词已在标签中存在"); 126 ErrorCode SENSITIVE_WORD_EXISTS = new ErrorCode(1_002_019_001, "系统敏感词已在标签中存在");
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java
1 package cn.iocoder.yudao.module.system.api.social; 1 package cn.iocoder.yudao.module.system.api.social;
2 2
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO; 4 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  5 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 import cn.iocoder.yudao.module.system.convert.social.SocialClientConvert; 6 import cn.iocoder.yudao.module.system.convert.social.SocialClientConvert;
5 import cn.iocoder.yudao.module.system.service.social.SocialClientService; 7 import cn.iocoder.yudao.module.system.service.social.SocialClientService;
6 import me.chanjar.weixin.common.bean.WxJsapiSignature; 8 import me.chanjar.weixin.common.bean.WxJsapiSignature;
@@ -32,4 +34,10 @@ public class SocialClientApiImpl implements SocialClientApi { @@ -32,4 +34,10 @@ public class SocialClientApiImpl implements SocialClientApi {
32 return SocialClientConvert.INSTANCE.convert(signature); 34 return SocialClientConvert.INSTANCE.convert(signature);
33 } 35 }
34 36
  37 + @Override
  38 + public SocialWxPhoneNumberInfoRespDTO getWxMaPhoneNumberInfo(Integer userType, String phoneCode) {
  39 + WxMaPhoneNumberInfo info = socialClientService.getWxMaPhoneNumberInfo(userType, phoneCode);
  40 + return SocialClientConvert.INSTANCE.convert(info);
  41 + }
  42 +
35 } 43 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/social/SocialClientConvert.java
1 package cn.iocoder.yudao.module.system.convert.social; 1 package cn.iocoder.yudao.module.system.convert.social;
2 2
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO; 4 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  5 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 import me.chanjar.weixin.common.bean.WxJsapiSignature; 6 import me.chanjar.weixin.common.bean.WxJsapiSignature;
5 import org.mapstruct.Mapper; 7 import org.mapstruct.Mapper;
6 import org.mapstruct.factory.Mappers; 8 import org.mapstruct.factory.Mappers;
@@ -12,4 +14,6 @@ public interface SocialClientConvert { @@ -12,4 +14,6 @@ public interface SocialClientConvert {
12 14
13 SocialWxJsapiSignatureRespDTO convert(WxJsapiSignature bean); 15 SocialWxJsapiSignatureRespDTO convert(WxJsapiSignature bean);
14 16
  17 + SocialWxPhoneNumberInfoRespDTO convert(WxMaPhoneNumberInfo bean);
  18 +
15 } 19 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java
1 package cn.iocoder.yudao.module.system.service.social; 1 package cn.iocoder.yudao.module.system.service.social;
2 2
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; 4 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
4 import com.xingyuv.jushauth.model.AuthUser; 5 import com.xingyuv.jushauth.model.AuthUser;
5 import me.chanjar.weixin.common.bean.WxJsapiSignature; 6 import me.chanjar.weixin.common.bean.WxJsapiSignature;
@@ -32,8 +33,10 @@ public interface SocialClientService { @@ -32,8 +33,10 @@ public interface SocialClientService {
32 */ 33 */
33 AuthUser getAuthUser(Integer socialType, Integer userType, String code, String state); 34 AuthUser getAuthUser(Integer socialType, Integer userType, String code, String state);
34 35
  36 + // =================== 微信公众号独有 ===================
  37 +
35 /** 38 /**
36 - * 创建微信 JS SDK 初始化所需的签名 39 + * 创建微信公众号的 JS SDK 初始化所需的签名
37 * 40 *
38 * @param userType 用户类型 41 * @param userType 用户类型
39 * @param url 访问的 URL 地址 42 * @param url 访问的 URL 地址
@@ -41,4 +44,15 @@ public interface SocialClientService { @@ -41,4 +44,15 @@ public interface SocialClientService {
41 */ 44 */
42 WxJsapiSignature createWxMpJsapiSignature(Integer userType, String url); 45 WxJsapiSignature createWxMpJsapiSignature(Integer userType, String url);
43 46
  47 + // =================== 微信小程序独有 ===================
  48 +
  49 + /**
  50 + * 获得微信小程序的手机信息
  51 + *
  52 + * @param userType 用户类型
  53 + * @param phoneCode 手机授权码
  54 + * @return 手机信息
  55 + */
  56 + WxMaPhoneNumberInfo getWxMaPhoneNumberInfo(Integer userType, String phoneCode);
  57 +
44 } 58 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java
1 package cn.iocoder.yudao.module.system.service.social; 1 package cn.iocoder.yudao.module.system.service.social;
2 2
  3 +import cn.binarywang.wx.miniapp.api.WxMaService;
  4 +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  5 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
  6 +import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
3 import cn.hutool.core.bean.BeanUtil; 7 import cn.hutool.core.bean.BeanUtil;
4 import cn.hutool.core.lang.Assert; 8 import cn.hutool.core.lang.Assert;
5 import cn.hutool.core.util.ReflectUtil; 9 import cn.hutool.core.util.ReflectUtil;
@@ -10,6 +14,7 @@ import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory; @@ -10,6 +14,7 @@ import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
10 import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; 14 import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
11 import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper; 15 import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper;
12 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; 16 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
  17 +import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
13 import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; 18 import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
14 import com.google.common.cache.CacheLoader; 19 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache; 20 import com.google.common.cache.LoadingCache;
@@ -22,6 +27,7 @@ import com.xingyuv.jushauth.utils.AuthStateUtils; @@ -22,6 +27,7 @@ import com.xingyuv.jushauth.utils.AuthStateUtils;
22 import lombok.SneakyThrows; 27 import lombok.SneakyThrows;
23 import lombok.extern.slf4j.Slf4j; 28 import lombok.extern.slf4j.Slf4j;
24 import me.chanjar.weixin.common.bean.WxJsapiSignature; 29 import me.chanjar.weixin.common.bean.WxJsapiSignature;
  30 +import me.chanjar.weixin.common.error.WxErrorException;
25 import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; 31 import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
26 import me.chanjar.weixin.mp.api.WxMpService; 32 import me.chanjar.weixin.mp.api.WxMpService;
27 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; 33 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
@@ -35,6 +41,7 @@ import java.util.Objects; @@ -35,6 +41,7 @@ import java.util.Objects;
35 41
36 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 42 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
37 import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; 43 import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
  44 +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_APP_WEIXIN_MINI_APP_PHONE_CODE_ERROR;
38 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_AUTH_FAILURE; 45 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_AUTH_FAILURE;
39 46
40 /** 47 /**
@@ -50,11 +57,11 @@ public class SocialClientServiceImpl implements SocialClientService { @@ -50,11 +57,11 @@ public class SocialClientServiceImpl implements SocialClientService {
50 private YudaoAuthRequestFactory yudaoAuthRequestFactory; 57 private YudaoAuthRequestFactory yudaoAuthRequestFactory;
51 58
52 @Resource 59 @Resource
53 - private WxMpService mpService; 60 + private WxMpService wxMpService;
54 @Resource 61 @Resource
55 - private StringRedisTemplate stringRedisTemplate; // WxMpService 需要使用到,所以在 Service 注入了它 62 + private WxMpProperties wxMpProperties;
56 @Resource 63 @Resource
57 - private WxMpProperties mpProperties; 64 + private StringRedisTemplate stringRedisTemplate; // WxMpService 需要使用到,所以在 Service 注入了它
58 /** 65 /**
59 * 缓存 WxMpService 对象 66 * 缓存 WxMpService 对象
60 * 67 *
@@ -63,14 +70,35 @@ public class SocialClientServiceImpl implements SocialClientService { @@ -63,14 +70,35 @@ public class SocialClientServiceImpl implements SocialClientService {
63 * 70 *
64 * 为什么要做 WxMpService 缓存?因为 WxMpService 构建成本比较大,所以尽量保证它是单例。 71 * 为什么要做 WxMpService 缓存?因为 WxMpService 构建成本比较大,所以尽量保证它是单例。
65 */ 72 */
66 - private final LoadingCache<String, WxMpService> mpServiceCache = CacheUtils.buildAsyncReloadingCache( 73 + private final LoadingCache<String, WxMpService> wxMpServiceCache = CacheUtils.buildAsyncReloadingCache(
67 Duration.ofSeconds(10L), 74 Duration.ofSeconds(10L),
68 new CacheLoader<String, WxMpService>() { 75 new CacheLoader<String, WxMpService>() {
69 76
70 @Override 77 @Override
71 public WxMpService load(String key) { 78 public WxMpService load(String key) {
72 String[] keys = key.split(":"); 79 String[] keys = key.split(":");
73 - return buildMpService(keys[0], keys[1]); 80 + return buildWxMpService(keys[0], keys[1]);
  81 + }
  82 +
  83 + });
  84 +
  85 + @Resource
  86 + private WxMaService wxMaService;
  87 + @Resource
  88 + private WxMaProperties wxMaProperties;
  89 + /**
  90 + * 缓存 WxMaService 对象
  91 + *
  92 + * 说明同 {@link #wxMpServiceCache} 变量
  93 + */
  94 + private final LoadingCache<String, WxMaService> wxMaServiceCache = CacheUtils.buildAsyncReloadingCache(
  95 + Duration.ofSeconds(10L),
  96 + new CacheLoader<String, WxMaService>() {
  97 +
  98 + @Override
  99 + public WxMaService load(String key) {
  100 + String[] keys = key.split(":");
  101 + return buildWxMaService(keys[0], keys[1]);
74 } 102 }
75 103
76 }); 104 });
@@ -129,28 +157,30 @@ public class SocialClientServiceImpl implements SocialClientService { @@ -129,28 +157,30 @@ public class SocialClientServiceImpl implements SocialClientService {
129 return request; 157 return request;
130 } 158 }
131 159
  160 + // =================== 微信公众号独有 ===================
  161 +
132 @Override 162 @Override
133 @SneakyThrows 163 @SneakyThrows
134 public WxJsapiSignature createWxMpJsapiSignature(Integer userType, String url) { 164 public WxJsapiSignature createWxMpJsapiSignature(Integer userType, String url) {
135 - WxMpService mpService = buildMpService(userType);  
136 - return mpService.createJsapiSignature(url); 165 + WxMpService service = getWxMpService(userType);
  166 + return service.createJsapiSignature(url);
137 } 167 }
138 168
139 /** 169 /**
140 - * 创建 clientId + clientSecret 对应的 WxMpService 对象 170 + * 获得 clientId + clientSecret 对应的 WxMpService 对象
141 * 171 *
142 * @param userType 用户类型 172 * @param userType 用户类型
143 * @return WxMpService 对象 173 * @return WxMpService 对象
144 */ 174 */
145 - private WxMpService buildMpService(Integer userType) { 175 + private WxMpService getWxMpService(Integer userType) {
146 // 第一步,查询 DB 的配置项,获得对应的 WxMpService 对象 176 // 第一步,查询 DB 的配置项,获得对应的 WxMpService 对象
147 SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType( 177 SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(
148 SocialTypeEnum.WECHAT_MP.getType(), userType); 178 SocialTypeEnum.WECHAT_MP.getType(), userType);
149 if (client != null && Objects.equals(client.getStatus(), CommonStatusEnum.ENABLE.getStatus())) { 179 if (client != null && Objects.equals(client.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
150 - return mpServiceCache.getUnchecked(client.getClientId() + ":" + client.getClientSecret()); 180 + return wxMpServiceCache.getUnchecked(client.getClientId() + ":" + client.getClientSecret());
151 } 181 }
152 // 第二步,不存在 DB 配置项,则使用 application-*.yaml 对应的 WxMpService 对象 182 // 第二步,不存在 DB 配置项,则使用 application-*.yaml 对应的 WxMpService 对象
153 - return mpService; 183 + return wxMpService;
154 } 184 }
155 185
156 /** 186 /**
@@ -160,11 +190,11 @@ public class SocialClientServiceImpl implements SocialClientService { @@ -160,11 +190,11 @@ public class SocialClientServiceImpl implements SocialClientService {
160 * @param clientSecret 微信公众号 secret 190 * @param clientSecret 微信公众号 secret
161 * @return WxMpService 对象 191 * @return WxMpService 对象
162 */ 192 */
163 - private WxMpService buildMpService(String clientId, String clientSecret) { 193 + private WxMpService buildWxMpService(String clientId, String clientSecret) {
164 // 第一步,创建 WxMpRedisConfigImpl 对象 194 // 第一步,创建 WxMpRedisConfigImpl 对象
165 WxMpRedisConfigImpl configStorage = new WxMpRedisConfigImpl( 195 WxMpRedisConfigImpl configStorage = new WxMpRedisConfigImpl(
166 new RedisTemplateWxRedisOps(stringRedisTemplate), 196 new RedisTemplateWxRedisOps(stringRedisTemplate),
167 - mpProperties.getConfigStorage().getKeyPrefix()); 197 + wxMpProperties.getConfigStorage().getKeyPrefix());
168 configStorage.setAppId(clientId); 198 configStorage.setAppId(clientId);
169 configStorage.setSecret(clientSecret); 199 configStorage.setSecret(clientSecret);
170 200
@@ -174,4 +204,55 @@ public class SocialClientServiceImpl implements SocialClientService { @@ -174,4 +204,55 @@ public class SocialClientServiceImpl implements SocialClientService {
174 return service; 204 return service;
175 } 205 }
176 206
  207 + // =================== 微信小程序独有 ===================
  208 +
  209 + @Override
  210 + public WxMaPhoneNumberInfo getWxMaPhoneNumberInfo(Integer userType, String phoneCode) {
  211 + WxMaService service = getWxMaService(userType);
  212 + try {
  213 + return service.getUserService().getPhoneNoInfo(phoneCode);
  214 + } catch (WxErrorException e) {
  215 + log.error("[getPhoneNoInfo][userType({}) phoneCode({}) 获得手机号失败]", userType, phoneCode, e);
  216 + throw exception(SOCIAL_APP_WEIXIN_MINI_APP_PHONE_CODE_ERROR);
  217 + }
  218 + }
  219 +
  220 + /**
  221 + * 获得 clientId + clientSecret 对应的 WxMpService 对象
  222 + *
  223 + * @param userType 用户类型
  224 + * @return WxMpService 对象
  225 + */
  226 + private WxMaService getWxMaService(Integer userType) {
  227 + // 第一步,查询 DB 的配置项,获得对应的 WxMaService 对象
  228 + SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(
  229 + SocialTypeEnum.WECHAT_MINI_APP.getType(), userType);
  230 + if (client != null && Objects.equals(client.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
  231 + return wxMaServiceCache.getUnchecked(client.getClientId() + ":" + client.getClientSecret());
  232 + }
  233 + // 第二步,不存在 DB 配置项,则使用 application-*.yaml 对应的 WxMaService 对象
  234 + return wxMaService;
  235 + }
  236 +
  237 + /**
  238 + * 创建 clientId + clientSecret 对应的 WxMaService 对象
  239 + *
  240 + * @param clientId 微信小程序 appId
  241 + * @param clientSecret 微信小程序 secret
  242 + * @return WxMaService 对象
  243 + */
  244 + private WxMaService buildWxMaService(String clientId, String clientSecret) {
  245 + // 第一步,创建 WxMaRedisBetterConfigImpl 对象
  246 + WxMaRedisBetterConfigImpl configStorage = new WxMaRedisBetterConfigImpl(
  247 + new RedisTemplateWxRedisOps(stringRedisTemplate),
  248 + wxMaProperties.getConfigStorage().getKeyPrefix());
  249 + configStorage.setAppid(clientId);
  250 + configStorage.setSecret(clientSecret);
  251 +
  252 + // 第二步,创建 WxMpService 对象
  253 + WxMaService service = new WxMaServiceImpl();
  254 + service.setWxMaConfig(configStorage);
  255 + return service;
  256 + }
  257 +
177 } 258 }