Browse Source

重定向url

chengcm 4 years ago
parent
commit
da89ef211b

+ 51 - 4
wjj-api/src/main/java/com/demo/wjj/controller/WeChatController.java

@@ -1,13 +1,12 @@
 package com.demo.wjj.controller;
 
-import com.demo.wjj.bo.AuthorizationUrlBo;
-import com.demo.wjj.bo.CallbackTicketBo;
-import com.demo.wjj.bo.DisplaceDetailBo;
-import com.demo.wjj.bo.SaveDisplaceBo;
+import com.demo.wjj.bo.*;
 import com.demo.wjj.po.Agent;
 import com.demo.wjj.po.Sale;
 import com.demo.wjj.service.*;
 import com.demo.wjj.utils.ApiResult;
+import com.demo.wjj.utils.ExecuteResult;
+import com.demo.wjj.utils.Result;
 import com.demo.wjj.utils.wechat.AesException;
 import org.apache.commons.lang3.StringUtils;
 import org.dom4j.DocumentException;
@@ -43,6 +42,9 @@ public class WeChatController {
     private SaleService saleService;
 
     @Autowired
+    private WeiXinMsgPushService weiXinMsgPushService;
+
+    @Autowired
     private DiggerAgentDisplaceManageService diggerAgentDisplaceManageService;
 
     private String appId = "wx94660c86d38f6bc3";
@@ -409,4 +411,49 @@ public class WeChatController {
             return ApiResult.createFailure();
         }
     }
+
+    @PostMapping("/redirectUrl")
+    public ApiResult redirectUrl(@RequestParam String agentId,
+                                 @RequestParam String id,
+                                 @RequestParam String openId,
+                                 @RequestParam String displaceId,
+                                 @RequestParam String goodsName) {
+        LOG.info("调用获取第三方平台token(/wechat/getToken)接口, appId:{}", appId);
+        final Agent agent = agentService.getAgent(agentId);
+        if (agent == null) {
+            final ApiResult apiResult = ApiResult.createFailure();
+            apiResult.setMessage("未查询到商家信息");
+            return apiResult;
+        }
+        final String appId = agent.getMchAppId();
+        if (StringUtils.isBlank(appId)) {
+            final ApiResult apiResult = ApiResult.createFailure();
+            apiResult.setMessage("商家appId为空");
+            return apiResult;
+        }
+        final DisplaceDetailBo displaceDetailBo = diggerAgentDisplaceManageService.queryDetails(displaceId);
+        if (displaceDetailBo == null) {
+            final ApiResult apiResult = ApiResult.createFailure();
+            apiResult.setMessage("未查询到置换设备信息");
+            return apiResult;
+        }
+
+        SaveDisplaceBo saveDisplaceBo = new SaveDisplaceBo();
+        saveDisplaceBo.setBrand(displaceDetailBo.getBrand());
+        saveDisplaceBo.setModel(displaceDetailBo.getModel());
+        saveDisplaceBo.setDeviceType(displaceDetailBo.getDeviceType());
+        //拼装参数
+        String redirectUrl="/pages/download/index?agentId="+agentId+"&id="+id+"&openId="+openId;
+//        weiXinMsgPushService.send(agent,redirectUrl,displaceId,goodsName);
+//        ApiResult apiResult;
+        try {
+            final boolean result = weiXinMsgPushService.send(agent,redirectUrl,saveDisplaceBo,goodsName);
+            final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
+            LOG.info("调用发送置换设备审核推送消息(/wechat/redirectUrl)接口成功, apiResult:{}", apiResult);
+            return apiResult;
+        } catch (Exception e) {
+            LOG.error("调用发送置换设备审核推送消息(/wechat/redirectUrl)接口异常", e);
+            return ApiResult.createFailure();
+        }
+    }
 }

+ 11 - 0
wjj-core/src/main/java/com/demo/wjj/service/WeiXinMsgPushService.java

@@ -0,0 +1,11 @@
+package com.demo.wjj.service;
+
+
+import com.demo.wjj.bo.SaveDisplaceBo;
+import com.demo.wjj.po.Agent;
+import com.demo.wjj.po.Sale;
+
+public interface WeiXinMsgPushService {
+
+    boolean send(Agent agent, String url, SaveDisplaceBo saveDisplaceBo, String goodsName);
+}

+ 178 - 0
wjj-core/src/main/java/com/demo/wjj/service/impl/WeiXinMsgPushImpl.java

@@ -0,0 +1,178 @@
+package com.demo.wjj.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.demo.wjj.bo.SaveDisplaceBo;
+import com.demo.wjj.mapper.WeiXinMessageMapper;
+import com.demo.wjj.po.Agent;
+import com.demo.wjj.po.PushRecord;
+import com.demo.wjj.po.User;
+import com.demo.wjj.service.WeChatService;
+import com.demo.wjj.service.WeiXinMsgPushService;
+import com.demo.wjj.utils.HttpResult;
+import com.demo.wjj.utils.HttpUtils;
+import com.demo.wjj.utils.UuidUtils;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+@Service
+public class WeiXinMsgPushImpl implements WeiXinMsgPushService {
+
+    private final Logger LOG = LoggerFactory.getLogger(getClass());
+
+    @Autowired
+    private WeChatService weChatService;
+
+    @Autowired
+    private WeiXinMessageMapper weiXinMessageMapper;
+
+    @Value("${displaceAuditTemplateName}")
+    private String displaceAuditTemplateName;
+
+    @Value("${displaceAuditHost}")
+    private String displaceAuditUrl;
+
+    /*
+    * 微信小程序推送单个用户
+    * */
+    public boolean send(Agent agent, String url, SaveDisplaceBo saveDisplaceBo, String goodsName) {
+
+
+        //获取access_token
+
+        final WeChatServiceImpl.PlatformAuthorization platformAuthorization = WeChatServiceImpl.PLATFORM_AUTHORIZATION_CACHE.get(agent.getAppId());
+        if (platformAuthorization == null) {
+            LOG.info("从缓存中未找到授权方信息, appId:{}", agent.getAppId());
+            return false;
+        }
+
+        String access_token = platformAuthorization.getAccessToken();
+
+        final int tryTimes = 5;
+        final Map<String, String> template = weChatService.getTemplate(agent.getAgentId(), displaceAuditTemplateName);
+        if (template == null) {
+            LOG.info("未查询到置换审核模板id");
+            return false;
+        }
+        final String templateId = template.get("templateId");
+//        final String templateId = getTemplateId(accessToken, agent.getAppId(), agent.getAppSecret(), tryTimes);
+        if (StringUtils.isBlank(templateId)) {
+            LOG.info("未查询到置换审核模板id");
+            return false;
+        }
+
+        final boolean sendResult = sendMessage(access_token, templateId, agent, tryTimes, url, saveDisplaceBo, goodsName);
+
+
+        LOG.info("退出微信推送置换审核消息, result=> {}", sendResult);
+        return sendResult;
+    }
+
+
+    private boolean sendMessage(String accessToken, String templateId, Agent agent, int tryTimes, String url, SaveDisplaceBo saveDisplaceBo, String goodsName) {
+        LOG.info("进入推送置换审核微信消息, accessToken:{}, templateId:{}, agent:{}", accessToken, templateId, agent);
+
+        final List<User> users = weiXinMessageMapper.selectAgentOpenId(agent.getAgentId());
+        if (CollectionUtils.isEmpty(users)) {
+            LOG.info("未查询到商家对应审核人员");
+            return false;
+        }
+
+        for (User user : users) {
+            internalSend(accessToken, templateId, agent, tryTimes, url, saveDisplaceBo, goodsName, user);
+        }
+
+        LOG.info("退出推送置换审核微信消息");
+        return true;
+    }
+
+    private Boolean internalSend(String accessToken, String templateId, Agent agent, int tryTimes, String url, SaveDisplaceBo saveDisplaceBo, String goodsName, User user) {
+        LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, sale:{}, saveDisplaceBo:{}, user:{}, tryTimes:{}", accessToken, templateId, agent, tryTimes);
+        final String apiUrlStr = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
+        final String apiUrl = String.format(apiUrlStr, accessToken);
+        Map<String, Object> parameters = new HashMap<>();
+        parameters.put("touser", user.getUserWx());
+        parameters.put("template_id", templateId);
+        parameters.put("url", String.format("%s/wx/wxApplogin/%s.mp", displaceAuditUrl, agent.getAgentId()));
+
+        Map<String, Map<String, String>> data = new HashMap<>();
+        final Map<String, String> first = new HashMap<>();
+        first.put("color", "#173177");
+        first.put("value", "详图视频任你下载");
+        data.put("first", first);
+
+        final Map<String, String> keyword1 = new HashMap<>();
+        keyword1.put("color", "#173177");
+        keyword1.put("value", saveDisplaceBo.getDisplaceId());
+        data.put("keyword1",keyword1);
+
+        final Map<String, String> keyword2 = new HashMap<>();
+        keyword2.put("color", "#173177");
+        keyword2.put("value", goodsName);
+        data.put("keyword2", keyword2);
+
+        final Map<String, String> remark = new HashMap<>();
+        remark.put("color", "#173177");
+        remark.put("value", url);
+        data.put("remark", remark);
+        parameters.put("data", data);
+        parameters.put("data", data);
+
+        final HttpResult httpResult = HttpUtils.post(apiUrl, JSON.toJSONString(parameters).getBytes(StandardCharsets.UTF_8));
+
+        if (httpResult.getStatusCode() != 200) {
+            LOG.info("请求微信发送置换审核消息失败, statusCode:{}", httpResult.getStatusCode());
+            return false;
+        }
+
+        final String content = httpResult.getContent();
+        if (StringUtils.isBlank(content)) {
+            LOG.info("请求微信发送置换审核消息, 响应内容为空");
+            return false;
+        }
+
+        final JSONObject jsonObject = JSON.parseObject(content);
+        final Integer errcode = jsonObject.getInteger("errcode");
+        if (errcode != null && errcode == 42001 && tryTimes > 0) {
+            LOG.info("推送微信模板消息失败, accessToken已过期. 重新获取accessToken. tryTimes:{}", tryTimes);
+            return false;
+            //return sendMessage(tokenManage.requestAccessToken(agent.getAppId(), agent.getAppSecret()), templateId, agent, sale, saveDisplaceBo, tryTimes - 1);
+        } else if (errcode != 0) {
+            LOG.info("请求微信发送模板消息, 推送失败. errcode:{}", errcode);
+            return false;
+        }
+
+        savePushRecord(agent, saveDisplaceBo, user);
+        LOG.info("退出微信消息推送");
+        return true;
+    }
+
+    private void savePushRecord(Agent agent, SaveDisplaceBo saveDisplaceBo, User user) {
+        PushRecord pushRecord = new PushRecord();
+        pushRecord.setId(UuidUtils.generate());
+        pushRecord.setAgentId(agent.getAgentId());
+        pushRecord.setAgentName(agent.getAgentName());
+        pushRecord.setDisplaceId(saveDisplaceBo.getDisplaceId());
+        pushRecord.setMenuName("zhsh");
+        pushRecord.setMsgType("sbsc");
+        pushRecord.setReceiveWxh(user.getUserWx());
+        pushRecord.setReceiveWxnc(user.getUserWxnc());
+        pushRecord.setPushTime(new Date());
+        pushRecord.setPushStatus("1");
+        pushRecord.setReferId(saveDisplaceBo.getDisplaceId());
+        pushRecord.setPushType("1");
+        pushRecord.setReceiveName(user.getName());
+        pushRecord.setReceivePhone(user.getPhone());
+        weiXinMessageMapper.insertPushRecord(pushRecord);
+    }
+}