移動雲mas 通過HTTP請求發送普通短信和 模板短信

 一、短信發送配置

  

tnar:
 mobileCloud:
    sms:
      url: http://112.35.1.155:1992/sms/norsubmit
      ecName: 公司名稱
      apId: 賬號
      secretKey: 密碼
      sign: 簽名

二、短信發送工具類

package com.tnar.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tnar.dto.request.SendReq;
import com.tnar.dto.request.SendTemplateReq;
import com.tnar.dto.response.SendRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

/**
 * @author dzx
 * @ClassName:
 * @Description: 移動雲mas短信工具類
 * @date 2019年05月15日 15:01:21
 */
@Component
public class MobileCloudSmsUtil {

    @Value("${tnar.mobileCloud.sms.url}")
    private String url;

    @Value("${tnar.mobileCloud.sms.ecName}")
    private String ecName;
    @Value("${tnar.mobileCloud.sms.apId}")
    private String apId;
    @Value("${tnar.mobileCloud.sms.secretKey}")
    private String secretKey;
    @Value("${tnar.mobileCloud.sms.sign}")
    private String sign;

    private String addSerial = "";

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 發送普通短信
     *
     * @param mobiles 多個手機號用逗號隔開
     * @param content 短信內容
     * @return
     * @throws UnsupportedEncodingException
     */
    public SendRes sendOrdinarySms(String mobiles, String content) throws UnsupportedEncodingException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        SendReq sendReq = new SendReq();
        sendReq.setEcName(ecName).setApId(apId).setSecretKey(secretKey).setMobiles(mobiles).setContent(content).
                setSign(sign).setAddSerial(addSerial);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sendReq.getEcName())
                .append(sendReq.getApId())
                .append(sendReq.getSecretKey())
                .append(sendReq.getMobiles())
                .append(sendReq.getContent())
                .append(sendReq.getSign())
                .append(sendReq.getAddSerial());

        sendReq.setMac(DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes()).toLowerCase());
        String jsonText = JSON.toJSONString(sendReq);
        String body = Base64.getEncoder().encodeToString(jsonText.getBytes("UTF-8"));
        HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);
        JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        return result.toJavaObject(SendRes.class);
    }


    /**
     * 發送模板短信
     *
     * @param mobiles    手機號
     * @param templateId 模板id
     * @param params     參數列表
     * @return
     * @throws UnsupportedEncodingException
     */
    public SendRes sendTemplateSms(String mobiles, String templateId, List<String> params) throws UnsupportedEncodingException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        SendTemplateReq sendTemplateReq = new SendTemplateReq();
        sendTemplateReq.setEcName(ecName).setApId(apId).setSecretKey(secretKey).setMobiles(mobiles)
                .setSign(sign).setAddSerial(addSerial)
                .setTemplateId(templateId)
                .setParams(JSON.toJSONString(params.toArray()));

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sendTemplateReq.getEcName())
                .append(sendTemplateReq.getApId())
                .append(sendTemplateReq.getSecretKey())
                .append(sendTemplateReq.getTemplateId())
                .append(sendTemplateReq.getMobiles())
                .append(sendTemplateReq.getParams())
                .append(sendTemplateReq.getSign())
                .append(sendTemplateReq.getAddSerial());

        sendTemplateReq.setMac(DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes()).toLowerCase());
        String jsonText = JSON.toJSONString(sendTemplateReq);
        String body = Base64.getEncoder().encodeToString(jsonText.getBytes("UTF-8"));
        HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);
        JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        return result.toJavaObject(SendRes.class);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章