WeChatService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package com.caimei.modules.order.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.caimei.modules.utils.RequestUtil;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import java.util.Map;
  10. /**
  11. * Description
  12. *
  13. * @author : Charles
  14. * @date : 2022/4/12
  15. */
  16. @Service
  17. public class WeChatService {
  18. @Value("${wx.crm-app-id}")
  19. private String crmAppId;
  20. @Value("${wx.crm-app-secret}")
  21. private String crmAppSecret;
  22. protected Logger logger = LoggerFactory.getLogger(getClass());
  23. /**
  24. * 微信公众号获取access_token
  25. *
  26. * @return access_token
  27. */
  28. public String getAccessToken() throws Exception {
  29. String link = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
  30. link = link.replace("APPID", "wx91c4152b60ca91a3");
  31. link = link.replace("APPSECRET", "a563dd2c07c9c815a4e697c8b6cb73dc");
  32. String result = RequestUtil.sendGet(link);
  33. logger.info("微信公众号获取access_token>>>" + result);
  34. Map<String, Object> map = JSONObject.parseObject(result, Map.class);
  35. String access_token = (String) map.get("access_token");
  36. return access_token;
  37. }
  38. /**
  39. * 发货消息推送
  40. * @param accessToken
  41. * @param openid
  42. * @param shopOrderNo 子订单编号
  43. * @param time 发货时间
  44. * @param company 快递名称
  45. * @param companyno 快递编号
  46. * @param pagePath 跳转链接
  47. */
  48. public void sendTemplateMsgy(String accessToken, String openid, String shopOrderNo,String time, String company, String companyno, String pagePath) {
  49. JSONObject first = new JSONObject();
  50. first.put("value", "尊敬的采美客户,您购买的订单已经发货啦~");
  51. JSONObject keyword1 = new JSONObject();
  52. keyword1.put("value", shopOrderNo);
  53. JSONObject keyword2 = new JSONObject();
  54. keyword2.put("value", time);
  55. JSONObject keyword3 = new JSONObject();
  56. keyword3.put("value", company);
  57. JSONObject keyword4 = new JSONObject();
  58. keyword4.put("value", companyno);
  59. JSONObject remark = new JSONObject();
  60. remark.put("value", "收到货后请记得确认收货哟~");
  61. JSONObject data = new JSONObject();
  62. data.put("first", first);
  63. data.put("keyword1", keyword1);
  64. data.put("keyword2", keyword2);
  65. data.put("keyword3", keyword3);
  66. data.put("keyword4", keyword4);
  67. data.put("remark", remark);
  68. JSONObject miniProgram = new JSONObject();
  69. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  70. miniProgram.put("pagepath", pagePath);
  71. JSONObject json = new JSONObject();
  72. json.put("touser", openid);
  73. json.put("template_id", "_Zc6WiUp3hM4yBgT30HkTyA-SmcCN-sjnGpIa6n3rKI");
  74. json.put("url", "https://www.caimei365.com/");
  75. json.put("miniprogram", miniProgram);
  76. json.put("data", data);
  77. // json 字符串
  78. String jsonString = json.toJSONString();
  79. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  80. try {
  81. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  82. // 发送请求
  83. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  84. logger.info(">>>>>>>>推送结果:" + result);
  85. } catch (Exception e) {
  86. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  87. }
  88. }
  89. /**
  90. * 资质机构认证成功消息推送
  91. * @param accessToken
  92. * @param openid
  93. * @param pagePath 跳转链接
  94. */
  95. public void sendTemplateMsg(String accessToken, String openid ,String auditTime, String pagePath) {
  96. JSONObject first = new JSONObject();
  97. first.put("value", "认证成功通知");
  98. JSONObject keyword1 = new JSONObject();
  99. keyword1.put("value", "恭喜您成功成为采美资质机构");
  100. JSONObject keyword2 = new JSONObject();
  101. keyword2.put("value", "资质机构");
  102. JSONObject keyword3 = new JSONObject();
  103. keyword3.put("value", "审核通过");
  104. JSONObject keyword4 = new JSONObject();
  105. keyword4.put("value", auditTime);
  106. JSONObject remark = new JSONObject();
  107. remark.put("value", "认证成功后,您将享受到更多的优惠机会。");
  108. JSONObject data = new JSONObject();
  109. data.put("first", first);
  110. data.put("keyword1", keyword1);
  111. data.put("keyword2", keyword2);
  112. data.put("keyword3", keyword3);
  113. data.put("keyword4", keyword4);
  114. data.put("remark", remark);
  115. JSONObject miniProgram = new JSONObject();
  116. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  117. miniProgram.put("pagepath", pagePath);
  118. JSONObject json = new JSONObject();
  119. json.put("touser", openid);
  120. json.put("template_id", "kfzhVTJ6sNOJSgwk8x7Z9_m8S2LRzDfAGtiUgGyO8Zk");
  121. json.put("url", "https://www.caimei365.com/");
  122. json.put("miniprogram", miniProgram);
  123. json.put("data", data);
  124. // json 字符串
  125. String jsonString = json.toJSONString();
  126. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  127. try {
  128. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  129. // 发送请求
  130. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  131. logger.info(">>>>>>>>推送结果:" + result);
  132. } catch (Exception e) {
  133. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  134. }
  135. }
  136. /**
  137. * 资质机构认证失败消息推送
  138. * @param accessToken
  139. * @param openid
  140. * @param pagePath 跳转链接
  141. */
  142. public void sendTemplateMessg(String accessToken, String openid, String auditResult,String auditTime, String pagePath) {
  143. JSONObject first = new JSONObject();
  144. first.put("value", "认证失败通知");
  145. JSONObject keyword1 = new JSONObject();
  146. keyword1.put("value", "您升级采美资质机构认证失败");
  147. JSONObject keyword2 = new JSONObject();
  148. keyword2.put("value", "资质机构");
  149. JSONObject keyword3 = new JSONObject();
  150. keyword3.put("value", "审核未通过");
  151. JSONObject keyword4 = new JSONObject();
  152. keyword4.put("value", auditTime);
  153. JSONObject keyword5 = new JSONObject();
  154. keyword5.put("value", auditResult);
  155. JSONObject remark = new JSONObject();
  156. remark.put("value", " ");
  157. JSONObject data = new JSONObject();
  158. data.put("first", first);
  159. data.put("keyword1", keyword1);
  160. data.put("keyword2", keyword2);
  161. data.put("keyword3", keyword3);
  162. data.put("keyword4", keyword4);
  163. data.put("keyword5", keyword5);
  164. data.put("remark", remark);
  165. JSONObject miniProgram = new JSONObject();
  166. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  167. miniProgram.put("pagepath", pagePath);
  168. JSONObject json = new JSONObject();
  169. json.put("touser", openid);
  170. json.put("template_id", "btBvh1LIzTPMIpZNPNIupQnMcMw8_ziyQ6CGV68eN7s");
  171. json.put("url", "https://www.caimei365.com/");
  172. json.put("miniprogram", miniProgram);
  173. json.put("data", data);
  174. // json 字符串
  175. String jsonString = json.toJSONString();
  176. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  177. try {
  178. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  179. // 发送请求
  180. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  181. logger.info(">>>>>>>>推送结果:" + result);
  182. } catch (Exception e) {
  183. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  184. }
  185. }
  186. /**
  187. * 供应商审核失败消息推送
  188. * @param accessToken
  189. * @param openid
  190. * @param pagePath 跳转链接
  191. */
  192. public void sendTemplate(String accessToken, String openid,String shopName,String auditNote,String auditTime, String pagePath) {
  193. JSONObject first = new JSONObject();
  194. first.put("value", "您提交的供应商账号注册申请审核失败。");
  195. JSONObject keyword1 = new JSONObject();
  196. keyword1.put("value", shopName);
  197. JSONObject keyword2 = new JSONObject();
  198. keyword2.put("value", "账号注册");
  199. JSONObject keyword3 = new JSONObject();
  200. keyword3.put("value", auditNote);
  201. JSONObject keyword4 = new JSONObject();
  202. keyword4.put("value", auditTime);
  203. JSONObject remark = new JSONObject();
  204. remark.put("value", "请修改重新提交审核。");
  205. JSONObject data = new JSONObject();
  206. data.put("first", first);
  207. data.put("keyword1", keyword1);
  208. data.put("keyword2", keyword2);
  209. data.put("keyword3", keyword3);
  210. data.put("keyword4", keyword4);
  211. data.put("remark", remark);
  212. JSONObject miniProgram = new JSONObject();
  213. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  214. miniProgram.put("pagepath", pagePath);
  215. JSONObject json = new JSONObject();
  216. json.put("touser", openid);
  217. json.put("template_id", "QogD0Mu5zvNo51hqJXXNYiVByuDmYLxg2kxHUMqlyCI");
  218. json.put("url", "https://www.caimei365.com/");
  219. json.put("miniprogram", miniProgram);
  220. json.put("data", data);
  221. // json 字符串
  222. String jsonString = json.toJSONString();
  223. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  224. try {
  225. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  226. // 发送请求
  227. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  228. logger.info(">>>>>>>>推送结果:" + result);
  229. } catch (Exception e) {
  230. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  231. }
  232. }
  233. /**
  234. * 商品审核未通过消息推送
  235. * @param accessToken
  236. * @param openid
  237. * @param pagePath 跳转链接
  238. */
  239. public void sendTemp(String accessToken, String openid,String name,String auditTime, String pagePath) {
  240. JSONObject first = new JSONObject();
  241. first.put("value", "商品审核未通过通知");
  242. JSONObject keyword1 = new JSONObject();
  243. keyword1.put("value", "抱歉,您发布的商品未通过审核");
  244. JSONObject keyword2 = new JSONObject();
  245. keyword2.put("value", "审核未通过");
  246. JSONObject keyword3 = new JSONObject();
  247. keyword3.put("value", auditTime);
  248. JSONObject keyword4 = new JSONObject();
  249. keyword4.put("value", name);
  250. JSONObject remark = new JSONObject();
  251. remark.put("value", "请前往采美网站修改商品信息重新发布");
  252. JSONObject data = new JSONObject();
  253. data.put("first", first);
  254. data.put("keyword1", keyword1);
  255. data.put("keyword2", keyword2);
  256. data.put("keyword3", keyword3);
  257. data.put("keyword4", keyword4);
  258. data.put("remark", remark);
  259. JSONObject miniProgram = new JSONObject();
  260. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  261. miniProgram.put("pagepath", pagePath);
  262. JSONObject json = new JSONObject();
  263. json.put("touser", openid);
  264. json.put("template_id", "oGiOHlJgx0L8rWmq6wqXHUIZjIzuPkiRu2xbqTE26Tg");
  265. json.put("url", "https://www.caimei365.com/");
  266. json.put("miniprogram", miniProgram);
  267. json.put("data", data);
  268. // json 字符串
  269. String jsonString = json.toJSONString();
  270. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  271. try {
  272. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  273. // 发送请求
  274. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  275. logger.info(">>>>>>>>推送结果:" + result);
  276. } catch (Exception e) {
  277. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  278. }
  279. }
  280. /**
  281. * 商品审核未通过消息推送
  282. * @param accessToken
  283. * @param openid
  284. * @param pagePath 跳转链接
  285. */
  286. public void messteme(String accessToken, String openid,String name,String qualificationNo,String auditTime,String proName, String pagePath) {
  287. JSONObject first = new JSONObject();
  288. first.put("value", "您好,您有商品资质证书即将到期。");
  289. JSONObject keyword1 = new JSONObject();
  290. keyword1.put("value", name);
  291. JSONObject keyword2 = new JSONObject();
  292. keyword2.put("value", "医疗器械注册证");
  293. JSONObject keyword3 = new JSONObject();
  294. keyword3.put("value", qualificationNo);
  295. JSONObject keyword4 = new JSONObject();
  296. keyword4.put("value", auditTime);
  297. JSONObject remark = new JSONObject();
  298. remark.put("value", proName);
  299. JSONObject data = new JSONObject();
  300. data.put("first", first);
  301. data.put("keyword1", keyword1);
  302. data.put("keyword2", keyword2);
  303. data.put("keyword3", keyword3);
  304. data.put("keyword4", keyword4);
  305. data.put("remark", remark);
  306. JSONObject miniProgram = new JSONObject();
  307. miniProgram.put("appid", "wxf3cd4ae0cdd11c36");
  308. miniProgram.put("pagepath", pagePath);
  309. JSONObject json = new JSONObject();
  310. json.put("touser", openid);
  311. json.put("template_id", "gpD9TqBpkgaw3YSM_JPk4WSTwKZY0hxrBCH0SjzNKY4");
  312. json.put("url", "https://www.caimei365.com/");
  313. json.put("miniprogram", miniProgram);
  314. json.put("data", data);
  315. // json 字符串
  316. String jsonString = json.toJSONString();
  317. logger.info(">>>>>>>>推送微信模板消息:" + jsonString);
  318. try {
  319. String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
  320. // 发送请求
  321. String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
  322. logger.info(">>>>>>>>推送结果:" + result);
  323. } catch (Exception e) {
  324. logger.info(">>>>>>>>>推送微信模板消息失败:",e);
  325. }
  326. }
  327. }