张延森 2 年前
父节点
当前提交
9205428ac6

+ 75
- 0
src/main/java/com/example/wenhua/common/ResponseBean.java 查看文件

@@ -0,0 +1,75 @@
1
+package com.example.wenhua.common;
2
+
3
+import java.io.Serializable;
4
+
5
+/**
6
+ * 接口统一状态返回BEAN.
7
+ */
8
+public class ResponseBean<T> implements Serializable {
9
+
10
+    private static final long serialVersionUID = 3593827217136880822L;
11
+
12
+    public static final int CODE_SUCCESS = 1000;
13
+    public static final int ERROR_AUTH_FAIL = 1001;
14
+    public static final int ERROR_AUTH_EXPIRED = 1002;
15
+    public static final int ERROR_MISSING_PARAMS = 1003;
16
+    public static final int ERROR_ILLEGAL_PARAMS = 1004;
17
+    public static final int ERROR_UNAVAILABLE = 1005;
18
+
19
+    private int code;
20
+    private String message;
21
+    private T data;
22
+    private String token;
23
+
24
+    /**
25
+     * 禁止外部 new
26
+     */
27
+    private ResponseBean() {
28
+        code = ResponseBean.CODE_SUCCESS;
29
+    }
30
+
31
+    public static <T> ResponseBean success(T data, String ...msgs) {
32
+        ResponseBean responseBean = new ResponseBean();
33
+        responseBean.code = ResponseBean.CODE_SUCCESS;
34
+        responseBean.data = data;
35
+
36
+        if (null != msgs && msgs.length > 0) {
37
+            responseBean.message = msgs[0];
38
+        }
39
+
40
+        return responseBean;
41
+    }
42
+
43
+    public static <T> ResponseBean error(String msg, int code, T ...datas) {
44
+        ResponseBean responseBean = new ResponseBean();
45
+        responseBean.code = code;
46
+        responseBean.message = msg;
47
+
48
+        if (null != datas && datas.length > 0) {
49
+            responseBean.data = datas[0];
50
+        }
51
+
52
+        return responseBean;
53
+    }
54
+
55
+    public int getCode() {
56
+        return code;
57
+    }
58
+
59
+    public String getMessage() {
60
+        return message;
61
+    }
62
+
63
+    public T getData() {
64
+        return data;
65
+    }
66
+
67
+    public String getToken() {
68
+        return token;
69
+    }
70
+
71
+    @Override
72
+    public String toString() {
73
+        return  "{ code: "+code+", message: "+message+", data: <T> }";
74
+    }
75
+}

+ 14
- 24
src/main/java/com/example/wenhua/config/WxMpConfiguration.java 查看文件

@@ -1,9 +1,7 @@
1 1
 package com.example.wenhua.config;
2 2
 
3
-//import com.github.binarywang.demo.wx.mp.handler.*;
4 3
 import lombok.AllArgsConstructor;
5 4
 import me.chanjar.weixin.common.redis.JedisWxRedisOps;
6
-import me.chanjar.weixin.mp.api.WxMpMessageRouter;
7 5
 import me.chanjar.weixin.mp.api.WxMpService;
8 6
 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
9 7
 import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
@@ -14,9 +12,6 @@ import org.springframework.context.annotation.Configuration;
14 12
 import redis.clients.jedis.JedisPool;
15 13
 import redis.clients.jedis.JedisPoolConfig;
16 14
 
17
-import java.util.List;
18
-import java.util.stream.Collectors;
19
-
20 15
 /**
21 16
  * wechat mp configuration
22 17
  *
@@ -31,31 +26,26 @@ public class WxMpConfiguration {
31 26
     @Bean
32 27
     public WxMpService wxMpService() {
33 28
         // 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
34
-        final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
29
+        final WxMpProperties.MpConfig configs = this.properties.getConfigs();
35 30
         if (configs == null) {
36 31
             throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
37 32
         }
38 33
 
39 34
         WxMpService service = new WxMpServiceImpl();
40
-        service.setMultiConfigStorages(configs
41
-                .stream().map(a -> {
42
-                    WxMpDefaultConfigImpl configStorage;
43
-                    if (this.properties.isUseRedis()) {
44
-                        final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
45
-                        JedisPoolConfig poolConfig = new JedisPoolConfig();
46
-                        JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
47
-                                redisConfig.getTimeout(), redisConfig.getPassword());
48
-                        configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
49
-                    } else {
50
-                        configStorage = new WxMpDefaultConfigImpl();
51
-                    }
35
+        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
36
+        if (this.properties.isUseRedis()) {
37
+            final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
38
+            JedisPoolConfig poolConfig = new JedisPoolConfig();
39
+            JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
40
+                    redisConfig.getTimeout(), redisConfig.getPassword());
41
+            configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), configs.getAppId());
42
+        }
43
+        configStorage.setAppId(configs.getAppId());
44
+        configStorage.setSecret(configs.getSecret());
45
+        configStorage.setToken(configs.getToken());
46
+        configStorage.setAesKey(configs.getAesKey());
52 47
 
53
-                    configStorage.setAppId(a.getAppId());
54
-                    configStorage.setSecret(a.getSecret());
55
-                    configStorage.setToken(a.getToken());
56
-                    configStorage.setAesKey(a.getAesKey());
57
-                    return configStorage;
58
-                }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
48
+        service.setWxMpConfigStorage(configStorage);
59 49
         return service;
60 50
     }
61 51
 }

+ 1
- 1
src/main/java/com/example/wenhua/config/WxMpProperties.java 查看文件

@@ -50,7 +50,7 @@ public class WxMpProperties {
50 50
     /**
51 51
      * 多个公众号配置信息
52 52
      */
53
-    private List<MpConfig> configs;
53
+    private MpConfig configs;
54 54
 
55 55
     @Data
56 56
     public static class MpConfig {

+ 7
- 9
src/main/java/com/example/wenhua/controller/WxJsapiController.java 查看文件

@@ -1,13 +1,11 @@
1 1
 package com.example.wenhua.controller;
2 2
 
3
+import com.example.wenhua.common.ResponseBean;
3 4
 import lombok.AllArgsConstructor;
4 5
 import me.chanjar.weixin.common.bean.WxJsapiSignature;
5 6
 import me.chanjar.weixin.common.error.WxErrorException;
6 7
 import me.chanjar.weixin.mp.api.WxMpService;
7
-import org.springframework.web.bind.annotation.GetMapping;
8
-import org.springframework.web.bind.annotation.PathVariable;
9
-import org.springframework.web.bind.annotation.RequestMapping;
10
-import org.springframework.web.bind.annotation.RestController;
8
+import org.springframework.web.bind.annotation.*;
11 9
 
12 10
 import java.net.MalformedURLException;
13 11
 
@@ -19,14 +17,14 @@ import java.net.MalformedURLException;
19 17
  */
20 18
 @AllArgsConstructor
21 19
 @RestController
22
-@RequestMapping("/wx/jsapi/{appid}")
20
+@RequestMapping("/wx")
23 21
 public class WxJsapiController {
24 22
     private final WxMpService wxService;
25 23
 
26
-    @GetMapping("/getJsapiTicket")
27
-    public String getJsapiTicket(@PathVariable String appid) throws WxErrorException {
28
-        final WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(appid).createJsapiSignature("111");
24
+    @GetMapping("/jsapi")
25
+    public ResponseBean getJsapiTicket(@RequestParam String url) throws WxErrorException {
26
+        final WxJsapiSignature jsapiSignature = this.wxService.createJsapiSignature(url);
29 27
         System.out.println(jsapiSignature);
30
-        return this.wxService.getJsapiTicket(true);
28
+        return ResponseBean.success(jsapiSignature);
31 29
     }
32 30
 }

+ 19
- 0
src/main/java/com/example/wenhua/exception/GlobalExceptionHandler.java 查看文件

@@ -0,0 +1,19 @@
1
+package com.example.wenhua.exception;
2
+
3
+import com.example.wenhua.common.ResponseBean;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.springframework.web.bind.annotation.ExceptionHandler;
6
+import org.springframework.web.bind.annotation.ResponseBody;
7
+import org.springframework.web.bind.annotation.RestControllerAdvice;
8
+
9
+@Slf4j
10
+@RestControllerAdvice
11
+public class GlobalExceptionHandler {
12
+
13
+    @ResponseBody
14
+    @ExceptionHandler(Exception.class)
15
+    public ResponseBean handleException(Exception e){
16
+        log.error(e.getMessage(),e);
17
+        return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
18
+    }
19
+}

+ 11
- 6
src/main/resources/application.yml 查看文件

@@ -1,11 +1,16 @@
1
+server:
2
+  port: 7088
3
+  servlet:
4
+    context-path: /api
5
+
1 6
 wx:
2 7
   mp:
3 8
     useRedis: false
4 9
     redisConfig:
5
-      host: 127.0.0.1
6
-      port: 6379
10
+      host:
11
+      port:
7 12
     configs:
8
-      - appId: wxc10b9358486f6009 # ???????appid
9
-        secret: 57579fc5d9f54ecdc914255ccd4c3206 # ????appsecret
10
-        token: 111 # ??????Token?
11
-        aesKey: 111 # ??????EncodingAESKey?
13
+      appId: wxd3bab568bc42d1de
14
+      secret: a29e70e90686b36fd3a9ab513d43558e
15
+      token: fd743543e7710bb0834671c4fa2622d2
16
+      aesKey: SreLm50AMt6SV9AFfbMLzqYhKN3CnR24cUDZCzumKj2

+ 0
- 13
src/test/java/com/example/wenhua/WenhuaApplicationTests.java 查看文件

@@ -1,13 +0,0 @@
1
-package com.example.wenhua;
2
-
3
-import org.junit.jupiter.api.Test;
4
-import org.springframework.boot.test.context.SpringBootTest;
5
-
6
-@SpringBootTest
7
-class WenhuaApplicationTests {
8
-
9
-	@Test
10
-	void contextLoads() {
11
-	}
12
-
13
-}