Jelajahi Sumber

fixed 领取会员卡功能

fushengqian 1 tahun lalu
induk
melakukan
786bd51f3a

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

@@ -2,10 +2,18 @@ package com.fuint.common.http;
 
 import com.fuint.utils.StringUtil;
 import okhttp3.*;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+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 org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
 
 
 /**
@@ -21,6 +29,12 @@ public class HttpRESTDataClient {
 
     private static final OkHttpClient client = new OkHttpClient();
 
+    private static HttpClientBuilder httpClientBuilder;
+
+    static {
+        httpClientBuilder = HttpClientBuilder.create();
+    }
+
     public static String requestGet(String url) throws IOException {
         Request request = new Request.Builder()
                 .url(url)
@@ -58,4 +72,29 @@ public class HttpRESTDataClient {
 
         return response.body().string();
     }
+
+    public static String requestPostBody(String url, String body) {
+        logger.debug("[HttpRESTDataClient] [requestPostBody] 入参 url={} body={}", url, body);
+
+        HttpPost httpPost = new HttpPost(url);
+        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
+        StringEntity entity = new StringEntity(body, "utf-8");
+        entity.setContentEncoding("UTF-8");
+        entity.setContentType("application/json");
+        httpPost.setEntity(entity);
+
+        try {
+            HttpClient client = httpClientBuilder.build();
+            HttpResponse response = client.execute(httpPost);
+            if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                String result = EntityUtils.toString(response.getEntity(), "utf-8");
+                return result;
+            }
+            return "";
+        }
+        catch (IOException ex) {
+            logger.error("[HttpRESTDataClient] [requestPostBody] 请求异常 ex={}", url, ex);
+            return "";
+        }
+    }
 }

+ 14 - 2
fuint-application/src/main/java/com/fuint/common/service/WeixinService.java

@@ -153,11 +153,23 @@ public interface WeixinService {
     String createWxCard(Integer merchantId) throws BusinessCheckException;
 
     /**
-     * 微信卡券apiTicket
+     * 创建微信卡券领取的二维码
      *
      * @param merchantId 商户ID
+     * @param cardId 微信卡券ID
+     * @param code 会员卡编码
      * @return
      * */
-    String getApiTicket(Integer merchantId);
+    String createCardQrCode(Integer merchantId, String cardId, String code);
+
+    /**
+     * 是否已领取卡券
+     *
+     * @param merchantId 商户ID
+     * @param cardId 微信卡券ID
+     * @param openId openId
+     * @return
+     * */
+    Boolean isOpenCard(Integer merchantId, String cardId, String openId);
 
 }

+ 1 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/SendSmsServiceImpl.java

@@ -184,6 +184,7 @@ public class SendSmsServiceImpl implements SendSmsService {
             String res = "";
             try {
                 CommonResponse response = client.getCommonResponse(request);
+                logger.info("sendMessage response:{}", response.toString());
                 res = response.getData();
                 System.out.println(response.getData());
             } catch (ServerException e) {

+ 71 - 7
fuint-application/src/main/java/com/fuint/common/service/impl/WeixinServiceImpl.java

@@ -15,6 +15,7 @@ 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.framework.exception.BusinessCheckException;
 import com.fuint.framework.web.ResponseObject;
@@ -769,28 +770,91 @@ public class WeixinServiceImpl implements WeixinService {
     }
 
     /**
-     * 微信卡券apiTicket
+     * 创建微信卡券领取的二维码
      *
      * @param merchantId 商户ID
+     * @param cardId 微信卡券ID
+     * @param code 会员卡编码
      * @return
      * */
     @Override
-    public String getApiTicket(Integer merchantId) {
+    public String createCardQrCode(Integer merchantId, String cardId, String code) {
         try {
-            String accessToken = getAccessToken(merchantId, false,true);
-            String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=wx_card";
-            String response = HttpRESTDataClient.requestGet(url);
-            logger.error("微信卡券apiTicket接口返回:{}", response);
+            String accessToken = getAccessToken(merchantId, false,false);
+            String url = "https://api.weixin.qq.com/card/qrcode/create?access_token="+accessToken;
+
+            Map<String, Object> param = new HashMap<>();
+            Map<String, Object> actionInfo = new HashMap<>();
+            Map<String, Object> card = new HashMap<>();
+            card.put("card_id", cardId);
+            card.put("code", code);
+            card.put("is_unique_code", false);
+            card.put("outer_str", "12b");
+            actionInfo.put("card", card);
+            param.put("action_name", "QR_CARD");
+            param.put("action_info", actionInfo);
+
+            String reqDataJsonStr = JsonUtil.toJSONString(param);
+            String response = HttpRESTDataClient.requestPostBody(url, reqDataJsonStr);
+            logger.info("微信卡券createCardQrCode接口返回:{}", response);
             JSONObject data = (JSONObject) JSONObject.parse(response);
+            String qrCode = "";
             if (data.get("errcode").toString().equals("0")) {
-                return data.get("ticket").toString();
+                String content = data.get("url").toString();
+                try {
+                    // 生成并输出二维码
+                    ByteArrayOutputStream out = new ByteArrayOutputStream();
+                    com.fuint.common.util.QRCodeUtil.createQrCode(out, content, 800, 800, "png", "");
+                    // 对数据进行Base64编码
+                    qrCode = new String(Base64Util.baseEncode(out.toByteArray()), "UTF-8");
+                    return "data:image/jpg;base64," + qrCode;
+                } catch (Exception e) {
+                    logger.error("生成并输出二维码出错:{}", e.getMessage());
+                }
             }
         } catch (Exception e) {
+            logger.error("创建微信卡券领取二维码出错:{}", e.getMessage());
             return "";
         }
         return "";
     }
 
+    /**
+     * 是否已领取卡券
+     *
+     * @param merchantId 商户ID
+     * @param cardId 微信卡券ID
+     * @param openId openId
+     * @return
+     * */
+    @Override
+    public Boolean isOpenCard(Integer merchantId, String cardId, String openId) {
+        try {
+            String accessToken = getAccessToken(merchantId, false,false);
+            String url = "https://api.weixin.qq.com/card/user/getcardlist?access_token="+accessToken;
+
+            Map<String, Object> param = new HashMap<>();
+            param.put("openid", openId);
+            param.put("card_id", cardId);
+
+            String reqDataJsonStr = JsonUtil.toJSONString(param);
+            String response = HttpRESTDataClient.requestPostBody(url, reqDataJsonStr);
+            logger.info("微信卡券createCardQrCode接口返回:{}", response);
+            JSONObject data = (JSONObject) JSONObject.parse(response);
+            if (data.get("errcode").toString().equals("0")) {
+                Object cards = data.get("card_list");
+                if (cards != null && StringUtil.isNotEmpty(cards.toString())) {
+                    return true;
+                }
+                return false;
+            }
+        } catch (Exception e) {
+            logger.error("创建微信卡券领取二维码出错:{}", e.getMessage());
+            return true;
+        }
+        return true;
+    }
+
     /**
      * 刷卡支付
      *

+ 16 - 22
fuint-application/src/main/java/com/fuint/module/clientApi/controller/ClientUserController.java

@@ -2,7 +2,6 @@ package com.fuint.module.clientApi.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import com.fuint.common.dto.AssetDto;
-import com.fuint.common.dto.OpenWxCardDto;
 import com.fuint.common.dto.UserDto;
 import com.fuint.common.dto.UserInfo;
 import com.fuint.common.enums.*;
@@ -12,8 +11,6 @@ import com.fuint.framework.exception.BusinessCheckException;
 import com.fuint.framework.web.BaseController;
 import com.fuint.framework.web.ResponseObject;
 import com.fuint.repository.model.*;
-import com.fuint.utils.Digests;
-import com.fuint.utils.Encodes;
 import com.fuint.utils.StringUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -146,36 +143,25 @@ public class ClientUserController extends BaseController {
                 }
             }
         }
-        outParams.put("isMerchant", isMerchant);
 
         // 是否开通微信会员卡
+        boolean openWxCard = false;
         if (mtUser != null && StringUtil.isNotEmpty(mtUser.getOpenId())) {
             MtSetting cardSetting = settingService.querySettingByName(mtUser.getMerchantId(), UserSettingEnum.OPEN_WX_CARD.getKey());
             if (cardSetting != null && cardSetting.getValue().equals(YesOrNoEnum.TRUE.getKey())) {
                 MtSetting cardIdSetting = settingService.querySettingByName(mtUser.getMerchantId(), UserSettingEnum.WX_MEMBER_CARD_ID.getKey());
                 if (cardIdSetting != null) {
-                    String apiTicket = weixinService.getApiTicket(mtUser.getMerchantId());
-                    if (StringUtil.isNotEmpty(apiTicket)) {
-                        String cardId = cardIdSetting.getValue();
-                        String openId = mtUser.getOpenId();
-                        String code = mtUser.getUserNo();
-                        String timestamp = (System.currentTimeMillis()/1000) + "";
-                        String nonceStr = "WeApp";
-                        String str = nonceStr + timestamp + apiTicket + cardId;
-                        byte[] signatureByte = Digests.sha1(str.getBytes());
-                        String signature = Encodes.encodeHex(signatureByte);
-                        OpenWxCardDto openWxCardDto = new OpenWxCardDto();
-                        openWxCardDto.setCode(code);
-                        openWxCardDto.setOpenId(openId);
-                        openWxCardDto.setTimestamp(timestamp);
-                        openWxCardDto.setSignature(signature);
-                        openWxCardDto.setCardId(cardId);
-                        openWxCardDto.setNonceStr(nonceStr);
-                        outParams.put("openCardPara", openWxCardDto);
+                    boolean isOpen = weixinService.isOpenCard(mtUser.getMerchantId(), cardIdSetting.getValue(), mtUser.getOpenId());
+                    if (!isOpen) {
+                        openWxCard = true;
                     }
                 }
             }
         }
+
+        outParams.put("isMerchant", isMerchant);
+        outParams.put("openWxCard", openWxCard);
+
         return getSuccessResult(outParams);
     }
 
@@ -364,9 +350,17 @@ public class ClientUserController extends BaseController {
             logger.error(e.getMessage(), e);
         }
 
+        // 微信会员卡领取二维码
+        String wxCardQrCode = "";
+        MtSetting cardIdSetting = settingService.querySettingByName(mtUser.getMerchantId(), UserSettingEnum.WX_MEMBER_CARD_ID.getKey());
+        if (cardIdSetting != null) {
+            wxCardQrCode = weixinService.createCardQrCode(mtUser.getMerchantId(), cardIdSetting.getValue(), mtUser.getUserNo());
+        }
+
         Map<String, Object> outParams = new HashMap<>();
         outParams.put("qrCode", qrCode);
         outParams.put("userInfo", mtUser);
+        outParams.put("wxCardQrCode", wxCardQrCode);
 
         return getSuccessResult(outParams);
     }