Browse Source

新增微信版本升级

赵冬冬 4 years ago
parent
commit
49dcd8a6b8

+ 1 - 1
carbon-h5/carbon-h5-service/pom.xml

@@ -14,7 +14,7 @@
     <version>0.0.1-SNAPSHOT</version>
     <name>carbon-h5-service</name>
     <properties>
-        <binarywang.weixin.version>3.1.0</binarywang.weixin.version>
+        <binarywang.weixin.version>3.9.0</binarywang.weixin.version>
     </properties>
     <dependencies>
         <dependency>

+ 13 - 17
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/config/wx/WxMaConfiguration.java

@@ -2,37 +2,33 @@ package com.hcloud.microserver.h5.config.wx;
 
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
-import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
-import org.springframework.beans.factory.annotation.Value;
+import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 @ConditionalOnClass(WxMaService.class)
+@EnableConfigurationProperties(WxMaProperties.class)
 public class WxMaConfiguration {
 
-    /**
-     * 设置微信小程序的appid
-     */
-    @Value("${wx.ma.appId}")
-    private String appId;
-
-    /**
-     * 设置微信小程序的app secret
-     */
-    @Value("${wx.ma.secret}")
-
-    private String secret;
+    private WxMaProperties wxMaProperties;
 
+    @Autowired
+    public WxMaConfiguration(WxMaProperties properties) {
+        this.wxMaProperties = properties;
+    }
 
     @Bean
     @ConditionalOnMissingBean
     public WxMaService wxMaService() {
-        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
-        config.setAppid(appId);
-        config.setSecret(secret);
+        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
+        config.setAppid(StringUtils.trimToNull(this.wxMaProperties.getAppId()));
+        config.setSecret(StringUtils.trimToNull(this.wxMaProperties.getSecret()));
         WxMaService wxMaService = new WxMaServiceImpl();
         wxMaService.setWxMaConfig(config);
         return wxMaService;

+ 31 - 0
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/config/wx/WxMaProperties.java

@@ -0,0 +1,31 @@
+package com.hcloud.microserver.h5.config.wx;
+
+
+import com.hcloud.microserver.commoncore.util.JsonUtils;
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * @author xiezt
+ */
+@Data
+@ConfigurationProperties(prefix = "wx.ma")
+public class WxMaProperties {
+    /**
+     * 设置微信公众号的appid
+     */
+    private String appId;
+
+    /**
+     * 设置微信公众号的app secret
+     */
+    private String secret;
+
+
+    @Override
+    public String toString() {
+        return JsonUtils.toJson(this);
+    }
+}

+ 20 - 4
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/config/wx/WxMpConfiguration.java

@@ -1,11 +1,12 @@
 package com.hcloud.microserver.h5.config.wx;
 
 
-
 import lombok.extern.slf4j.Slf4j;
-import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
 import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.WxOAuth2Service;
 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.api.impl.WxOAuth2ServiceImpl;
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -16,7 +17,7 @@ import org.springframework.context.annotation.Configuration;
 
 @Slf4j
 @Configuration
-@ConditionalOnClass(WxMpService.class)
+@ConditionalOnClass({WxMpService.class,WxOAuth2Service.class})
 @EnableConfigurationProperties(WxMpProperties.class)
 public class WxMpConfiguration {
 
@@ -27,10 +28,11 @@ public class WxMpConfiguration {
         this.properties = properties;
     }
 
+
     @Bean
     @ConditionalOnMissingBean
     public WxMpService wxMpService() {
-        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
+        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
         configStorage.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
         configStorage.setSecret(StringUtils.trimToNull(this.properties.getSecret()));
         configStorage.setToken(StringUtils.trimToNull(this.properties.getToken()));
@@ -39,4 +41,18 @@ public class WxMpConfiguration {
         wxMpService.setWxMpConfigStorage(configStorage);
         return wxMpService;
     }
+
+    @Bean
+    @ConditionalOnMissingBean
+    public WxOAuth2Service wxOAuth2Service() {
+        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
+        configStorage.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
+        configStorage.setSecret(StringUtils.trimToNull(this.properties.getSecret()));
+        configStorage.setToken(StringUtils.trimToNull(this.properties.getToken()));
+        configStorage.setAesKey(StringUtils.trimToNull(this.properties.getAesKey()));
+        WxMpService wxMpService = new WxMpServiceImpl();
+        wxMpService.setWxMpConfigStorage(configStorage);
+        WxOAuth2Service wxOAuth2Service = new WxOAuth2ServiceImpl(wxMpService);
+        return wxOAuth2Service;
+    }
 }

+ 0 - 1
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/config/wx/WxMpProperties.java

@@ -13,7 +13,6 @@ import org.springframework.stereotype.Component;
  */
 @Data
 @ConfigurationProperties(prefix = "wx.mp")
-@Component("properties")
 public class WxMpProperties {
     /**
      * 设置微信公众号的appid

+ 1 - 2
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/controller/web/WechatAppController.java

@@ -75,8 +75,7 @@ public class WechatAppController extends BaseController {
                 throw new RuntimeException("login handler error");
             }
             // 解密用户信息
-            WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(),
-                    request.getEncryptedData(), request.getIv());
+            WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(),request.getEncryptedData(), request.getIv());
             //解密手机号
             WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session.getSessionKey(), request.getEncryptedData(), request.getIv());
             if (null == wxUserInfo) {

+ 7 - 2
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/controller/web/WechatManagerController.java

@@ -29,6 +29,7 @@ import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.util.http.URIUtil;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.api.WxMpUserService;
+import me.chanjar.weixin.mp.api.WxOAuth2Service;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -101,6 +102,10 @@ public class WechatManagerController extends CarbonBaseController {
     @Autowired
     private RedisUtils redisUtils;
 
+
+    @Autowired
+    private WxOAuth2Service wxOAuth2Service;
+
     @ApiOperation(value = "微信登录", notes = "微信登录")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "furl", value = "授权成功后的", dataType = "String", paramType = "query"),
@@ -128,9 +133,9 @@ public class WechatManagerController extends CarbonBaseController {
         log.info("currentCustomerId is ================>{}", currentCustomerId);
         String authUrls = "";
         if (authScope == 2) {
-            authUrls = wxMpService.oauth2buildAuthorizationUrl(weChatBaseUrl, WECHAT_AUTH_BASE_SCOPE, URIUtil.encodeURIComponent(weChatForwardUrl));
+            authUrls = wxOAuth2Service.buildAuthorizationUrl(weChatBaseUrl, WECHAT_AUTH_BASE_SCOPE, URIUtil.encodeURIComponent(weChatForwardUrl));
         } else {
-            authUrls = wxMpService.oauth2buildAuthorizationUrl(wechatRedirectUrl, WECHAT_AUTH_SCOPE, URIUtil.encodeURIComponent(weChatForwardUrl));
+            authUrls = wxOAuth2Service.buildAuthorizationUrl(wechatRedirectUrl, WECHAT_AUTH_SCOPE, URIUtil.encodeURIComponent(weChatForwardUrl));
         }
         try {
             log.info("authUrls===============>{}", authUrls);

+ 3 - 3
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/entity/LoginRequest.java

@@ -10,12 +10,12 @@ public class LoginRequest {
     //原始数据字符串
     String signature;
 
-    //校验用户信息字符串
-    Object rawData;
-
     //加密用户数据
     String encryptedData;
 
     //加密算法的初始向量
     String iv;
+
+    //校验用户信息字符串
+    Object rawData;
 }