|
@@ -1,10 +1,25 @@
|
|
|
package com.szwl.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.szwl.constant.ConfigConsts;
|
|
|
+import com.szwl.model.bean.WeChatTemplateMsg;
|
|
|
+import com.szwl.model.entity.TAdmin;
|
|
|
import com.szwl.model.entity.TWechat;
|
|
|
import com.szwl.mapper.TWechatMapper;
|
|
|
+import com.szwl.service.TAdminService;
|
|
|
import com.szwl.service.TWechatService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.szwl.util.WechatSendUtil;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -17,4 +32,75 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> implements TWechatService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ TAdminService adminService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String sendNoworkMessage(String openId, String clientId, String name, String ifForeign, String companyType) {
|
|
|
+ // 模板参数
|
|
|
+ Map<String, WeChatTemplateMsg> sendMag = new HashMap();
|
|
|
+ // 公众号的模板id(也有相应的接口可以查询到)
|
|
|
+ String templateId = "tNMH94OG5bRkTCK1RFi7_zdmj6Y-27iy5Fq1mBH6wTQ";
|
|
|
+ // 微信的基础accessToken
|
|
|
+ String accessToken = WechatSendUtil.getAccessToken(companyType);;
|
|
|
+// TAdmin admin = adminService.getById(1L);
|
|
|
+// accessToken = admin.getDepartment();
|
|
|
+// if(StringUtils.isEmpty(accessToken)){
|
|
|
+// accessToken = WechatSendUtil.getAccessToken();
|
|
|
+// }
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
|
|
|
+
|
|
|
+// StringBuffer cli = new StringBuffer();
|
|
|
+// String keyword1 = cli.append(clientId.substring(clientId.length()-6)).append("-").append(name).toString();
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String alarmTime = format.format(new Date());
|
|
|
+ if (StringUtils.isEmpty(ifForeign) || ifForeign.equals("0")) {
|
|
|
+ sendMag.put("thing11", new WeChatTemplateMsg(name));
|
|
|
+ sendMag.put("character_string14", new WeChatTemplateMsg(clientId.substring(clientId.length()-6)));
|
|
|
+ sendMag.put("thing4", new WeChatTemplateMsg("严重"));
|
|
|
+ sendMag.put("time5", new WeChatTemplateMsg(alarmTime));
|
|
|
+ sendMag.put("thing6", new WeChatTemplateMsg("机器可能出现断网或突然断电,请及时处理!"));
|
|
|
+ // 国内
|
|
|
+// sendMag.put("first", new WeChatTemplateMsg("设备异常"));
|
|
|
+// sendMag.put("keyword1", new WeChatTemplateMsg(keyword1));
|
|
|
+// sendMag.put("keyword2", new WeChatTemplateMsg("现在"));
|
|
|
+// sendMag.put("keyword3", new WeChatTemplateMsg("严重"));
|
|
|
+// sendMag.put("keyword4", new WeChatTemplateMsg("信号"));
|
|
|
+// sendMag.put("keyword5",
|
|
|
+// new WeChatTemplateMsg("机器可能出现断网或突然断电,请及时处理!"));
|
|
|
+// sendMag.put("remark",
|
|
|
+// new WeChatTemplateMsg("Please pay attention to the time and status of the above alarms and take appropriate control measures/请注意以上告警发生的时间和状态,采取适当的控制措施."));
|
|
|
+ } else {
|
|
|
+ sendMag.put("thing11", new WeChatTemplateMsg(name));
|
|
|
+ sendMag.put("character_string14", new WeChatTemplateMsg(clientId.substring(clientId.length()-6)));
|
|
|
+ sendMag.put("thing4", new WeChatTemplateMsg("Serious"));
|
|
|
+ sendMag.put("time5", new WeChatTemplateMsg(alarmTime));
|
|
|
+ sendMag.put("thing6", new WeChatTemplateMsg("Power/Network loss."));
|
|
|
+ // 国外
|
|
|
+// sendMag.put("first", new WeChatTemplateMsg("Equipment abnormality"));
|
|
|
+// sendMag.put("keyword1", new WeChatTemplateMsg(keyword1));
|
|
|
+// sendMag.put("keyword2", new WeChatTemplateMsg("Now"));
|
|
|
+// sendMag.put("keyword3", new WeChatTemplateMsg("Serious"));
|
|
|
+// sendMag.put("keyword4", new WeChatTemplateMsg("Network"));
|
|
|
+// sendMag.put("keyword5",
|
|
|
+// new WeChatTemplateMsg("The machine may be disconnected or suddenly power off, please deal with it in time!"));
|
|
|
+ }
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ Map<String, Object> sendBody = new HashMap<>();
|
|
|
+ sendBody.put("touser", openId);
|
|
|
+ sendBody.put("topcolor", "#FF0000");
|
|
|
+ sendBody.put("data", sendMag);
|
|
|
+ if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
+ sendBody.put("template_id", ConfigConsts.SZ_TEMPLATE_ID);
|
|
|
+ } else {
|
|
|
+ sendBody.put("template_id", ConfigConsts.SC_TEMPLATE_ID);
|
|
|
+ }
|
|
|
+// sendBody.put("template_id", templateId);
|
|
|
+ ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
|
|
|
+ String messageCode = jsonObject.getString("errcode");
|
|
|
+ String msgId = jsonObject.getString("msgid");
|
|
|
+ System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
|
|
|
+ return forEntity.getBody();
|
|
|
+ }
|
|
|
}
|