From 65e5e0a89ae66f3966290af58db5ed82f66531c1 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Wed, 07 Sep 2022 10:23:26 +0800
Subject: [PATCH] 20220902

---
 src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java |  109 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 77 insertions(+), 32 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java b/src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java
index 2b29706..7ea2f40 100644
--- a/src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java
@@ -17,9 +17,13 @@
 import cc.mrbird.febs.pay.util.WechatConfigure;
 import cc.mrbird.febs.pay.util.WeixinServiceUtil;
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.text.StrFormatter;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.baomidou.dynamic.datasource.toolkit.Base64;
@@ -32,6 +36,7 @@
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
+import cn.hutool.core.io.FileUtil;
 
 import java.io.*;
 import java.math.BigDecimal;
@@ -248,38 +253,78 @@
 
     @Override
     public FebsResponse generateQrCode(WxGenerateQrCodeDto wxGenerateQrCodeDto) {
-        String base64 = null;
-//        try {
-//            RestTemplate restTemplate = new RestTemplate();
-//            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY);
-//            Map<String, Object> params = new HashMap<>();
-//            params.put("scene", wxGenerateQrCodeDto.getScene());
-//            params.put("page", wxGenerateQrCodeDto.getPage());
-//            params.put("width", 430);
-//            ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, params, byte[].class);
-//            log.info(String.valueOf(JSONUtil.parse(responseEntity)));
-//            if (responseEntity.getStatusCode() == HttpStatus.OK) {
-//                byte[] body = responseEntity.getBody();
-//                InputStream inputStream = new ByteArrayInputStream(body);
-//                // 将获取流转为base64格式
-//                byte[] data = null;
-//                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
-//                byte[] buff = new byte[100];
-//                int rc = 0;
-//                while ((rc = inputStream.read(buff, 0, 100)) > 0) {
-//                    swapStream.write(buff, 0, rc);
-//                }
-//                data = swapStream.toByteArray();
-//                base64 = Base64.byteArrayToBase64(data);
-//                inputStream.close();
-//                swapStream.close();
-//
-//                return new FebsResponse().success().data(body);
-//            }
-//        } catch (IOException e) {
-//            throw new ApiException("生成二维码失败");
-//        }
-        return new FebsResponse().success();
+        String randomNum = MallUtils.getRandomNum(5);
+        String imgName="user_" + randomNum + "_acode_1.jpg";
+        String codeImgPath = generateAcode(wxGenerateQrCodeDto.getScene(), wxGenerateQrCodeDto.getPage(), imgName, "800px", null);
+        return new FebsResponse().success().data(codeImgPath);
+    }
+
+
+    /**
+     * 生成小程序码
+     * @param scene 参数
+     * @param path  跳转路径
+     * @param imgName  图片唯一名称
+     * @return
+     */
+    //图片上传路径
+    public  static  final String IMG_UPLOAD_PATH="/mnt/sdc/webresource/groupbuy/wxcode";
+    public  String generateAcode(String scene,String path,String imgName,String width, Integer type){
+        String urlPrefix="http://hwfile.csxuncong.com/groupbuy/wxcode";
+        String imgPath=IMG_UPLOAD_PATH+imgName;
+        if(!FileUtil.exist(imgPath)){
+
+            cn.hutool.json.JSONObject obj = JSONUtil.createObj();
+            //调用二维码接口
+            String url = null;
+            if (type == null) {
+                // 该接口无数量限制,但是 scene 传参最大字符长度为32个字符
+                url = StrFormatter.format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={}",redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString());
+                obj.put("scene", scene);
+                obj.put("path", path);
+            } else {
+                // 该接口存在数量限制, 总共可生成10w个, 但参数是接在path后面
+                url = StrFormatter.format("https://api.weixin.qq.com/wxa/getwxacode?access_token={}", redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString());
+                obj.put("path", path + "?" + scene );
+            }
+
+            //最小 280px,最大 1280px
+            obj.put("width", width);
+            obj.put("auto_color", false);
+            cn.hutool.json.JSONObject obj2 = JSONUtil.createObj();
+            obj2.put("r", 0);
+            obj2.put("g", 0);
+            obj2.put("b", 0);
+            obj.put("line_color", obj2);
+            //是否需要透明底色,为 true 时,生成透明底色的小程序码
+            obj.put("is_hyaline", false);
+            try {
+                HttpResponse execute = HttpRequest.post(url).body(obj.toString(), "application/json").execute();
+                InputStream inputStream = execute.bodyStream();
+                File file = new File(imgPath);
+                FileUtil.writeFromStream(inputStream, file);
+                long uploadUrl = FileUtil.size(file);
+                //小于10kb重新生成
+                if(uploadUrl<=10240){
+                    log.error("生成微信小程序码失败:图片大小异常:{}",uploadUrl);
+                    return null;
+                }
+            } catch (Exception e) {
+                log.error("生成微信小程序码失败",e);
+                return null;
+            }
+
+        }else {
+            //判断文件是否正常 不正常 删除
+            File file = new File(imgPath);
+            long uploadUrl = FileUtil.size(file);
+            if(uploadUrl<=10240){
+                FileUtil.del(file);
+                return null;
+            }
+        }
+        log.debug("生成微信小程序码成功,路径:" + imgPath);
+        return urlPrefix+"/"+imgName;
     }
 
     /**

--
Gitblit v1.9.1