123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- 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<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,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<String, Object> 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<String, String> miniprogram = new HashMap<>();
- // miniprogram.put("appid",agent.getAppId());
- miniprogram.put("appid","wx8d16261e538c6e9e");
- miniprogram.put("pagepath",url);
- 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", goodsName);
- data.put("keyword1",keyword1);
- final Map<String, String> keyword2 = new HashMap<>();
- keyword2.put("color", "#173177");
- keyword2.put("value", sdf.format(date));
- data.put("keyword2", keyword2);
- final Map<String, String> keyword3 = new HashMap<>();
- keyword3.put("color", "#173177");
- keyword3.put("value", "详图/视频保存手机");
- data.put("keyword3", keyword3);
- final Map<String, String> 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<String, String> 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<Map<String, Object>> 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<String, Object> 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<String, Object> 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<String, String> miniprogram = new HashMap<>();
- //// miniprogram.put("appid",agent.getAppId());
- // miniprogram.put("appid",appId);
- // miniprogram.put("pagepath",url);
- 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", userInfo.getUsername());
- data.put("keyword1",keyword1);
- final Map<String, String> keyword2 = new HashMap<>();
- keyword2.put("color", "#173177");
- keyword2.put("value", sdf.format(date));
- data.put("keyword2", keyword2);
- final Map<String, String> 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<String, String> 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<String, Object> 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<String, String> miniprogram = new HashMap<>();
- // miniprogram.put("appid",agent.getAppId());
- miniprogram.put("appid",appId);
- // miniprogram.put("pagepath",url);
- 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);
- String statusName="审核通过";
- if("2".equals(status)){
- statusName="审核不通过";
- }
- final Map<String, String> keyword1 = new HashMap<>();
- keyword1.put("color", "#173177");
- keyword1.put("value", statusName);
- data.put("keyword1",keyword1);
- final Map<String, String> keyword2 = new HashMap<>();
- keyword2.put("color", "#173177");
- keyword2.put("value", sdf.format(date));
- data.put("keyword2", keyword2);
- final Map<String, String> 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;
- }
- }
|