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 19 ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确");
20 20 ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用");
21 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 22 ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用");
24 23  
25 24 // ========== 用户收件地址 1-004-004-000 ==========
... ...
yudao-module-member/yudao-module-member-biz/pom.xml
... ... @@ -43,10 +43,6 @@
43 43 <groupId>cn.iocoder.boot</groupId>
44 44 <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId>
45 45 </dependency>
46   - <dependency>
47   - <groupId>cn.iocoder.boot</groupId>
48   - <artifactId>yudao-spring-boot-starter-biz-weixin</artifactId>
49   - </dependency>
50 46  
51 47 <!-- Web 相关 -->
52 48 <dependency>
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
1 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 3 import cn.hutool.core.lang.Assert;
6 4 import cn.hutool.core.util.ObjectUtil;
7 5 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
... ... @@ -23,6 +21,7 @@ import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
23 21 import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
24 22 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
25 23 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
  24 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
26 25 import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
27 26 import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
28 27 import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
... ... @@ -61,9 +60,6 @@ public class MemberAuthServiceImpl implements MemberAuthService {
61 60 @Resource
62 61 private OAuth2TokenApi oauth2TokenApi;
63 62  
64   - @Resource
65   - private WxMaService wxMaService;
66   -
67 63 @Override
68 64 public AppAuthLoginRespVO login(AppAuthLoginReqVO reqVO) {
69 65 // 使用手机 + 密码,进行登录。
... ... @@ -124,15 +120,13 @@ public class MemberAuthServiceImpl implements MemberAuthService {
124 120 @Override
125 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 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 1 package cn.iocoder.yudao.module.system.api.social;
2 2  
3 3 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  4 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 5 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
5 6  
6 7 /**
... ... @@ -21,7 +22,7 @@ public interface SocialClientApi {
21 22 String getAuthorizeUrl(Integer type, Integer userType, String redirectUri);
22 23  
23 24 /**
24   - * 创建微信 JS SDK 初始化所需的签名
  25 + * 创建微信公众号 JS SDK 初始化所需的签名
25 26 *
26 27 * @param userType 用户类型
27 28 * @param url 访问的 URL 地址
... ... @@ -29,4 +30,13 @@ public interface SocialClientApi {
29 30 */
30 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 119 ErrorCode SOCIAL_USER_UNBIND_NOT_SELF = new ErrorCode(1_002_018_001, "社交解绑失败,非当前用户绑定");
120 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 124 // ========== 系统敏感词 1-002-019-000 =========
123 125 ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_002_019_000, "系统敏感词在所有标签中都不存在");
124 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 1 package cn.iocoder.yudao.module.system.api.social;
2 2  
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 4 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  5 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 6 import cn.iocoder.yudao.module.system.convert.social.SocialClientConvert;
5 7 import cn.iocoder.yudao.module.system.service.social.SocialClientService;
6 8 import me.chanjar.weixin.common.bean.WxJsapiSignature;
... ... @@ -32,4 +34,10 @@ public class SocialClientApiImpl implements SocialClientApi {
32 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 1 package cn.iocoder.yudao.module.system.convert.social;
2 2  
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 4 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
  5 +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
4 6 import me.chanjar.weixin.common.bean.WxJsapiSignature;
5 7 import org.mapstruct.Mapper;
6 8 import org.mapstruct.factory.Mappers;
... ... @@ -12,4 +14,6 @@ public interface SocialClientConvert {
12 14  
13 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 1 package cn.iocoder.yudao.module.system.service.social;
2 2  
  3 +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
3 4 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
4 5 import com.xingyuv.jushauth.model.AuthUser;
5 6 import me.chanjar.weixin.common.bean.WxJsapiSignature;
... ... @@ -32,8 +33,10 @@ public interface SocialClientService {
32 33 */
33 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 41 * @param userType 用户类型
39 42 * @param url 访问的 URL 地址
... ... @@ -41,4 +44,15 @@ public interface SocialClientService {
41 44 */
42 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 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 7 import cn.hutool.core.bean.BeanUtil;
4 8 import cn.hutool.core.lang.Assert;
5 9 import cn.hutool.core.util.ReflectUtil;
... ... @@ -10,6 +14,7 @@ import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
10 14 import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
11 15 import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper;
12 16 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
  17 +import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
13 18 import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
14 19 import com.google.common.cache.CacheLoader;
15 20 import com.google.common.cache.LoadingCache;
... ... @@ -22,6 +27,7 @@ import com.xingyuv.jushauth.utils.AuthStateUtils;
22 27 import lombok.SneakyThrows;
23 28 import lombok.extern.slf4j.Slf4j;
24 29 import me.chanjar.weixin.common.bean.WxJsapiSignature;
  30 +import me.chanjar.weixin.common.error.WxErrorException;
25 31 import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
26 32 import me.chanjar.weixin.mp.api.WxMpService;
27 33 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
... ... @@ -35,6 +41,7 @@ import java.util.Objects;
35 41  
36 42 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
37 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 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 57 private YudaoAuthRequestFactory yudaoAuthRequestFactory;
51 58  
52 59 @Resource
53   - private WxMpService mpService;
  60 + private WxMpService wxMpService;
54 61 @Resource
55   - private StringRedisTemplate stringRedisTemplate; // WxMpService 需要使用到,所以在 Service 注入了它
  62 + private WxMpProperties wxMpProperties;
56 63 @Resource
57   - private WxMpProperties mpProperties;
  64 + private StringRedisTemplate stringRedisTemplate; // WxMpService 需要使用到,所以在 Service 注入了它
58 65 /**
59 66 * 缓存 WxMpService 对象
60 67 *
... ... @@ -63,14 +70,35 @@ public class SocialClientServiceImpl implements SocialClientService {
63 70 *
64 71 * 为什么要做 WxMpService 缓存?因为 WxMpService 构建成本比较大,所以尽量保证它是单例。
65 72 */
66   - private final LoadingCache<String, WxMpService> mpServiceCache = CacheUtils.buildAsyncReloadingCache(
  73 + private final LoadingCache<String, WxMpService> wxMpServiceCache = CacheUtils.buildAsyncReloadingCache(
67 74 Duration.ofSeconds(10L),
68 75 new CacheLoader<String, WxMpService>() {
69 76  
70 77 @Override
71 78 public WxMpService load(String key) {
72 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 157 return request;
130 158 }
131 159  
  160 + // =================== 微信公众号独有 ===================
  161 +
132 162 @Override
133 163 @SneakyThrows
134 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 172 * @param userType 用户类型
143 173 * @return WxMpService 对象
144 174 */
145   - private WxMpService buildMpService(Integer userType) {
  175 + private WxMpService getWxMpService(Integer userType) {
146 176 // 第一步,查询 DB 的配置项,获得对应的 WxMpService 对象
147 177 SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(
148 178 SocialTypeEnum.WECHAT_MP.getType(), userType);
149 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 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 190 * @param clientSecret 微信公众号 secret
161 191 * @return WxMpService 对象
162 192 */
163   - private WxMpService buildMpService(String clientId, String clientSecret) {
  193 + private WxMpService buildWxMpService(String clientId, String clientSecret) {
164 194 // 第一步,创建 WxMpRedisConfigImpl 对象
165 195 WxMpRedisConfigImpl configStorage = new WxMpRedisConfigImpl(
166 196 new RedisTemplateWxRedisOps(stringRedisTemplate),
167   - mpProperties.getConfigStorage().getKeyPrefix());
  197 + wxMpProperties.getConfigStorage().getKeyPrefix());
168 198 configStorage.setAppId(clientId);
169 199 configStorage.setSecret(clientSecret);
170 200  
... ... @@ -174,4 +204,55 @@ public class SocialClientServiceImpl implements SocialClientService {
174 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 }
... ...