package com.stylefeng.guns.modular.system.util;
|
|
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.http.HttpRequest;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.stylefeng.guns.core.util.MD5Util;
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Arrays;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2023/3/31 10:05
|
*/
|
public class SMSUtil {
|
|
private static String username = "Ztrbkjhy";
|
|
private static String password = "Zycx2023Rbkj";
|
|
private static String signature = "【昭阳出行】";
|
|
|
/**
|
* 模板短信发送
|
* @param tpId 模板id
|
* @param records 模板变量
|
* "records":[
|
* {
|
* "mobile":"138****0000",
|
* "tpContent":{
|
* "var1":"变量1",
|
* "var2":"变量2"
|
* }
|
* },
|
* {
|
* "mobile":"138****0001",
|
* "tpContent":{
|
* "var1":"变量2",
|
* "var2":"变量2"
|
* }
|
* }
|
* ]
|
*/
|
public static void sendSmsTp(Long tpId, JSONArray records){
|
//地址
|
String urls = "https://api.mix2.zthysms.com/v2/sendSmsTp";
|
//请求入参
|
JSONObject requestJson = new JSONObject();
|
//账号
|
requestJson.put("username", username);
|
//tKey
|
long tKey = System.currentTimeMillis() / 1000;
|
requestJson.put("tKey", tKey);
|
//明文密码
|
requestJson.put("password", SecureUtil.md5(SecureUtil.md5(password) + tKey));
|
//模板ID
|
requestJson.put("tpId", tpId);
|
//签名
|
requestJson.put("signature", signature);
|
//扩展号
|
requestJson.put("ext", "");
|
//自定义参数
|
requestJson.put("extend", "");
|
requestJson.put("records", records);
|
String result = HttpRequest.post(urls)
|
.timeout(60000)
|
.body(requestJson.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE).execute().body();
|
if(ToolUtil.isNotEmpty(result)){
|
JSONObject jsonObject = JSON.parseObject(result);
|
int code = jsonObject.getIntValue("code");
|
if(200 != code){
|
System.err.println(jsonObject.getString("msg"));
|
}
|
}else{
|
System.err.println("短信发送异常");
|
}
|
}
|
|
}
|