111
k94314517
2025-02-20 68057e752bd9767e76c021610966719e3a227467
server/services/src/main/java/com/doumee/core/dingding/DingDingNotice.java
@@ -1,6 +1,8 @@
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;
@@ -8,6 +10,7 @@
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;
@@ -16,6 +19,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
 * Created by IntelliJ IDEA.
@@ -60,48 +64,126 @@
    }
    /**
     * 钉钉通知 - 站点通信异常上报
     * @param ddUrl
     * @throws Exception
     */
    public static void siteNoLink(List<Sites> siteList, String ddUrl , String secret)  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<>();
        content.put("msgtype","text");
        Map<String,String> text = new HashMap<>();
        String  desc = "";
        if(siteList.size() == 1){
            desc = "【站点异常上报】站点【"+siteList.get(0).getId()+"】,自【"+ DateUtil.getPlusTime2(siteList.get(0).getLastLinkDate())+"】后未收到通讯请求,请及时查看处理\n";
        }else{
               desc = "【站点异常上报】 以下站点通讯发生异常,请及时查看处理:\n";
            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+"&timestamp="+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.replace("{车辆编号}",bikeCode);
        if(StringUtils.isNotBlank(place)){
           desc.replace("{定位位置}",place);
        }else{
            desc.replace("{定位位置}","未知");
        }
        if(StringUtils.isNotBlank(siteName)){
            desc.replace("{站点名称}",siteName);
        }else{
            desc.replace("{站点名称}","未知");
        }
        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+"&timestamp="+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";
    }
}