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.WeChatMapper; 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.po.UserInfo; import com.demo.wjj.service.CertificationService; import com.demo.wjj.service.WeChatService; import com.demo.wjj.service.WeiXinMsgPushService; import com.demo.wjj.utils.ApiResult; 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.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; 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("下载成功提示") private String displaceAuditTemplateName; @Value("认证审核提醒") private String certificationVerifyTemplateName; @Value("实名认证审核结果通知") private String certificationResTemplateName; @Value("${displaceAuditHost}") private String displaceAuditUrl; @Autowired private CertificationService certificationService; @Autowired private WeChatMapper weChatMapper; /* * 微信小程序推送单个用户 * */ public boolean send(Agent agent, String url, SaveDisplaceBo saveDisplaceBo, String goodsName,String openId) { LOG.info("chengcm1"); //获取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 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,openId); LOG.info("退出微信推送置换审核消息, result=> {}", sendResult); return sendResult; } private boolean sendMessage(String accessToken, String templateId, Agent agent, int tryTimes, String url, SaveDisplaceBo saveDisplaceBo, String goodsName,String openId) { LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, saveDisplaceBo:{}, user:{}, tryTimes:{}", accessToken, templateId, agent, saveDisplaceBo,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 parameters = new HashMap<>(); parameters.put("touser", openId); parameters.put("template_id", templateId); // parameters.put("url", url); Date date = new Date(); String str = "yyyy年MM月dd日 HH:mm"; SimpleDateFormat sdf = new SimpleDateFormat(str); Map miniprogram = new HashMap<>(); // miniprogram.put("appid",agent.getAppId()); miniprogram.put("appid","wx8d16261e538c6e9e"); miniprogram.put("pagepath",url); Map> data = new HashMap<>(); final Map first = new HashMap<>(); first.put("color", "#173177"); first.put("value", "您好!请点击“详情”领取资料!"); data.put("first", first); final Map keyword1 = new HashMap<>(); keyword1.put("color", "#173177"); keyword1.put("value", goodsName); data.put("keyword1",keyword1); final Map keyword2 = new HashMap<>(); keyword2.put("color", "#173177"); keyword2.put("value", sdf.format(date)); data.put("keyword2", keyword2); final Map keyword3 = new HashMap<>(); keyword3.put("color", "#173177"); keyword3.put("value", "详图/视频保存手机"); data.put("keyword3", keyword3); final Map remark = new HashMap<>(); remark.put("color", "#173177"); remark.put("value", "感谢您使用。"); data.put("remark", remark); parameters.put("miniprogram", miniprogram); 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; } LOG.info("退出微信消息推送"); return true; } /* * 实名认证审核 * */ public boolean sendVerifyUserInfo(UserInfo userInfo, Agent agent, String url,String verifyOpenId,String appId) { LOG.info("chengcm1"); //获取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 template = weChatService.getTemplate(agent.getAgentId(), certificationVerifyTemplateName); 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 List> userList = weChatMapper.selectPushMessageUsersC(agent.getAgentId()); if (userList == null || userList.isEmpty()) { LOG.info("未查询到该商家的管理员信息. agentId:{}", agent.getAgentId()); return false; } //判读用户是否已有实名认证信息 UserInfo userInfo1=certificationService.getUserInfo(userInfo.getIdcard(),null); if(userInfo1!=null){ certificationService.deleteUserInfo(userInfo.getIdcard()); } //保存待审核记录 // UserInfo info=new UserInfo(); // info.setUsername(userInfo.getUsername()); // info.setSex(userInfo.getSex()); // info.setIdcard(userInfo.getIdcard()); // info.setBirthday(userInfo.getBirthday()); // info.setProvince(userInfo.getProvince()); // info.setCity(userInfo.getCity()); // info.setPrefecture(userInfo.getPrefecture()); // info.setArea(userInfo.getArea()); // info.setAddrcode(userInfo.getAddrcode()); // info.setAccountno(userInfo.getAccountno()); // info.setMobile(userInfo.getMobile()); // info.setBank(userInfo.getBank()); // info.setCardname(userInfo.getCardname()); // info.setCardtype(userInfo.getCardtype()); // info.setStatus("1"); // info.setUseropenid(userInfo.getUseropenid()); // info.setIdcarddfrontpic(userInfo.getIdcarddfrontpic()); // info.setIdcarddbackpic(userInfo.getIdcarddbackpic()); // info.setCardvalidity(userInfo.getCardvalidity()); // info.setRole(userInfo.getRole()); // info.setPic(userInfo.getPic()); // info.setBankcardpic(userInfo.getBankcardpic()); // userInfo.setStatus("1"); LOG.info("退出微信消息推送"); ApiResult apiResult = certificationService.saveUserInfo(userInfo); boolean result = false; for (Map user : userList) { final String wxh = (String) user.get("certification_wxh"); if (StringUtils.isBlank(wxh)) { LOG.info("用户微信号为空, id:{}", user.get("id")); } // 推送给微工程用户 final boolean sendResult = sendVerifyUserInfoMessage(access_token, templateId, agent, tryTimes, wxh, url, userInfo,appId); if (sendResult && !result) { result = true; } } LOG.info("退出微信推送置换审核消息, result=> {}", result); return result; } private boolean sendVerifyUserInfoMessage(String accessToken, String templateId, Agent agent, int tryTimes, String verifyOpenId, String url, UserInfo userInfo, String appId) { LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, 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 parameters = new HashMap<>(); parameters.put("touser", verifyOpenId); parameters.put("template_id", templateId); parameters.put("url", url); // parameters.put("url", url); Date date = new Date(); String str = "yyyy年MM月dd日 HH:mm"; SimpleDateFormat sdf = new SimpleDateFormat(str); // Map miniprogram = new HashMap<>(); //// miniprogram.put("appid",agent.getAppId()); // miniprogram.put("appid",appId); // miniprogram.put("pagepath",url); Map> data = new HashMap<>(); final Map first = new HashMap<>(); first.put("color", "#173177"); first.put("value", "实名认证审核"); data.put("first", first); final Map keyword1 = new HashMap<>(); keyword1.put("color", "#173177"); keyword1.put("value", userInfo.getUsername()); data.put("keyword1",keyword1); final Map keyword2 = new HashMap<>(); keyword2.put("color", "#173177"); keyword2.put("value", sdf.format(date)); data.put("keyword2", keyword2); final Map remark = new HashMap<>(); remark.put("color", "#173177"); remark.put("value", "请尽快处理!"); data.put("remark", remark); // parameters.put("miniprogram", miniprogram); 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; } return true; } /* * 返回审核结果给用户 * */ public boolean verificationSend(String openId,String reviewComments,String status,Agent agent,String idCard,String appId) { LOG.info("chengcm1"); //获取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 template = weChatService.getTemplate(agent.getAgentId(), certificationResTemplateName); 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 = verificationSendMessage(access_token, templateId, agent, tryTimes,openId, reviewComments, status,idCard,appId); LOG.info("退出微信推送置换审核消息, result=> {}", sendResult); return sendResult; } private boolean verificationSendMessage(String accessToken, String templateId, Agent agent, int tryTimes,String openId,String reviewComments,String status,String idCard,String appId) { LOG.info("进入微信消息推送, accessToken:{}, templateId:{}, agent:{}, 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 parameters = new HashMap<>(); parameters.put("touser", openId); parameters.put("template_id", templateId); // parameters.put("url", url); Date date = new Date(); String str = "yyyy年MM月dd日 HH:mm"; SimpleDateFormat sdf = new SimpleDateFormat(str); Map miniprogram = new HashMap<>(); // miniprogram.put("appid",agent.getAppId()); miniprogram.put("appid",appId); // miniprogram.put("pagepath",url); Map> data = new HashMap<>(); final Map first = new HashMap<>(); first.put("color", "#173177"); first.put("value", "感谢您进行实名认证!"); data.put("first", first); String statusName="审核通过"; if("2".equals(status)){ statusName="审核不通过"; } final Map keyword1 = new HashMap<>(); keyword1.put("color", "#173177"); keyword1.put("value", statusName); data.put("keyword1",keyword1); final Map keyword2 = new HashMap<>(); keyword2.put("color", "#173177"); keyword2.put("value", sdf.format(date)); data.put("keyword2", keyword2); final Map remark = new HashMap<>(); remark.put("color", "#173177"); remark.put("value", reviewComments); data.put("remark", remark); 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; } //模板消息推送成功后更新用户实名认证状态 if("1".equals(status)) { certificationService.updateUserInfoByIdCard("2", idCard); }else{ certificationService.updateUserInfoByIdCard("3", idCard); } return true; } }