Browse Source

fixed 生成二维码异常捕获

fushengqian 11 months ago
parent
commit
677313e653

+ 39 - 0
fuint-application/src/main/java/com/fuint/common/http/HttpRESTDataClient.java

@@ -1,16 +1,24 @@
 package com.fuint.common.http;
 
+import com.alibaba.fastjson.JSONObject;
 import com.fuint.utils.StringUtil;
 import okhttp3.*;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.HttpClient;
@@ -58,6 +66,37 @@ public class HttpRESTDataClient {
         return response.body().bytes();
     }
 
+    /**
+     *  请求
+     * @param url
+     * @param jsonParam
+     * @return
+     */
+    public static InputStream doWXPost(String url, JSONObject jsonParam) {
+        InputStream instreams = null;
+        HttpPost httpRequst = new HttpPost(url);// 创建HttpPost对象
+        try {
+            StringEntity se = new StringEntity(jsonParam.toString(),"utf-8");
+            se.setContentType("application/json");
+            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
+            httpRequst.setEntity(se);
+            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
+            if (httpResponse.getStatusLine().getStatusCode() == 200) {
+                HttpEntity httpEntity = httpResponse.getEntity();
+                if (httpEntity != null) {
+                    instreams = httpEntity.getContent();
+                }
+            }
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        } catch (ClientProtocolException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return instreams;
+    }
+
     public static String requestPost(String url, String contentType, String postData) throws IOException {
         MediaType mediaType = null;
         if (StringUtil.isNotEmpty(contentType)) {

+ 6 - 7
fuint-application/src/main/java/com/fuint/common/service/impl/BookServiceImpl.java

@@ -3,7 +3,6 @@ package com.fuint.common.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-
 import com.fuint.common.service.BookService;
 import com.fuint.common.service.StoreService;
 import com.fuint.framework.annoation.OperationServiceLog;
@@ -95,13 +94,13 @@ public class BookServiceImpl extends ServiceImpl<MtBookMapper, MtBook> implement
     }
 
     /**
-     * 添加预约
+     * 添加预约项目
      *
      * @param mtBook 预约信息
      * @return
      */
     @Override
-    @OperationServiceLog(description = "添加预约")
+    @OperationServiceLog(description = "添加预约项目")
     public MtBook addBook(MtBook mtBook) throws BusinessCheckException {
         Integer storeId = mtBook.getStoreId() == null ? 0 : mtBook.getStoreId();
         if (mtBook.getMerchantId() == null || mtBook.getMerchantId() <= 0) {
@@ -134,11 +133,11 @@ public class BookServiceImpl extends ServiceImpl<MtBookMapper, MtBook> implement
      */
     @Override
     public MtBook getBookById(Integer id) {
-        return mtBookMapper.selectById(id);
+       return mtBookMapper.selectById(id);
     }
 
     /**
-     * 修改预约
+     * 修改预约项目
      *
      * @param  mtBook
      * @throws BusinessCheckException
@@ -146,11 +145,11 @@ public class BookServiceImpl extends ServiceImpl<MtBookMapper, MtBook> implement
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    @OperationServiceLog(description = "修改预约")
+    @OperationServiceLog(description = "修改预约项目")
     public MtBook updateBook(MtBook mtBook) throws BusinessCheckException {
         MtBook book = getBookById(mtBook.getId());
         if (book == null) {
-            throw new BusinessCheckException("该预约状态异常");
+            throw new BusinessCheckException("该预约项目状态异常");
         }
         book.setId(book.getId());
         if (book.getLogo() != null) {

+ 57 - 25
fuint-application/src/main/java/com/fuint/common/service/impl/WeixinServiceImpl.java

@@ -523,7 +523,11 @@ public class WeixinServiceImpl implements WeixinService {
     @Override
     public Boolean doSendSubscribeMessage(Integer merchantId, String reqDataJsonStr) {
         try {
-            String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(merchantId, true,true);
+            String token = getAccessToken(merchantId, true,true);
+            if (StringUtil.isEmpty(token)) {
+                return false;
+            }
+            String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + token;
             String response = HttpRESTDataClient.requestPost(url, "application/json; charset=utf-8", reqDataJsonStr);
             logger.info("WeixinService sendSubscribeMessage response={}", response);
             JSONObject json = (JSONObject) JSONObject.parse(response);
@@ -648,6 +652,10 @@ public class WeixinServiceImpl implements WeixinService {
     public String createQrCode(Integer merchantId, String type, Integer id, String page, Integer width) throws BusinessCheckException {
         try {
             String accessToken = getAccessToken(merchantId, true,true);
+            if (StringUtil.isEmpty(accessToken)) {
+                throw new BusinessCheckException("生成二维码出错,请检查小程序配置");
+            }
+
             String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + accessToken;
             String reqDataJsonStr = "";
 
@@ -657,34 +665,58 @@ public class WeixinServiceImpl implements WeixinService {
             reqData.put("width", width);
             reqDataJsonStr = JsonUtil.toJSONString(reqData);
 
-            byte[] bytes = HttpRESTDataClient.requestPost(url, reqDataJsonStr);
-            logger.info("WechatService createStoreQrCode reqData:{}", reqDataJsonStr);
-
-            String pathRoot = env.getProperty("images.root");
-            String baseImage = env.getProperty("images.path");
-
-            String filePath = "Qr" + type + id + ".png";
-            String path = pathRoot + baseImage + filePath;
-            QRCodeUtil.saveQrCodeToLocal(bytes, path);
-
-            // 上传阿里云oss
-            String mode = env.getProperty("aliyun.oss.mode");
-            if (mode.equals("1")) { // 检查是否开启上传
-                String endpoint = env.getProperty("aliyun.oss.endpoint");
-                String accessKeyId = env.getProperty("aliyun.oss.accessKeyId");
-                String accessKeySecret = env.getProperty("aliyun.oss.accessKeySecret");
-                String bucketName = env.getProperty("aliyun.oss.bucketName");
-                String folder = env.getProperty("aliyun.oss.folder");
-                OSS ossClient = AliyunOssUtil.getOSSClient(accessKeyId, accessKeySecret, endpoint);
-                File ossFile = new File(path);
-                return AliyunOssUtil.upload(ossClient, ossFile, bucketName, folder);
-            } else {
-                return baseImage + filePath;
+            JSONObject jsonParam = new JSONObject();
+            jsonParam.put("path", page);
+            jsonParam.put("width", width);
+
+            InputStream inputStream = HttpRESTDataClient.doWXPost(url, jsonParam);
+
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            byte[] buffer = new byte[1024];
+            int n;
+            while (-1 != (n = inputStream.read(buffer))) {
+                output.write(buffer, 0, n);
+            }
+            byte[] bytes = output.toByteArray();
+
+            String resStr = output.toString();
+            try {
+                JSONObject res = JSON.parseObject(resStr);
+                String errCode = res.get("errcode").toString();
+                if (errCode.equals("40001")) {
+                    getAccessToken(merchantId, true, false);
+                }
+            } catch (Exception e) {
+                logger.info("WechatService createStoreQrCode reqData:{}", reqDataJsonStr);
+
+                String pathRoot = env.getProperty("images.root");
+                String baseImage = env.getProperty("images.path");
+
+                String filePath = "Qr" + type + id + ".png";
+                String path = pathRoot + baseImage + filePath;
+                QRCodeUtil.saveQrCodeToLocal(bytes, path);
+
+                // 上传阿里云oss
+                String mode = env.getProperty("aliyun.oss.mode");
+                if (mode.equals("1")) { // 检查是否开启上传
+                    String endpoint = env.getProperty("aliyun.oss.endpoint");
+                    String accessKeyId = env.getProperty("aliyun.oss.accessKeyId");
+                    String accessKeySecret = env.getProperty("aliyun.oss.accessKeySecret");
+                    String bucketName = env.getProperty("aliyun.oss.bucketName");
+                    String folder = env.getProperty("aliyun.oss.folder");
+                    OSS ossClient = AliyunOssUtil.getOSSClient(accessKeyId, accessKeySecret, endpoint);
+                    File ossFile = new File(path);
+                    return AliyunOssUtil.upload(ossClient, ossFile, bucketName, folder);
+                } else {
+                    return baseImage + filePath;
+                }
             }
         } catch (Exception e) {
             logger.error("生成店铺二维码出错啦:{}", e.getMessage());
-            throw new BusinessCheckException("生成店铺二维码出错,请检查小程序配置");
+            throw new BusinessCheckException("生成二维码出错,请检查小程序配置.");
         }
+
+        throw new BusinessCheckException("生成二维码出错,请稍后再试.");
     }
 
     /**

+ 1 - 1
fuint-repository/src/main/java/com/fuint/repository/model/MtBookItem.java

@@ -70,7 +70,7 @@ public class MtBookItem implements Serializable {
     @ApiModelProperty("最后操作人")
     private String operator;
 
-    @ApiModelProperty("A:正常;D:删除")
+    @ApiModelProperty("A:已提交;B:审核通过;C:审核未通过;D:删除;E:已完成")
     private String status;
 
 }