|
@@ -0,0 +1,97 @@
|
|
|
+package com.caimei.modules.user.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.caimei.modules.utils.RequestUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : hzg
|
|
|
+ * @date : 2023/11/16
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class WebChatUserService {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分配协销消息
|
|
|
+ *
|
|
|
+ * @param title 消息抬头
|
|
|
+ * @param keyWord1
|
|
|
+ * @param keyWord2
|
|
|
+ * @param keyWord3
|
|
|
+ * @param keyWord4
|
|
|
+ * @param Remark 消息下备注
|
|
|
+ * @param path 跳转链接
|
|
|
+ * @param openId 公众号openId
|
|
|
+ * @param templateId 模板Id
|
|
|
+ */
|
|
|
+ public void sendChoseServiceMessage(String title, String keyWord1, String keyWord2, String keyWord3, String keyWord4, String Remark, String path, String openId, String templateId) {
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", title);
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", keyWord1);
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", keyWord2);
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", keyWord3);
|
|
|
+ JSONObject keyword4 = new JSONObject();
|
|
|
+ keyword4.put("value", keyWord4);
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", Remark);
|
|
|
+
|
|
|
+ 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", path);
|
|
|
+
|
|
|
+ JSONObject json = new JSONObject(new LinkedHashMap());
|
|
|
+ json.put("touser", openId);
|
|
|
+ json.put("template_id", templateId);
|
|
|
+ json.put("url", "https://www.caimei365.com/");
|
|
|
+ json.put("miniprogram", miniProgram);
|
|
|
+ json.put("data", data);
|
|
|
+ // json 字符串
|
|
|
+ String jsonString = json.toJSONString();
|
|
|
+ log.info(">>>>>>>>推送微信模板消息:" + jsonString);
|
|
|
+ try {
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken();
|
|
|
+ // 发送请求
|
|
|
+ String result = RequestUtil.httpRequest(requestUrl, "POST", jsonString);
|
|
|
+ log.info(">>>>>>>>推送结果:" + result);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("推送微信模板消息失败:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信公众号获取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);
|
|
|
+ log.info("微信公众号获取access_token>>>" + link);
|
|
|
+ log.info("微信公众号获取access_token>>>" + result);
|
|
|
+ Map<String, Object> map = JSONObject.parseObject(result, Map.class);
|
|
|
+ return (String) map.get("access_token");
|
|
|
+ }
|
|
|
+}
|