gaodm пре 5 година
родитељ
комит
1ba7f2b5d6

+ 6 - 0
zzcx-service/pom.xml

@@ -154,6 +154,12 @@
             <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-mp</artifactId>
+            <version>3.6.0</version>
+        </dependency>
+
         <dependency>
             <groupId>io.github.lvyahui8</groupId>
             <artifactId>spring-boot-data-aggregator-starter</artifactId>

+ 47 - 0
zzcx-service/src/main/java/com/diagbot/config/wx/WxMpConfiguration.java

@@ -0,0 +1,47 @@
+package com.diagbot.config.wx;
+
+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.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2020/2/5 19:52
+ */
+@AllArgsConstructor
+@Configuration
+@EnableConfigurationProperties(WxMpProperties.class)
+public class WxMpConfiguration {
+
+    private final WxMpProperties properties;
+
+    @Bean
+    public WxMpService wxMpService() {
+        // 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
+        final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
+        if (configs == null) {
+            throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
+        }
+
+        WxMpService service = new WxMpServiceImpl();
+        service.setMultiConfigStorages(configs
+                .stream().map(a -> {
+                    WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
+                    configStorage.setAppId(a.getAppId());
+                    configStorage.setSecret(a.getSecret());
+                    configStorage.setToken(a.getToken());
+                    configStorage.setAesKey(a.getAesKey());
+                    return configStorage;
+                }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
+        return service;
+    }
+
+}

+ 46 - 0
zzcx-service/src/main/java/com/diagbot/config/wx/WxMpProperties.java

@@ -0,0 +1,46 @@
+package com.diagbot.config.wx;
+
+import com.diagbot.util.FastJsonUtils;
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.List;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2020/2/5 20:00
+ */
+@Data
+@ConfigurationProperties(prefix = "wx.mp")
+public class WxMpProperties {
+    private List<MpConfig> configs;
+
+    @Data
+    public static class MpConfig {
+        /**
+         * 设置微信公众号的appid
+         */
+        private String appId;
+
+        /**
+         * 设置微信公众号的app secret
+         */
+        private String secret;
+
+        /**
+         * 设置微信公众号的token
+         */
+        private String token;
+
+        /**
+         * 设置微信公众号的EncodingAESKey
+         */
+        private String aesKey;
+    }
+
+    @Override
+    public String toString() {
+        return FastJsonUtils.getBeanToJson(this);
+    }
+}

+ 43 - 0
zzcx-service/src/main/java/com/diagbot/web/WeXinJsSdkController.java

@@ -0,0 +1,43 @@
+package com.diagbot.web;
+
+import io.swagger.annotations.Api;
+import lombok.AllArgsConstructor;
+import me.chanjar.weixin.common.bean.WxJsapiSignature;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2020/2/5 18:03
+ */
+@AllArgsConstructor
+@RestController
+@Api(value = "微信验证", tags = { "微信验证API" })
+@RequestMapping("jssdk/{appid}")
+public class WeXinJsSdkController {
+
+    private final WxMpService wxMpService;
+
+    @RequestMapping(value = "/config", method = RequestMethod.GET)
+    public WxJsapiSignature wxJsSdkConfig(HttpServletRequest request, @PathVariable String appid, String url) {
+        if (!this.wxMpService.switchover(appid)) {
+            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
+        }
+
+        try { // 直接调用wxMpServer 接口
+            WxJsapiSignature wxJsapiSignature = wxMpService.createJsapiSignature(url);
+            return wxJsapiSignature;
+        } catch (WxErrorException e) {
+            return null;
+        }
+    }
+
+}

+ 12 - 0
zzcx-service/src/main/resources/bootstrap.yml

@@ -16,4 +16,16 @@ eureka:
     serviceUrl:
       defaultZone: http://${myuri}:8761/eureka/
 
+wx:
+  mp:
+    configs:
+    - appId: wxedd53be102996426
+      secret: c164e280950342f166a933dd7aa6daf7
+      token: testwxshare123
+      aesKey:
+    - appId: 2222 (另一个公众号的appid,以下同上)
+      secret: 1111
+      token: 111
+      aesKey: 111
+
 myuri: localhost