AliSmsUtil.java 6.3 KB
package com.skua.tool.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.skua.common.constant.CacheConstant;
import com.skua.core.exception.JeecgBootException;
import com.skua.modules.common.vo.AliAlarm;
import com.skua.modules.common.vo.AliCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里短信发送
 */
@Slf4j
@Service
public class AliSmsUtil {
    static String url = "http://47.104.23.94:8088/";
    static String signName = "金控数矿";
    // 项目id(每个项目独立使用)
    static String projectId = "556a0a9d-5a78-400e-bf10-24fa14a1a925";
    // 项目accessKey(每个项目独立使用)
    static String accessKey = "9a1832e31135dc5bd72590580dc89943b68554dd8d26af0021af84aae7259773";
    static RestTemplate restTemplate = new RestTemplate();

    /**
     *
     * @param phoneNumbers 多个号码,以,隔开
     * @param template 短信模板id
     * @param message 短信内容 模板内容与模板参数必须完全匹配
     */
    public static void sendMsg(String phoneNumbers, String template, Map<String, Object> message){
        RequestEntity<Map> requestEntity = null;
        ResponseEntity<Map> responseEntity = null;
        try {
            Map requestBody = new HashMap();
            requestBody.put("phone_numbers", phoneNumbers);
            requestBody.put("template", template);
            requestBody.put("message", new ObjectMapper().writeValueAsString(message));
            requestBody.put("operator", "aliyun");
            requestBody.put("sign_name", signName);
            requestBody.put("project_id", projectId);
            requestBody.put("access_key", accessKey);
            requestEntity = RequestEntity.post(new URI(url + "sms/send"))
                    .header("content-type","application/json")
                    .body(requestBody, Map.class);
            responseEntity = restTemplate.exchange(requestEntity, Map.class);
            Integer code = (Integer) responseEntity.getBody().get("code");
            if (code != 200) {
                log.error(String.format("阿里短信发送失败 请求=%s 响应=%s", requestBody.toString(), responseEntity.toString()));
            }
        } catch (Exception e) {
            log.error(String.format("阿里短信发送失败 请求=%s 响应=%s",
                    requestEntity == null ? "" : requestEntity.toString() ,
                    responseEntity == null ? "" : responseEntity.toString()), e);
            throw new JeecgBootException("阿里短信发送失败");
        }
    }

    /**
     * 发送阈值报警短信
     */
    public static void sendLimitAlarmMsg(AliAlarm aliAlarm) {
        if(StringUtils.isNotEmpty(aliAlarm.getPhone())){
            HashMap<String, Object> message = new HashMap<>();
            message.put("time", aliAlarm.getTime());
            message.put("factory", aliAlarm.getFactory());
            message.put("alarmParamName", aliAlarm.getAlarmParamName());
            message.put("actualAvgValue", aliAlarm.getAlarmParamValue() + aliAlarm.getAlarmParamUnit());
            message.put("limit", aliAlarm.getLimitValue() + aliAlarm.getAlarmParamUnit());
            message.put("alarmParamUnit", aliAlarm.getAlarmParamReason());
            AliSmsUtil.sendMsg(aliAlarm.getPhone(), CacheConstant.SMS_TEMPLATE_LIMIT, message);
        }else{
            log.info("=======>>>>>>>>>>【阈值报警】接收人手机号未设定!");
        }

    }

    /**
     * 发送设备报警短信
     */
    public static void sendEquipAlarmMsg(AliAlarm aliAlarm) {
        if(StringUtils.isNotEmpty(aliAlarm.getPhone())){
            HashMap<String, Object> message = new HashMap<>();
            message.put("time", aliAlarm.getTime());
            message.put("factory", aliAlarm.getFactory());
            message.put("alarmParamName", aliAlarm.getAlarmParamName());
            message.put("actualAvgValue", aliAlarm.getAlarmParamValue() + aliAlarm.getAlarmParamUnit());
            message.put("limit", aliAlarm.getLimitValue() + aliAlarm.getAlarmParamUnit());
            message.put("alarmParamUnit", aliAlarm.getAlarmParamReason());
            AliSmsUtil.sendMsg(aliAlarm.getPhone(), CacheConstant.SMS_TEMPLATE_EQUIP, message);
        }else{
            log.info("=======>>>>>>>>>>【设备报警】接收人手机号未设定!");
        }
    }

    /**
     * 发送验证码短信
     */
    public static void sendCodeMsg(AliCode aliCode) {
        if(StringUtils.isNotEmpty(aliCode.getPhone())){
            HashMap<String, Object> message = new HashMap<>();
            message.put("code", aliCode.getCode());
            message.put("product", aliCode.getProduct());
            if("update".equals(aliCode.getType())){
                AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_UPDATE, message);
            }else if("login".equals(aliCode.getType())){
                AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_LOGIN, message);
            }else if("register".equals(aliCode.getType())){
                AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_REGISTER, message);
            }else{
                log.info("=======>>>>>>>>>>短信验证码缺少对应模版");
            }
        }else{
            log.info("=======>>>>>>>>>>【验证码】接收人手机号未设定!");
        }
    }

    public static void main(String[] args) {
//        AliAlarm aliAlarm = new AliAlarm();
//        aliAlarm.setPhone("18519526218");
//        aliAlarm.setTime("2024-11-01 11:25:29");
//        aliAlarm.setFactory("天长市污水处理厂");
//        aliAlarm.setAlarmParamName("进水COD");
//        aliAlarm.setAlarmParamValue("130");
//        aliAlarm.setAlarmParamUnit("mg/L");
//        aliAlarm.setLimitValue("100");
//        aliAlarm.setAlarmParamReason("大于上限");
//        sendLimitAlarmMsg(aliAlarm);
        AliCode aliCode = new AliCode();
        aliCode.setPhone("18519526218");
        aliCode.setCode("889955");
        aliCode.setProduct("数字化企业大脑");
        aliCode.setType("login");
        sendCodeMsg(aliCode);
    }
}