|
@@ -1,76 +0,0 @@
|
|
|
-package com.huimv.env.device.config;
|
|
|
-
|
|
|
-
|
|
|
-import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
|
|
-import cn.hutool.http.HttpUtil;
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
-import me.chanjar.weixin.common.service.WxService;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-
|
|
|
-@Component
|
|
|
-public class WeChatMessage {
|
|
|
- @Value("${wx.appId}")
|
|
|
- private String appId;
|
|
|
-
|
|
|
- @Value("${wx.secret}")
|
|
|
- private String secret;
|
|
|
-
|
|
|
- @Value("${wx.templateId}")
|
|
|
- private String templateId;
|
|
|
-
|
|
|
- private String openid;
|
|
|
-
|
|
|
- public void sendMsg(String deviceCode, Date uploadDate,String location){
|
|
|
- //1:获取token(接口调用凭证)
|
|
|
- String token = queryToken();
|
|
|
- //2:发送订阅消息
|
|
|
- send(token,deviceCode,uploadDate,location);
|
|
|
- }
|
|
|
-
|
|
|
- // 1: 获取 access_token (2h过期)
|
|
|
- public String queryToken(){
|
|
|
- String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
|
|
|
- tokenUrl = tokenUrl + "&appid=" + appId + "&secret=" + secret;
|
|
|
- String result = HttpUtil.get(tokenUrl);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
- String token = jsonObject.get("access_token").toString();
|
|
|
- return token;
|
|
|
- }
|
|
|
-
|
|
|
- public void send(String token,String deviceCode, Date uploadDate,String location){
|
|
|
- String msgUrl="https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
|
|
|
- msgUrl = msgUrl + "?access_token=" + token;
|
|
|
- // 设置模板参数
|
|
|
- HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("touser", openid); // 接收方
|
|
|
- paramMap.put("template_id", templateId); // 模板id
|
|
|
- paramMap.put("page","pages/self/self"); // 消息中要跳转的页面
|
|
|
- // 设置data 模板内容
|
|
|
- HashMap<String, Object> data = new HashMap<>();
|
|
|
- //报警设备
|
|
|
- data.put("thing1", formatParam(deviceCode));
|
|
|
- //报警类型
|
|
|
- data.put("thing2", formatParam("断电报警"));
|
|
|
- //报警时间
|
|
|
- data.put("thing3", formatParam(uploadDate.toString()));
|
|
|
- //报警地点
|
|
|
- data.put("thing5", formatParam(location));
|
|
|
- paramMap.put("data", data);
|
|
|
- // 转json字符串
|
|
|
- String jsonObject = JSONUtil.toJsonStr(paramMap);
|
|
|
- String result= HttpUtil.post(msgUrl, jsonObject);
|
|
|
- System.out.println(result);
|
|
|
- }
|
|
|
-
|
|
|
- public HashMap<String, Object> formatParam(String value){
|
|
|
- HashMap<String, Object> data = new HashMap<>();
|
|
|
- data.put("value", value);
|
|
|
- return data;
|
|
|
- }
|
|
|
-
|
|
|
-}
|