WeiXinMsgPushImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package com.demo.wjj.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.demo.wjj.bo.SaveDisplaceBo;
  5. import com.demo.wjj.mapper.WeChatMapper;
  6. import com.demo.wjj.mapper.WeiXinMessageMapper;
  7. import com.demo.wjj.po.Agent;
  8. import com.demo.wjj.po.PushRecord;
  9. import com.demo.wjj.po.User;
  10. import com.demo.wjj.po.UserInfo;
  11. import com.demo.wjj.service.CertificationService;
  12. import com.demo.wjj.service.WeChatService;
  13. import com.demo.wjj.service.WeiXinMsgPushService;
  14. import com.demo.wjj.utils.ApiResult;
  15. import com.demo.wjj.utils.HttpResult;
  16. import com.demo.wjj.utils.HttpUtils;
  17. import com.demo.wjj.utils.UuidUtils;
  18. import org.apache.commons.collections4.CollectionUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import java.io.UnsupportedEncodingException;
  26. import java.nio.charset.StandardCharsets;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. @Service
  33. public class WeiXinMsgPushImpl implements WeiXinMsgPushService {
  34. private final Logger LOG = LoggerFactory.getLogger(getClass());
  35. @Autowired
  36. private WeChatService weChatService;
  37. @Autowired
  38. private WeiXinMessageMapper weiXinMessageMapper;
  39. @Value("下载成功提示")
  40. private String displaceAuditTemplateName;
  41. @Value("认证审核提醒")
  42. private String certificationVerifyTemplateName;
  43. @Value("实名认证审核结果通知")
  44. private String certificationResTemplateName;
  45. @Value("${displaceAuditHost}")
  46. private String displaceAuditUrl;
  47. @Autowired
  48. private CertificationService certificationService;
  49. @Autowired
  50. private WeChatMapper weChatMapper;
  51. /*
  52. * 微信小程序推送单个用户
  53. * */
  54. public boolean send(Agent agent, String url, SaveDisplaceBo saveDisplaceBo, String goodsName,String openId) {
  55. LOG.info("chengcm1");
  56. //获取access_token
  57. final WeChatServiceImpl.PlatformAuthorization platformAuthorization = WeChatServiceImpl.PLATFORM_AUTHORIZATION_CACHE.get(agent.getAppId());
  58. if (platformAuthorization == null) {
  59. LOG.info("从缓存中未找到授权方信息, appId:{}", agent.getAppId());
  60. return false;
  61. }
  62. String access_token = platformAuthorization.getAccessToken();
  63. final int tryTimes = 5;
  64. final Map<String, String> template = weChatService.getTemplate(agent.getAgentId(), displaceAuditTemplateName);
  65. if (template == null) {
  66. LOG.info("未查询到置换审核模板id");
  67. return false;
  68. }
  69. final String templateId = template.get("templateId");
  70. // final String templateId = getTemplateId(accessToken, agent.getAppId(), agent.getAppSecret(), tryTimes);
  71. if (StringUtils.isBlank(templateId)) {
  72. LOG.info("未查询到置换审核模板id");
  73. return false;
  74. }
  75. final boolean sendResult = sendMessage(access_token, templateId, agent, tryTimes, url, saveDisplaceBo, goodsName,openId);
  76. LOG.info("退出微信推送置换审核消息, result=> {}", sendResult);
  77. return sendResult;
  78. }
  79. private boolean sendMessage(String accessToken, String templateId, Agent agent, int tryTimes, String url, SaveDisplaceBo saveDisplaceBo, String goodsName,String openId) {
  80. LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, saveDisplaceBo:{}, user:{}, tryTimes:{}", accessToken, templateId, agent, saveDisplaceBo,tryTimes);
  81. final String apiUrlStr = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
  82. final String apiUrl = String.format(apiUrlStr, accessToken);
  83. Map<String, Object> parameters = new HashMap<>();
  84. parameters.put("touser", openId);
  85. parameters.put("template_id", templateId);
  86. // parameters.put("url", url);
  87. Date date = new Date();
  88. String str = "yyyy年MM月dd日 HH:mm";
  89. SimpleDateFormat sdf = new SimpleDateFormat(str);
  90. Map<String, String> miniprogram = new HashMap<>();
  91. // miniprogram.put("appid",agent.getAppId());
  92. miniprogram.put("appid","wx8d16261e538c6e9e");
  93. miniprogram.put("pagepath",url);
  94. Map<String, Map<String, String>> data = new HashMap<>();
  95. final Map<String, String> first = new HashMap<>();
  96. first.put("color", "#173177");
  97. first.put("value", "您好!请点击“详情”领取资料!");
  98. data.put("first", first);
  99. final Map<String, String> keyword1 = new HashMap<>();
  100. keyword1.put("color", "#173177");
  101. keyword1.put("value", goodsName);
  102. data.put("keyword1",keyword1);
  103. final Map<String, String> keyword2 = new HashMap<>();
  104. keyword2.put("color", "#173177");
  105. keyword2.put("value", sdf.format(date));
  106. data.put("keyword2", keyword2);
  107. final Map<String, String> keyword3 = new HashMap<>();
  108. keyword3.put("color", "#173177");
  109. keyword3.put("value", "详图/视频保存手机");
  110. data.put("keyword3", keyword3);
  111. final Map<String, String> remark = new HashMap<>();
  112. remark.put("color", "#173177");
  113. remark.put("value", "感谢您使用。");
  114. data.put("remark", remark);
  115. parameters.put("miniprogram", miniprogram);
  116. parameters.put("data", data);
  117. final HttpResult httpResult = HttpUtils.post(apiUrl, JSON.toJSONString(parameters).getBytes(StandardCharsets.UTF_8));
  118. if (httpResult.getStatusCode() != 200) {
  119. LOG.info("请求微信发送置换审核消息失败, statusCode:{}", httpResult.getStatusCode());
  120. return false;
  121. }
  122. final String content = httpResult.getContent();
  123. if (StringUtils.isBlank(content)) {
  124. LOG.info("请求微信发送置换审核消息, 响应内容为空");
  125. return false;
  126. }
  127. final JSONObject jsonObject = JSON.parseObject(content);
  128. final Integer errcode = jsonObject.getInteger("errcode");
  129. if (errcode != null && errcode == 42001 && tryTimes > 0) {
  130. LOG.info("推送微信模板消息失败, accessToken已过期. 重新获取accessToken. tryTimes:{}", tryTimes);
  131. return false;
  132. //return sendMessage(tokenManage.requestAccessToken(agent.getAppId(), agent.getAppSecret()), templateId, agent, sale, saveDisplaceBo, tryTimes - 1);
  133. } else if (errcode != 0) {
  134. LOG.info("请求微信发送模板消息, 推送失败. errcode:{}", errcode);
  135. return false;
  136. }
  137. LOG.info("退出微信消息推送");
  138. return true;
  139. }
  140. /*
  141. * 实名认证审核
  142. * */
  143. public boolean sendVerifyUserInfo(UserInfo userInfo,
  144. Agent agent,
  145. String url,String verifyOpenId,String appId) {
  146. LOG.info("chengcm1");
  147. //获取access_token
  148. final WeChatServiceImpl.PlatformAuthorization platformAuthorization = WeChatServiceImpl.PLATFORM_AUTHORIZATION_CACHE.get(agent.getAppId());
  149. if (platformAuthorization == null) {
  150. LOG.info("从缓存中未找到授权方信息, appId:{}", agent.getAppId());
  151. return false;
  152. }
  153. String access_token = platformAuthorization.getAccessToken();
  154. final int tryTimes = 5;
  155. final Map<String, String> template = weChatService.getTemplate(agent.getAgentId(), certificationVerifyTemplateName);
  156. if (template == null) {
  157. LOG.info("未查询到置换审核模板id");
  158. return false;
  159. }
  160. final String templateId = template.get("templateId");
  161. // final String templateId = getTemplateId(accessToken, agent.getAppId(), agent.getAppSecret(), tryTimes);
  162. if (StringUtils.isBlank(templateId)) {
  163. LOG.info("未查询到置换审核模板id");
  164. return false;
  165. }
  166. final List<Map<String, Object>> userList = weChatMapper.selectPushMessageUsersC(agent.getAgentId());
  167. if (userList == null || userList.isEmpty()) {
  168. LOG.info("未查询到该商家的管理员信息. agentId:{}", agent.getAgentId());
  169. return false;
  170. }
  171. //判读用户是否已有实名认证信息
  172. UserInfo userInfo1=certificationService.getUserInfo(userInfo.getIdcard(),null);
  173. if(userInfo1!=null){
  174. certificationService.deleteUserInfo(userInfo.getIdcard());
  175. }
  176. //保存待审核记录
  177. // UserInfo info=new UserInfo();
  178. // info.setUsername(userInfo.getUsername());
  179. // info.setSex(userInfo.getSex());
  180. // info.setIdcard(userInfo.getIdcard());
  181. // info.setBirthday(userInfo.getBirthday());
  182. // info.setProvince(userInfo.getProvince());
  183. // info.setCity(userInfo.getCity());
  184. // info.setPrefecture(userInfo.getPrefecture());
  185. // info.setArea(userInfo.getArea());
  186. // info.setAddrcode(userInfo.getAddrcode());
  187. // info.setAccountno(userInfo.getAccountno());
  188. // info.setMobile(userInfo.getMobile());
  189. // info.setBank(userInfo.getBank());
  190. // info.setCardname(userInfo.getCardname());
  191. // info.setCardtype(userInfo.getCardtype());
  192. // info.setStatus("1");
  193. // info.setUseropenid(userInfo.getUseropenid());
  194. // info.setIdcarddfrontpic(userInfo.getIdcarddfrontpic());
  195. // info.setIdcarddbackpic(userInfo.getIdcarddbackpic());
  196. // info.setCardvalidity(userInfo.getCardvalidity());
  197. // info.setRole(userInfo.getRole());
  198. // info.setPic(userInfo.getPic());
  199. // info.setBankcardpic(userInfo.getBankcardpic());
  200. //
  201. userInfo.setStatus("1");
  202. LOG.info("退出微信消息推送");
  203. ApiResult apiResult = certificationService.saveUserInfo(userInfo);
  204. boolean result = false;
  205. for (Map<String, Object> user : userList) {
  206. final String wxh = (String) user.get("certification_wxh");
  207. if (StringUtils.isBlank(wxh)) {
  208. LOG.info("用户微信号为空, id:{}", user.get("id"));
  209. }
  210. // 推送给微工程用户
  211. final boolean sendResult = sendVerifyUserInfoMessage(access_token,
  212. templateId,
  213. agent,
  214. tryTimes,
  215. wxh,
  216. url,
  217. userInfo,appId);
  218. if (sendResult && !result) {
  219. result = true;
  220. }
  221. }
  222. LOG.info("退出微信推送置换审核消息, result=> {}", result);
  223. return result;
  224. }
  225. private boolean sendVerifyUserInfoMessage(String accessToken,
  226. String templateId,
  227. Agent agent,
  228. int tryTimes,
  229. String verifyOpenId,
  230. String url,
  231. UserInfo userInfo,
  232. String appId) {
  233. LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, user:{}, tryTimes:{}", accessToken, templateId, agent,tryTimes);
  234. final String apiUrlStr = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
  235. final String apiUrl = String.format(apiUrlStr, accessToken);
  236. Map<String, Object> parameters = new HashMap<>();
  237. parameters.put("touser", verifyOpenId);
  238. parameters.put("template_id", templateId);
  239. parameters.put("url", url);
  240. // parameters.put("url", url);
  241. Date date = new Date();
  242. String str = "yyyy年MM月dd日 HH:mm";
  243. SimpleDateFormat sdf = new SimpleDateFormat(str);
  244. // Map<String, String> miniprogram = new HashMap<>();
  245. //// miniprogram.put("appid",agent.getAppId());
  246. // miniprogram.put("appid",appId);
  247. // miniprogram.put("pagepath",url);
  248. Map<String, Map<String, String>> data = new HashMap<>();
  249. final Map<String, String> first = new HashMap<>();
  250. first.put("color", "#173177");
  251. first.put("value", "实名认证审核");
  252. data.put("first", first);
  253. final Map<String, String> keyword1 = new HashMap<>();
  254. keyword1.put("color", "#173177");
  255. keyword1.put("value", userInfo.getUsername());
  256. data.put("keyword1",keyword1);
  257. final Map<String, String> keyword2 = new HashMap<>();
  258. keyword2.put("color", "#173177");
  259. keyword2.put("value", sdf.format(date));
  260. data.put("keyword2", keyword2);
  261. final Map<String, String> remark = new HashMap<>();
  262. remark.put("color", "#173177");
  263. remark.put("value", "请尽快处理!");
  264. data.put("remark", remark);
  265. // parameters.put("miniprogram", miniprogram);
  266. parameters.put("data", data);
  267. //获取实名认证审核岗
  268. final HttpResult httpResult = HttpUtils.post(apiUrl, JSON.toJSONString(parameters).getBytes(StandardCharsets.UTF_8));
  269. if (httpResult.getStatusCode() != 200) {
  270. LOG.info("请求微信发送实名认证审核消息失败, statusCode:{}", httpResult.getStatusCode());
  271. return false;
  272. }
  273. final String content = httpResult.getContent();
  274. if (StringUtils.isBlank(content)) {
  275. LOG.info("请求微信发送实名认证审核消息, 响应内容为空");
  276. return false;
  277. }
  278. final JSONObject jsonObject = JSON.parseObject(content);
  279. final Integer errcode = jsonObject.getInteger("errcode");
  280. if (errcode != null && errcode == 42001 && tryTimes > 0) {
  281. LOG.info("推送微信模板消息失败, accessToken已过期. 重新获取accessToken. tryTimes:{}", tryTimes);
  282. return false;
  283. //return sendMessage(tokenManage.requestAccessToken(agent.getAppId(), agent.getAppSecret()), templateId, agent, sale, saveDisplaceBo, tryTimes - 1);
  284. } else if (errcode != 0) {
  285. LOG.info("请求微信发送模板消息, 推送失败. errcode:{}", errcode);
  286. return false;
  287. }
  288. return true;
  289. }
  290. /*
  291. * 返回审核结果给用户
  292. * */
  293. public boolean verificationSend(String openId,String reviewComments,String status,Agent agent,String idCard,String appId) {
  294. LOG.info("chengcm1");
  295. //获取access_token
  296. final WeChatServiceImpl.PlatformAuthorization platformAuthorization = WeChatServiceImpl.PLATFORM_AUTHORIZATION_CACHE.get(agent.getAppId());
  297. if (platformAuthorization == null) {
  298. LOG.info("从缓存中未找到授权方信息, appId:{}", agent.getAppId());
  299. return false;
  300. }
  301. String access_token = platformAuthorization.getAccessToken();
  302. final int tryTimes = 5;
  303. final Map<String, String> template = weChatService.getTemplate(agent.getAgentId(), certificationResTemplateName);
  304. if (template == null) {
  305. LOG.info("未查询到置换审核模板id");
  306. return false;
  307. }
  308. final String templateId = template.get("templateId");
  309. // final String templateId = getTemplateId(accessToken, agent.getAppId(), agent.getAppSecret(), tryTimes);
  310. if (StringUtils.isBlank(templateId)) {
  311. LOG.info("未查询到置换审核模板id");
  312. return false;
  313. }
  314. final boolean sendResult = verificationSendMessage(access_token, templateId, agent, tryTimes,openId, reviewComments, status,idCard,appId);
  315. LOG.info("退出微信推送置换审核消息, result=> {}", sendResult);
  316. return sendResult;
  317. }
  318. private boolean verificationSendMessage(String accessToken, String templateId, Agent agent, int tryTimes,String openId,String reviewComments,String status,String idCard,String appId) {
  319. LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, user:{}, tryTimes:{}", accessToken, templateId, agent,tryTimes);
  320. final String apiUrlStr = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
  321. final String apiUrl = String.format(apiUrlStr, accessToken);
  322. Map<String, Object> parameters = new HashMap<>();
  323. parameters.put("touser", openId);
  324. parameters.put("template_id", templateId);
  325. // parameters.put("url", url);
  326. Date date = new Date();
  327. String str = "yyyy年MM月dd日 HH:mm";
  328. SimpleDateFormat sdf = new SimpleDateFormat(str);
  329. Map<String, String> miniprogram = new HashMap<>();
  330. // miniprogram.put("appid",agent.getAppId());
  331. miniprogram.put("appid",appId);
  332. // miniprogram.put("pagepath",url);
  333. Map<String, Map<String, String>> data = new HashMap<>();
  334. final Map<String, String> first = new HashMap<>();
  335. first.put("color", "#173177");
  336. first.put("value", "感谢您进行实名认证!");
  337. data.put("first", first);
  338. String statusName="审核通过";
  339. if("2".equals(status)){
  340. statusName="审核不通过";
  341. }
  342. final Map<String, String> keyword1 = new HashMap<>();
  343. keyword1.put("color", "#173177");
  344. keyword1.put("value", statusName);
  345. data.put("keyword1",keyword1);
  346. final Map<String, String> keyword2 = new HashMap<>();
  347. keyword2.put("color", "#173177");
  348. keyword2.put("value", sdf.format(date));
  349. data.put("keyword2", keyword2);
  350. final Map<String, String> remark = new HashMap<>();
  351. remark.put("color", "#173177");
  352. remark.put("value", reviewComments);
  353. data.put("remark", remark);
  354. parameters.put("data", data);
  355. final HttpResult httpResult = HttpUtils.post(apiUrl, JSON.toJSONString(parameters).getBytes(StandardCharsets.UTF_8));
  356. if (httpResult.getStatusCode() != 200) {
  357. LOG.info("请求微信发送实名认证审核消息失败, statusCode:{}", httpResult.getStatusCode());
  358. return false;
  359. }
  360. final String content = httpResult.getContent();
  361. if (StringUtils.isBlank(content)) {
  362. LOG.info("请求微信发送实名认证审核消息, 响应内容为空");
  363. return false;
  364. }
  365. final JSONObject jsonObject = JSON.parseObject(content);
  366. final Integer errcode = jsonObject.getInteger("errcode");
  367. if (errcode != null && errcode == 42001 && tryTimes > 0) {
  368. LOG.info("推送微信模板消息失败, accessToken已过期. 重新获取accessToken. tryTimes:{}", tryTimes);
  369. return false;
  370. //return sendMessage(tokenManage.requestAccessToken(agent.getAppId(), agent.getAppSecret()), templateId, agent, sale, saveDisplaceBo, tryTimes - 1);
  371. } else if (errcode != 0) {
  372. LOG.info("请求微信发送模板消息, 推送失败. errcode:{}", errcode);
  373. return false;
  374. }
  375. //模板消息推送成功后更新用户实名认证状态
  376. if("1".equals(status)) {
  377. certificationService.updateUserInfoByIdCard("2", idCard);
  378. }else{
  379. certificationService.updateUserInfoByIdCard("3", idCard);
  380. }
  381. return true;
  382. }
  383. }