package com.doumee.core.dingding; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.doumee.core.constants.Constants; 
 | 
import com.doumee.core.utils.HttpsUtil; 
 | 
import com.doumee.dao.business.model.BikeRepair; 
 | 
import com.doumee.dao.business.web.request.RepairRequest; 
 | 
import org.apache.commons.codec.binary.Base64; 
 | 
  
 | 
import javax.crypto.Mac; 
 | 
import javax.crypto.spec.SecretKeySpec; 
 | 
import java.math.BigDecimal; 
 | 
import java.net.URLEncoder; 
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
  
 | 
/** 
 | 
 * Created by IntelliJ IDEA. 
 | 
 * 
 | 
 * @Author : Rk 
 | 
 * @create 2022/12/7 11:16 
 | 
 */ 
 | 
public class DingDingNotice { 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 钉钉机器人 加签 
 | 
     * @param timestamp 
 | 
     * @param secret 
 | 
     * @return 
 | 
     * @throws Exception 
 | 
     */ 
 | 
    public static String getSign(Long timestamp, String secret)  throws Exception{ 
 | 
        String stringToSign = timestamp + "\n" + secret; 
 | 
        Mac mac = Mac.getInstance("HmacSHA256"); 
 | 
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); 
 | 
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); 
 | 
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8"); 
 | 
        return sign; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 钉钉通知 - 车辆异常上报 
 | 
     * @param repairRequest 
 | 
     * @param ddUrl 
 | 
     * @throws Exception 
 | 
     */ 
 | 
    public static void bikeRepair(RepairRequest repairRequest, String ddUrl , String secret)  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","【故障上报】车辆编号:"+repairRequest.getBikeCode()+",位置:"+repairRequest.getAddr()+",联系电话:"+repairRequest.getLinkphone()+",请及时查看处理。"); 
 | 
        content.put("text",text); 
 | 
        HttpsUtil.post(ddUrl+"×tamp="+timestamp+"&sign="+getSign(timestamp,secret), JSONObject.toJSONString(content),"application/json",false); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 钉钉通知 - 站点储车告警 
 | 
     * @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{ 
 | 
        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%,请及时进行车辆调配"); 
 | 
  
 | 
        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); 
 | 
    } 
 | 
} 
 |