Переглянути джерело

fixed 店铺二维码功能

fushengqian 11 місяців тому
батько
коміт
3eb9d9f133

+ 75 - 0
fuint-application/src/main/java/com/fuint/common/enums/QrCodeEnum.java

@@ -0,0 +1,75 @@
+package com.fuint.common.enums;
+
+/**
+ * 二维码枚举
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+public enum QrCodeEnum {
+    TABLE("table", "桌码二维码", "pages/category/index"),
+    STORE("store", "店铺二维码", "pages/index/index");
+
+    private String key;
+
+    private String value;
+
+    private String page;
+
+    QrCodeEnum(String key, String value, String page) {
+        this.key = key;
+        this.value = value;
+        this.page = page;
+    }
+
+    public String getKey() {
+        return key;
+    }
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getValue() {
+        return value;
+    }
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public String getPage() {
+        return page;
+    }
+    public void setPage(String page) {
+        this.page = page;
+    }
+
+    // 普通方法,通过key获取value
+    public static String getValue(String k) {
+        for (QrCodeEnum c : QrCodeEnum.values()) {
+            if (c.getKey().equals(k)) {
+                return c.getValue();
+            }
+        }
+        return null;
+    }
+
+    // 普通方法,通过Value获取key
+    public static String getKey(String v) {
+        for (QrCodeEnum c : QrCodeEnum.values()) {
+            if (c.getValue() == v) {
+                return c.getKey();
+            }
+        }
+        return null;
+    }
+
+    // 普通方法,通过key获取page
+    public static String getPage(String k) {
+        for (QrCodeEnum c : QrCodeEnum.values()) {
+            if (c.getKey().equals(k)) {
+                return c.getPage();
+            }
+        }
+        return null;
+    }
+}

+ 5 - 3
fuint-application/src/main/java/com/fuint/common/service/WeixinService.java

@@ -135,14 +135,16 @@ public interface WeixinService {
     Boolean doRefund(Integer storeId, String orderSn, BigDecimal totalAmount, BigDecimal refundAmount, String platform) throws BusinessCheckException;
 
     /**
-     * 生成店铺二维码
+     * 生成二维码
      *
      * @param merchantId 商户ID
-     * @param storeId 店铺ID
+     * @param type 类型
+     * @param id 数据ID
+     * @param page 页面
      * @param width 宽度
      * @return
      * */
-    String createStoreQrCode(Integer merchantId, Integer storeId, Integer width) throws BusinessCheckException;
+    String createQrCode(Integer merchantId, String type, Integer id, String page, Integer width) throws BusinessCheckException;
 
     /**
      * 开通微信卡券

+ 10 - 4
fuint-application/src/main/java/com/fuint/common/service/impl/StoreServiceImpl.java

@@ -4,6 +4,7 @@ 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.dto.StoreDto;
+import com.fuint.common.enums.QrCodeEnum;
 import com.fuint.common.enums.StatusEnum;
 import com.fuint.common.enums.YesOrNoEnum;
 import com.fuint.common.service.MerchantService;
@@ -179,9 +180,6 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
             mtStore.setMerchantId(storeDto.getMerchantId());
         }
 
-        String qr = weixinService.createStoreQrCode(mtStore.getMerchantId(), mtStore.getId(), 320);
-        mtStore.setQrCode(qr);
-
         if (mtStore.getStatus() == null) {
             mtStore.setStatus(StatusEnum.ENABLED.getKey());
         }
@@ -190,6 +188,13 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
         } else {
             mtStoreMapper.updateById(mtStore);
         }
+
+        // 保存二维码
+        String page = QrCodeEnum.STORE.getPage() + "?" + QrCodeEnum.STORE.getKey() + "Id=" + mtStore.getId();
+        String qr = weixinService.createQrCode(mtStore.getMerchantId(), QrCodeEnum.STORE.getKey(), mtStore.getId(), page, 320);
+        mtStore.setQrCode(qr);
+        mtStoreMapper.updateById(mtStore);
+
         return mtStore;
     }
 
@@ -277,7 +282,8 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
         BeanUtils.copyProperties(mtStore, mtStoreDto);
 
         if (StringUtil.isEmpty(mtStore.getQrCode())) {
-            String qr = weixinService.createStoreQrCode(mtStore.getMerchantId(), mtStore.getId(), 320);
+            String page = QrCodeEnum.STORE.getPage() + "?" + QrCodeEnum.STORE.getKey() + "Id = " + mtStore.getId();
+            String qr = weixinService.createQrCode(mtStore.getMerchantId(), QrCodeEnum.STORE.getKey(), mtStore.getId(), page, 320);
             mtStoreDto.setQrCode(qr);
         }
 

+ 9 - 8
fuint-application/src/main/java/com/fuint/common/service/impl/WeixinServiceImpl.java

@@ -14,9 +14,7 @@ import com.fuint.common.dto.WxCardDto;
 import com.fuint.common.enums.*;
 import com.fuint.common.http.HttpRESTDataClient;
 import com.fuint.common.service.*;
-import com.fuint.common.util.AliyunOssUtil;
-import com.fuint.common.util.Base64Util;
-import com.fuint.common.util.RedisUtil;
+import com.fuint.common.util.*;
 import com.fuint.framework.exception.BusinessCheckException;
 import com.fuint.framework.web.ResponseObject;
 import com.fuint.repository.model.*;
@@ -637,15 +635,17 @@ public class WeixinServiceImpl implements WeixinService {
     }
 
     /***
-     * 生成店铺二维码
+     * 生成二维码
      *
      * @param merchantId 商户ID
-     * @param storeId 店铺ID
+     * @param type 类型
+     * @param id 数据ID
+     * @param page 页面
      * @param width 宽度
      * @return
      * */
     @Override
-    public String createStoreQrCode(Integer merchantId, Integer storeId, Integer width) throws BusinessCheckException {
+    public String createQrCode(Integer merchantId, String type, Integer id, String page, Integer width) throws BusinessCheckException {
         try {
             String accessToken = getAccessToken(merchantId, true,true);
             String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + accessToken;
@@ -653,7 +653,7 @@ public class WeixinServiceImpl implements WeixinService {
 
             Map<String, Object> reqData = new HashMap<>();
             reqData.put("access_token", accessToken);
-            reqData.put("path", "pages/index/index?storeId=" + storeId);
+            reqData.put("path", page);
             reqData.put("width", width);
             reqDataJsonStr = JsonUtil.toJSONString(reqData);
 
@@ -662,7 +662,8 @@ public class WeixinServiceImpl implements WeixinService {
 
             String pathRoot = env.getProperty("images.root");
             String baseImage = env.getProperty("images.path");
-            String filePath = "storeQr" + storeId + ".png";
+
+            String filePath = "Qr" + type + id + ".png";
             String path = pathRoot + baseImage + filePath;
             QRCodeUtil.saveQrCodeToLocal(bytes, path);
 

+ 107 - 0
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCommonController.java

@@ -0,0 +1,107 @@
+package com.fuint.module.backendApi.controller;
+
+import com.fuint.common.dto.AccountInfo;
+import com.fuint.common.enums.QrCodeEnum;
+import com.fuint.common.service.SettingService;
+import com.fuint.common.service.StoreService;
+import com.fuint.common.service.WeixinService;
+import com.fuint.common.util.Base64Util;
+import com.fuint.common.util.QRCodeUtil;
+import com.fuint.common.util.TokenUtil;
+import com.fuint.framework.exception.BusinessCheckException;
+import com.fuint.framework.web.BaseController;
+import com.fuint.framework.web.ResponseObject;
+import com.fuint.repository.model.MtStore;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.AllArgsConstructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
+import org.springframework.web.bind.annotation.*;
+import javax.servlet.http.HttpServletRequest;
+import java.io.ByteArrayOutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 后台公共接口控制器
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+@Api(tags="管理端-公共接口")
+@RestController
+@AllArgsConstructor
+@RequestMapping(value = "/backendApi/common")
+public class BackendCommonController extends BaseController {
+
+    private static final Logger logger = LoggerFactory.getLogger(BackendCommonController.class);
+
+    private Environment env;
+
+    /**
+     * 微信服务接口
+     * */
+    private WeixinService weixinService;
+
+    /**
+     * 系统设置服务接口
+     * */
+    private SettingService settingService;
+
+    /**
+     * 店铺服务接口
+     * */
+    private StoreService storeService;
+
+    /**
+     * 获取二维码
+     *
+     * @return
+     */
+    @ApiOperation(value = "获取二维码")
+    @RequestMapping(value = "/createQrCode", method = RequestMethod.POST)
+    @CrossOrigin
+    public ResponseObject createQrCode(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
+        String token = request.getHeader("Access-Token");
+        String type = params.get("type") != null ? params.get("type").toString() : "";
+        Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
+
+        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
+        if (accountInfo == null) {
+            return getFailureResult(1001, "请先登录");
+        }
+        Integer merchantId = 0;
+        String page = QrCodeEnum.STORE.getPage() + "?" + QrCodeEnum.STORE.getKey() + "Id=" + id;
+        if (type.equals(QrCodeEnum.TABLE.getKey())) {
+            page = QrCodeEnum.TABLE.getPage() + "?" + QrCodeEnum.TABLE.getKey() + "Id=" + id;
+        }
+        if (type.equals(QrCodeEnum.STORE.getKey())) {
+            MtStore mtStore = storeService.queryStoreById(id);
+            if (mtStore != null) {
+                merchantId = mtStore.getMerchantId();
+            }
+        }
+        String h5QrCode = "";
+        try {
+            page = env.getProperty("website.url") + "#" + page;
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            QRCodeUtil.createQrCode(out, page, 800, 800, "png", "");
+            h5QrCode = new String(Base64Util.baseEncode(out.toByteArray()), "UTF-8");
+            h5QrCode = "data:image/jpg;base64," + h5QrCode;
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+        }
+
+        String imagePath = settingService.getUploadBasePath();
+        String minAppQrCode = weixinService.createQrCode(merchantId, type, id, page, 320);
+        minAppQrCode = imagePath + minAppQrCode;
+
+        Map<String, Object> result = new HashMap<>();
+        result.put("minAppQrCode", minAppQrCode);
+        result.put("h5QrCode", h5QrCode);
+
+        return getSuccessResult(result);
+    }
+}