|
@@ -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);
|
|
|
|
+ }
|
|
|
|
+}
|