|
@@ -0,0 +1,80 @@
|
|
|
+package com.jihengbel.intelligent.applicationtask.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jihengbel.intelligent.applicationtask.common.HttpCilentUntil;
|
|
|
+import com.jihengbel.intelligent.applicationtask.entry.SendMsgReq;
|
|
|
+import com.jihengbel.intelligent.applicationtask.entry.TemplateEntry;
|
|
|
+import com.jihengbel.intelligent.applicationtask.model.UserMapper;
|
|
|
+import com.jihengbel.intelligent.applicationtask.service.MsgService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author sMArT
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2021-08-04 10:56 上午
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class MsgServiceImpl implements MsgService {
|
|
|
+ @Value("${wx.minprogram.appsecret}")
|
|
|
+ private String appsecret;
|
|
|
+
|
|
|
+ @Value("${wx.minprogram.appid}")
|
|
|
+ private String appid;
|
|
|
+
|
|
|
+ @Value("${wx.minprogram.templateId}")
|
|
|
+ private String templateId;
|
|
|
+
|
|
|
+ @Value("${wx.minprogram.page}")
|
|
|
+ private String page;
|
|
|
+
|
|
|
+ @Value("${wx.minprogram.miniprogramState}")
|
|
|
+ private String miniprogramState;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String sendWechatMessage(SendMsgReq sendMsgReq) {
|
|
|
+ try {
|
|
|
+ Map params = new HashMap();
|
|
|
+ params.put("secret", appsecret);
|
|
|
+ params.put("appid", appid);
|
|
|
+ params.put("grant_type", "client_credential");
|
|
|
+
|
|
|
+ HashMap<String, String> dataMap = new HashMap();
|
|
|
+ dataMap.put("thing1", sendMsgReq.getContent());
|
|
|
+ dataMap.put("thing2", sendMsgReq.getTitle());
|
|
|
+ dataMap.put("time3", sendMsgReq.getEndTime());
|
|
|
+ dataMap.put("thing4", "智云工厂系统");
|
|
|
+
|
|
|
+ String openId = userMapper.getOpenIdByUserId(sendMsgReq.getToUserId());
|
|
|
+ TemplateEntry template = new TemplateEntry();
|
|
|
+ template.setTemplate_id(templateId);
|
|
|
+ template.setTouser(openId);
|
|
|
+ template.setPage(page);
|
|
|
+ template.setMiniprogram_state(miniprogramState);
|
|
|
+ template.setData(dataMap);
|
|
|
+
|
|
|
+ String result = HttpCilentUntil.httpRequestToString(
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token", params);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+ String access_token = jsonObject.get("access_token").toString();
|
|
|
+ String pushUrl = new StringBuffer("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=").append(access_token).toString();
|
|
|
+ result = HttpCilentUntil.httpJsonToString(pushUrl, JSON.toJSONString(template));
|
|
|
+ jsonObject = JSON.parseObject(result);
|
|
|
+ String errCode = jsonObject.get("errcode").toString();
|
|
|
+ String errMsg = jsonObject.get("errmsg").toString();
|
|
|
+ System.out.println(errCode);
|
|
|
+ return errCode;
|
|
|
+ } catch (Throwable e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "-1";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|