From 4fabfe4dbd2eb28d07a4350597d314958cc1c281 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期四, 09 十月 2025 11:16:43 +0800 Subject: [PATCH] 优化 --- server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java | 121 +++++++++++++++++++++++++++++++++++++--- 1 files changed, 111 insertions(+), 10 deletions(-) diff --git a/server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java b/server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java index 2eab145..190910f 100644 --- a/server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java +++ b/server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java @@ -1,18 +1,25 @@ package com.doumee.core.dingding; import com.alibaba.fastjson.JSONObject; +import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse; +import com.aliyun.tea.TeaException; import com.doumee.core.constants.Constants; +import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.HttpsUtil; import com.doumee.dao.business.model.BikeRepair; +import com.doumee.dao.business.model.Sites; import com.doumee.dao.business.web.request.RepairRequest; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.math.BigDecimal; import java.net.URLEncoder; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; /** * Created by IntelliJ IDEA. @@ -55,26 +62,120 @@ content.put("text",text); HttpsUtil.post(ddUrl+"×tamp="+timestamp+"&sign="+getSign(timestamp,secret), JSONObject.toJSONString(content),"application/json",false); } + /** + * 閽夐拤閫氱煡 - 绔欑偣閫氫俊寮傚父涓婃姤 + * @throws Exception + */ + public static void siteNoLink(List<Sites> siteList,String token,String robotCode,String chatToken) throws Exception{ + + Long timestamp = System.currentTimeMillis(); + Map<String,Object> content = new HashMap<>(); + String desc = ""; + if(siteList.size() == 1){ + desc = "銆愮珯鐐瑰紓甯镐笂鎶ャ�戠珯鐐广��"+siteList.get(0).getId()+"銆戯紝鑷��"+ DateUtil.getPlusTime2(siteList.get(0).getLastLinkDate())+"銆戝悗鏈敹鍒伴�氳璇锋眰,璇峰強鏃舵煡鐪嬪鐞哱n"; + }else{ + desc = "銆愮珯鐐瑰紓甯镐笂鎶ャ�� 浠ヤ笅绔欑偣閫氳鍙戠敓寮傚父锛岃鍙婃椂鏌ョ湅澶勭悊锛歕n"; + for (Sites site : siteList){ + desc += "绔欑偣銆�"+site.getId()+"銆戯紝鑷��"+ DateUtil.getPlusTime2(site.getLastLinkDate())+"銆戝悗鏈敹鍒伴�氳璇锋眰\n"; + } + } + + /* //鍋滅敤鏃х増鏈�氱煡 + Map<String,String> text = new HashMap<>(); + text.put("content",desc); + content.put("msgtype","text"); + + content.put("text",text); + HttpsUtil.post(ddUrl+"×tamp="+timestamp+"&sign="+getSign(timestamp,secret), JSONObject.toJSONString(content),"application/json",false); + */ //浣跨敤鏂扮増鏈�氱煡 + + sendNoticeNew(desc,token,robotCode,chatToken); + } + + + public static void lowVoltageNotice(String bikeCode,String place,String siteName,String token,String robotCode,String chatToken)throws Exception{ + String desc = "銆愪綆鐢甸噺鎻愰啋銆戣溅杈嗙紪鍙凤細{杞﹁締缂栧彿}锛屽綋鍓嶄綅缃細{瀹氫綅浣嶇疆}锛屽綋鍓嶇珯鐐癸細{绔欑偣鍚嶇О}銆�"; + if(StringUtils.isBlank(bikeCode)){ + return; + } + desc= desc.replace("{杞﹁締缂栧彿}",bikeCode); + desc= desc.replace("{瀹氫綅浣嶇疆}",StringUtils.defaultString(place,"鏈煡")); + desc= desc.replace("{绔欑偣鍚嶇О}",StringUtils.defaultString(siteName,"鏈煡")); + sendNoticeNew(desc,token,robotCode,chatToken); + } /** * 閽夐拤閫氱煡 - 绔欑偣鍌ㄨ溅鍛婅 * @param siteName - * @param ddUrl - * @param secret * @param type 0=瓒呭嚭 1=浣庝簬 * @param warnVal 鎶ヨ鍊� * @throws Exception */ - public static void reservesNotice(String siteName, String ddUrl , String secret, Integer type, BigDecimal warnVal) throws Exception{ + public static void reservesNotice(String siteName, Integer type, BigDecimal warnVal,String token,String robotCode,String chatToken) throws Exception{ Long timestamp = System.currentTimeMillis(); - Map<String,Object> content = new HashMap<>(); - content.put("msgtype","text"); - Map<String,String> text = new HashMap<>(); - //text.put("content","銆愮珯鐐瑰偍杞﹀憡璀︺��"+siteName+"锛屽偍杞﹂噺瓒呰繃80%/浣庝簬20%锛岃鍙婃椂杩涜杞﹁締璋冮厤"); + String desc ="銆愮珯鐐瑰偍杞﹀憡璀︺��"+siteName+"锛屽偍杞﹂噺"+(type.equals(Constants.ZERO)?"瓒呭嚭":"浣庝簬")+warnVal+"%锛岃鍙婃椂杩涜杞﹁締璋冮厤"; + sendNoticeNew(desc,token,robotCode,chatToken); + } - text.put("content","銆愮珯鐐瑰偍杞﹀憡璀︺��"+siteName+"锛屽偍杞﹂噺"+(type.equals(Constants.ZERO)?"瓒呭嚭":"浣庝簬")+warnVal+"%锛岃鍙婃椂杩涜杞﹁締璋冮厤"); - content.put("text",text); - HttpsUtil.post(ddUrl+"×tamp="+timestamp+"&sign="+getSign(timestamp,secret), JSONObject.toJSONString(content),"application/json",false); + + public static void sendNoticeNew( String notice,String token,String robotCode,String chatToken) throws Exception { + com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config(); + config.protocol = "https"; + config.regionId = "central"; + com.aliyun.dingtalkrobot_1_0.Client client = new com.aliyun.dingtalkrobot_1_0.Client(config); + com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders orgGroupSendHeaders = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders(); + orgGroupSendHeaders.xAcsDingtalkAccessToken = token; + com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest orgGroupSendRequest = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest() + .setMsgParam("{\"content\":\""+notice+"\"}") + .setMsgKey("sampleText") + .setRobotCode(robotCode) + .setToken(chatToken); + try { + client.orgGroupSendWithOptions(orgGroupSendRequest, orgGroupSendHeaders, new com.aliyun.teautil.models.RuntimeOptions()); + } catch (TeaException err) { + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + } + + } catch (Exception _err) { + TeaException err = new TeaException(_err.getMessage(), _err); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + } + + } + } + + public static String getToken(String appKey,String appSecret) throws Exception { + + com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config(); + config.protocol = "https"; + config.regionId = "central"; + com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config); + com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest(); + getAccessTokenRequest.setAppKey(appKey); + getAccessTokenRequest.setAppSecret(appSecret); + try { + GetAccessTokenResponse response = client.getAccessToken(getAccessTokenRequest); + return response.getBody().getAccessToken(); + } catch (TeaException err) { + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + } + } catch (Exception _err) { + TeaException err = new TeaException(_err.getMessage(), _err); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + } + + } + return null; + } + + public static void main(String[] args) throws Exception { + + String token = "ddedf254eb76334184ab7c98d8bde50a"; + } } -- Gitblit v1.9.3