package com.caimei.modules.order.service; import com.alibaba.fastjson.JSONObject; import com.caimei.modules.utils.RequestUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Map; /** * Description * * @author : Charles * @date : 2022/4/12 */ @Service public class WeChatService { @Value("${wx.crm-app-id}") private String crmAppId; @Value("${wx.crm-app-secret}") private String crmAppSecret; protected Logger logger = LoggerFactory.getLogger(getClass()); /** * 微信公众号获取access_token * * @return access_token */ public String getAccessToken() throws Exception { String link = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; link = link.replace("APPID", "wx91c4152b60ca91a3"); link = link.replace("APPSECRET", "a563dd2c07c9c815a4e697c8b6cb73dc"); String result = RequestUtil.sendGet(link); logger.info("微信公众号获取access_token>>>" + result); Map map = JSONObject.parseObject(result, Map.class); String access_token = (String) map.get("access_token"); return access_token; } /** * 发货消息推送 * @param accessToken * @param openid * @param shopOrderNo 子订单编号 * @param time 发货时间 * @param company 快递名称 * @param companyno 快递编号 * @param pagePath 跳转链接 */ public void sendTemplateMsgy(String accessToken, String openid, String shopOrderNo,String time, String company, String companyno, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "尊敬的采美客户,您购买的订单已经发货啦~"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", shopOrderNo); JSONObject keyword2 = new JSONObject(); keyword2.put("value", time); JSONObject keyword3 = new JSONObject(); keyword3.put("value", company); JSONObject keyword4 = new JSONObject(); keyword4.put("value", companyno); JSONObject remark = new JSONObject(); remark.put("value", "收到货后请记得确认收货哟~"); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "_Zc6WiUp3hM4yBgT30HkTyA-SmcCN-sjnGpIa6n3rKI"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } /** * 资质机构认证成功消息推送 * @param accessToken * @param openid * @param pagePath 跳转链接 */ public void sendTemplateMsg(String accessToken, String openid ,String auditTime, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "认证成功通知"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", "恭喜您成功成为采美资质机构"); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "资质机构"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", "审核通过"); JSONObject keyword4 = new JSONObject(); keyword4.put("value", auditTime); JSONObject remark = new JSONObject(); remark.put("value", "认证成功后,您将享受到更多的优惠机会。"); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "kfzhVTJ6sNOJSgwk8x7Z9_m8S2LRzDfAGtiUgGyO8Zk"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } /** * 资质机构认证失败消息推送 * @param accessToken * @param openid * @param pagePath 跳转链接 */ public void sendTemplateMessg(String accessToken, String openid, String auditResult,String auditTime, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "认证失败通知"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", "您升级采美资质机构认证失败"); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "资质机构"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", "审核未通过"); JSONObject keyword4 = new JSONObject(); keyword4.put("value", auditTime); JSONObject keyword5 = new JSONObject(); keyword5.put("value", auditResult); JSONObject remark = new JSONObject(); remark.put("value", " "); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("keyword5", keyword5); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "btBvh1LIzTPMIpZNPNIupQnMcMw8_ziyQ6CGV68eN7s"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } /** * 供应商审核失败消息推送 * @param accessToken * @param openid * @param pagePath 跳转链接 */ public void sendTemplate(String accessToken, String openid,String shopName,String auditNote,String auditTime, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "您提交的供应商账号注册申请审核失败。"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", shopName); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "账号注册"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", auditNote); JSONObject keyword4 = new JSONObject(); keyword4.put("value", auditTime); JSONObject remark = new JSONObject(); remark.put("value", "请修改重新提交审核。"); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "QogD0Mu5zvNo51hqJXXNYiVByuDmYLxg2kxHUMqlyCI"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } /** * 商品审核未通过消息推送 * @param accessToken * @param openid * @param pagePath 跳转链接 */ public void sendTemp(String accessToken, String openid,String name,String auditTime, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "商品审核未通过通知"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", "抱歉,您发布的商品未通过审核"); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "审核未通过"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", auditTime); JSONObject keyword4 = new JSONObject(); keyword4.put("value", name); JSONObject remark = new JSONObject(); remark.put("value", "请前往采美网站修改商品信息重新发布"); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "oGiOHlJgx0L8rWmq6wqXHUIZjIzuPkiRu2xbqTE26Tg"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } /** * 商品审核未通过消息推送 * @param accessToken * @param openid * @param pagePath 跳转链接 */ public void messteme(String accessToken, String openid,String name,String qualificationNo,String auditTime,String proName, String pagePath) { JSONObject first = new JSONObject(); first.put("value", "您好,您有商品资质证书即将到期。"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", name); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "医疗器械注册证"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", qualificationNo); JSONObject keyword4 = new JSONObject(); keyword4.put("value", auditTime); JSONObject remark = new JSONObject(); remark.put("value", proName); JSONObject data = new JSONObject(); data.put("first", first); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("remark", remark); JSONObject miniProgram = new JSONObject(); miniProgram.put("appid", "wxf3cd4ae0cdd11c36"); miniProgram.put("pagepath", pagePath); JSONObject json = new JSONObject(); json.put("touser", openid); json.put("template_id", "gpD9TqBpkgaw3YSM_JPk4WSTwKZY0hxrBCH0SjzNKY4"); json.put("url", "https://www.caimei365.com/"); json.put("miniprogram", miniProgram); json.put("data", data); // json 字符串 String jsonString = json.toJSONString(); logger.info(">>>>>>>>推送微信模板消息:" + jsonString); try { String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken; // 发送请求 String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString); logger.info(">>>>>>>>推送结果:" + result); } catch (Exception e) { logger.info(">>>>>>>>>推送微信模板消息失败:",e); } } }