Bläddra i källkod

fix 记录公众号永久渠道用户来源

zoujiajian 3 år sedan
förälder
incheckning
689bb6e4ac

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/UserGzhRelation.java

@@ -32,4 +32,9 @@ public class UserGzhRelation extends BaseEntity{
 	 * 是否是扫描二维码
 	 */
 	private Boolean isQrScene;
+
+	/**
+	 * 首次关注的场景值
+	 */
+	private String sceneKey;
 }

+ 5 - 3
netflix-service/src/main/java/com/cyksj/service/gzh/factory/EventMsgService.java

@@ -121,14 +121,14 @@ public class EventMsgService implements GzhMsgService {
 				distributeService.saveDistributionInvitation(sharedId, user.getId());
 			}
 
-			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
+			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid(), sceneKey);
 		} else {
 			//之前关注过
 			if (user.getUnionid() == null || !user.getUnionid().equals(info.getUnionid())) {
 				user.setUnionid(info.getUnionid());
 				userMapper.updateById(user);
 			}
-			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
+			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid(), sceneKey);
 		}
 		//场景值扫描公众号二维码 登录
 		if (StrUtil.isNotEmpty(sceneKey)) {
@@ -137,7 +137,7 @@ public class EventMsgService implements GzhMsgService {
 		return true;
 	}
 
-	private void saveGzhUserRelation(MsgEvent msgEvent, Long userId, String openId, String unionid) {
+	private void saveGzhUserRelation(MsgEvent msgEvent, Long userId, String openId, String unionid,String sceneKey) {
 		WxApp wxApp = wxAppMapper.selectOne(Wrappers.lambdaQuery(WxApp.class)
 				.eq(WxApp::getGzhOriginalId, msgEvent.getToUserName()).last("limit 1"));
 		if (wxApp != null) {
@@ -158,6 +158,8 @@ public class EventMsgService implements GzhMsgService {
 			}
 			try {
 				if (userGzhRelation.getId() == null) {
+					//记录首次关注公众号的场景值
+					userGzhRelation.setSceneKey(sceneKey);
 					userGzhRelationMapper.insert(userGzhRelation);
 					return;
 				}

+ 6 - 1
netflix-service/src/main/java/com/cyksj/service/wechat/WeChatService.java

@@ -74,8 +74,13 @@ public interface WeChatService {
     void refundWxOrder(String appId, String shopId, String transactionId, String outRefundNo, BigDecimal totalFee, BigDecimal refundMoney, String notifyUrl) throws Exception;
 
     /**
-     * 获取公众号带参数的二维码
+     * 获取公众号带参数的临时二维码
      * @return
      */
     WxGzhQrCodeTicketRep getWxGzhQrCode(Long sharedId) throws Exception;
+
+    /**
+     * 获取公众号带参数的永久二维码
+     */
+    String getNotExpiredWxGzhQrCode(String sceneKey) throws Exception;
 }

+ 20 - 0
netflix-service/src/main/java/com/cyksj/service/wechat/impl/WeChatServiceImpl.java

@@ -510,6 +510,26 @@ public class WeChatServiceImpl implements WeChatService {
         return wxGzhQrCodeTicketRep;
     }
 
+    @Override
+    public String getNotExpiredWxGzhQrCode(String sceneKey) throws Exception {
+        String gzhQrCodeUrl = String.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", this.getAccessToken());
+        String body = String.format("{\"action_name\": \"%s\", \"action_info\": {\"scene\": {\"scene_str\": \"%s\"}}}",
+                "QR_LIMIT_STR_SCENE",
+                sceneKey);
+        HttpResponse<String> response = J11HttpC.custom()
+                .url(gzhQrCodeUrl)
+                .ofPost()
+                .body(HttpRequest.BodyPublishers.ofString(body))
+                .send(HttpResponse.BodyHandlers.ofString());
+        if (200 != response.statusCode()) {
+            throw BusinessRuntimeException.getInstance("获取永久渠道二维码失败: " + response.body());
+        }
+        JSONObject re = Jsons.parseObject(response.body(), JSONObject.class);
+        String ticket = re.getStr("ticket");
+        String url = String.format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s", ticket);
+        return url;
+    }
+
     /**
      * sha256_HMAC加密
      * @param message 消息

+ 16 - 4
netflix-web/src/main/java/com/cyksj/web/controller/manage/gzh/GzhController.java

@@ -4,15 +4,15 @@ import cn.hutool.core.util.StrUtil;
 import com.cyksj.common.util.IoKit;
 import com.cyksj.common.util.StringUtil;
 import com.cyksj.common.util.Xmls;
+import com.cyksj.dto.Result;
+import com.cyksj.enums.GatewayResponse;
 import com.cyksj.model.dto.MsgEvent;
 import com.cyksj.service.gzh.GzhService;
+import com.cyksj.service.wechat.WeChatService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
@@ -32,6 +32,8 @@ public class GzhController {
 
 	private final GzhService gzhService;
 
+	private final WeChatService weChatService;
+
 	@RequestMapping(value = "/msg/notify", method = {RequestMethod.GET, RequestMethod.POST})
 	public String msgNotify(@RequestParam(value = "echostr", required = false) String echoStr) throws IOException {
 		if (StrUtil.endWithIgnoreCase(request.getMethod(), RequestMethod.GET.name())) {
@@ -65,4 +67,14 @@ public class GzhController {
 
 		return gzhService.gzhMsg(msgEvent);
 	}
+
+	/**
+	 * 生成永久渠道二维码
+	 */
+	@GetMapping("/get/notExpired/popularizeCode")
+	public Result<String> getNotExpiredPopularizeCode(String sceneKey) throws Exception {
+		//获取gzh带参数的二维码
+		String url = weChatService.getNotExpiredWxGzhQrCode(sceneKey);
+		return GatewayResponse.SUCCESS.newBuilder().toResult(url);
+	}
 }