Parcourir la source

Merge remote-tracking branch 'origin/master'

小么熊🐻 il y a 3 ans
Parent
commit
9acffa9651

+ 14 - 0
pom.xml

@@ -233,6 +233,20 @@
                 <version>${feign.version}</version>
             </dependency>
 
+            <!--谷歌二维码生成-->
+            <dependency>
+                <groupId>com.google.zxing</groupId>
+                <artifactId>core</artifactId>
+                <version>3.4.0</version>
+            </dependency>
+
+            <!--hutool工具包-->
+            <dependency>
+                <groupId>cn.hutool</groupId>
+                <artifactId>hutool-all</artifactId>
+                <version>5.7.14</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 

+ 11 - 0
twzd-admin/pom.xml

@@ -69,6 +69,17 @@
             <version>1.0.1</version>
         </dependency>
 
+        <!--谷歌二维码生成-->
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+        </dependency>
+
+        <!--hutool工具包-->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 92 - 0
twzd-admin/src/main/java/com/miaxis/system/controller/test/TestController.java

@@ -0,0 +1,92 @@
+package com.miaxis.system.controller.test;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.extra.qrcode.QrCodeUtil;
+import cn.hutool.extra.qrcode.QrConfig;
+import com.alibaba.fastjson.JSONObject;
+import com.miaxis.common.annotation.Log;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.enums.BusinessTypeEnum;
+import com.miaxis.feign.dto.WxTicket;
+import com.miaxis.feign.service.IWxMpService;
+import com.miaxis.feign.service.IWxSendService;
+import com.miaxis.wx.service.IWxGzhService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import lombok.RequiredArgsConstructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import java.net.URLEncoder;
+
+/**
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/21 14:02
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping("/wxTest")
+@Api(tags={"测试"})
+public class TestController {
+
+    private static final Logger log = LoggerFactory.getLogger(TestController.class);
+
+    @Autowired
+    IWxSendService wxSendService;
+
+    @Autowired
+    IWxGzhService wxGzhService;
+
+    @Autowired
+    IWxMpService wxMpService;
+
+
+    /**
+     * 生成带参数的二维码
+     */
+    @GetMapping(value = "/generateTicket")
+    @ApiOperation("生成带参数的二维码")
+    public Response generateTicket() throws Exception{
+        String xcxMessageToken = wxGzhService.getGzhToken();
+        WxTicket wxTicket = new WxTicket();
+        wxTicket.setExpire_seconds(3000);
+        wxTicket.setAction_name("QR_SCENE");
+        JSONObject jsonObject1 = new JSONObject();
+        JSONObject jsonObject2 = new JSONObject();
+        jsonObject1.put("scene_id","123");
+        jsonObject2.put("scene",jsonObject1);
+        wxTicket.setAction_info(jsonObject2);
+        String result = wxSendService.generateTicket(xcxMessageToken,wxTicket);
+        System.out.println("生成ticket:" + result);
+        JSONObject jsonStr = JSONObject.parseObject(result);
+
+        QrCodeUtil.generate(
+                jsonStr.getString("url"), //二维码内容
+                QrConfig.create().setImg("C:\\Users\\wwl\\Desktop\\二维码\\微信图片_20211021120216.jpg"),
+                FileUtil.file("C:\\Users\\wwl\\Desktop\\二维码\\logo二维码.jpg")//写出到的文件
+        );
+
+        String decode = QrCodeUtil.decode(FileUtil.file("C:\\Users\\wwl\\Desktop\\二维码\\logo二维码.jpg"));
+        System.out.println("二维码信息:"+ decode);
+        return Response.success();
+    }
+
+
+    /**
+     * 测试
+     */
+    @Log(title = "测试", businessType = BusinessTypeEnum.UPDATE)
+    @PutMapping("/test")
+    @ApiOperation("测试")
+    public void test(Object object){
+        System.out.println("----test-----"+ object);
+        log.info("测试:"+object);
+    }
+
+
+}

+ 5 - 4
twzd-admin/src/main/resources/application-dev.yml

@@ -57,13 +57,12 @@ spring:
                         multi-statement-allow: true
     # redis 配置
     redis:
-        database: 2
         # 地址
-        host: 192.168.8.213
+        host: 1.15.29.64
         # 端口,默认为6379
         port: 6379
         # 密码
-        password:
+        password: miaxis110
         # 连接超时时间
         timeout: 10s
         lettuce:
@@ -76,10 +75,12 @@ spring:
                 max-active: 8
                 # #连接池最大阻塞等待时间(使用负值表示没有限制)
                 max-wait: -1ms
+        # 指定库
+        database: 12
 
 
 
-
+# 微信公众号
 app:
     appId: wx67ca1b8c9816ef28
     appSecret: 8604f2a6eb6338cfa64e7df4ec2c08b3

+ 3 - 0
twzd-admin/src/main/resources/application-prod.yml

@@ -75,7 +75,10 @@ spring:
                 max-active: 8
                 # #连接池最大阻塞等待时间(使用负值表示没有限制)
                 max-wait: -1ms
+        # 指定库
+        database: 12
 
+# 微信公众号
 app:
     appId: wx67ca1b8c9816ef28
     appSecret: 8604f2a6eb6338cfa64e7df4ec2c08b3

+ 22 - 0
twzd-service/src/main/java/com/miaxis/feign/dto/WxTicket.java

@@ -0,0 +1,22 @@
+package com.miaxis.feign.dto;
+
+import lombok.Data;
+
+/**
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/20 15:08
+ */
+@Data
+public class WxTicket {
+
+    private Integer expire_seconds;  //该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为60秒。
+    private String action_name;  //二维码类型,QR_SCENE为临时的整型参数值,QR_STR_SCENE为临时的字符串参数值,QR_LIMIT_SCENE为永久的整型参数值,QR_LIMIT_STR_SCENE为永久的字符串参数值
+
+    /**
+     * 二维码详细信息
+     *      参数:scene_id:场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
+     *            scene_str:场景值ID(字符串形式的ID),字符串类型,长度限制为1到64
+     */
+    private Object action_info;  //二维码详细信息
+}

+ 36 - 0
twzd-service/src/main/java/com/miaxis/feign/service/IWxMpService.java

@@ -0,0 +1,36 @@
+package com.miaxis.feign.service;
+
+import com.miaxis.common.config.FeignConfig;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import javax.servlet.annotation.HandlesTypes;
+
+/**
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/20 16:05
+ */
+@FeignClient(name="wxMpService",
+        url = "https://mp.weixin.qq.com/cgi-bin",configuration = FeignConfig.class)
+public interface IWxMpService {
+
+    /**
+     * 通过ticket换取二维码
+     * HTTP头(示例)如下:
+     *      Accept-Ranges:bytes
+     *      Cache-control:max-age=604800
+     *      Connection:keep-alive
+     *      Content-Length:28026
+     *      Content-Type:image/jpg
+     *      Date:Wed, 16 Oct 2013 06:37:10 GMT
+     *      Expires:Wed, 23 Oct 2013 14:37:10 +0800
+     *      Server:nginx/1.4.1
+     * @param ticket
+     * @return
+     */
+    @GetMapping(value = "/showqrcode"
+            ,headers = {"Accept-Ranges=bytes", "Cache-control='max-age=604800'", "Connection=keep-alive","Content-Length=28026","Content-Type=image/jpg"})
+    String showQrcode(@RequestParam("ticket") String ticket);
+}

+ 45 - 0
twzd-service/src/main/java/com/miaxis/feign/service/IWxSendService.java

@@ -0,0 +1,45 @@
+package com.miaxis.feign.service;
+
+import com.miaxis.common.config.FeignConfig;
+import com.miaxis.feign.dto.WxTicket;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * 微信公众号接口
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/20 14:59
+ */
+@FeignClient(name="wxSendService",
+        url = "https://api.weixin.qq.com/cgi-bin",configuration = FeignConfig.class)
+public interface IWxSendService {
+
+
+    /**
+     *  获取token
+     * @param grant_type
+     * @param appid 第三方用户唯一凭证
+     * @param secret 第三方用户唯一凭证密钥,即appsecret
+     * @return
+     */
+    @GetMapping(value = "/token")
+    String getAccessToken(@RequestParam("grant_type") String grant_type,
+                          @RequestParam("appid") String appid,
+                          @RequestParam("secret") String secret);
+
+
+    /**
+     * 生成带参数二维码接口
+     *      -文档链接:https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
+     * @param accessToken
+     * @param wxTicket:二维码参数
+     * @return
+     */
+    @PostMapping(value = "/qrcode/create")
+    String generateTicket(@RequestParam("access_token")String accessToken, WxTicket wxTicket);
+
+
+}

+ 15 - 0
twzd-service/src/main/java/com/miaxis/wx/service/IWxGzhService.java

@@ -0,0 +1,15 @@
+package com.miaxis.wx.service;
+
+/**
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/20 14:56
+ */
+public interface IWxGzhService {
+
+    /**
+     * 获取微信公众号token
+     * @return
+     */
+    String getGzhToken();
+}

+ 55 - 0
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -0,0 +1,55 @@
+package com.miaxis.wx.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.redis.RedisCache;
+import com.miaxis.feign.service.IWxSendService;
+import com.miaxis.wx.service.IWxGzhService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 微信公众号业务层
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/20 14:57
+ */
+@Service
+@Slf4j
+public class WxGzhServiceImpl implements IWxGzhService {
+
+
+    @Resource
+    private RedisCache redisCache;
+
+    @Resource
+    private IWxSendService wxSendService;
+
+    @Value("${app.appid}")
+    private String appid;
+
+    @Value("${app.appSecret}")
+    private String secret;
+
+
+    @Override
+    public String getGzhToken() {
+        Object cacheObject = redisCache.getCacheObject(Constants.GZH_MESSAGE_TOKEN);
+        //如果过期,重新获取并保存到redis
+        if (cacheObject == null){
+            String result = wxSendService.getAccessToken("client_credential",appid,secret);
+            JSONObject json = JSONObject.parseObject(result);
+            String token = json.getString("access_token");
+            int expiresIn = json.getIntValue("expires_in");
+            redisCache.setCacheObject(Constants.GZH_MESSAGE_TOKEN,token,expiresIn, TimeUnit.SECONDS);
+            return token;
+        }else {
+            return String.valueOf(cacheObject);
+        }
+
+    }
+}