package com.xncoding.pos.utils;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.http.HttpRequest;
|
import com.xncoding.pos.config.ShellProperties;
|
import com.xncoding.pos.config.SmsProperties;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Value;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
|
/**
|
* 短信服务
|
*/
|
public class SmsUtils {
|
private static final Logger logger = LoggerFactory.getLogger(SmsUtils.class);
|
private static SmsProperties smsProperties;
|
|
public SmsUtils() {
|
if (smsProperties == null){
|
smsProperties = (SmsProperties)ApplicationContextUtils.getBean("smsProperties");
|
}
|
}
|
|
public void sendSms( boolean isSuccess) {
|
logger.info("是否发送短信{}",smsProperties.isSendSms());
|
if(!smsProperties.isSendSms()){return;}
|
//请求入参
|
JSONObject requestJson = new JSONObject();
|
//账号
|
requestJson.put("username", smsProperties.getUserName());
|
//tKey
|
long tKey = System.currentTimeMillis() / 1000;
|
requestJson.put("tKey", tKey);
|
//明文密码
|
requestJson.put("password", SecureUtil.md5(SecureUtil.md5(smsProperties.getPwd()) + tKey));
|
//模板ID
|
requestJson.put("tpId",isSuccess? smsProperties.getTpIdSuccess():smsProperties.getTpIdError());
|
//签名
|
requestJson.put("signature", smsProperties.getSignature());
|
//扩展号
|
requestJson.put("ext", "");
|
//自定义参数
|
requestJson.put("extend", "");
|
//发送记录集合
|
JSONArray records = new JSONArray();
|
JSONObject record = new JSONObject();
|
|
String[] split = smsProperties.getMobiles().split(";");
|
String now = DateUtil.now();
|
for (String mobile : split) {
|
//手机号
|
record.put("mobile", mobile);
|
//替换变量
|
JSONObject param = new JSONObject();
|
param.put("date",now);
|
record.put("tpContent", param);
|
records.add(record);
|
|
}
|
requestJson.put("records", records);
|
logger.info("短信请求:{}", requestJson);
|
String result = HttpRequest.post(smsProperties.getUrl())
|
.timeout(60000)
|
.body(requestJson.toJSONString(),"application/json;charset=UTF-8").execute().body();
|
logger.info("短信返回:{}", result);
|
|
}
|
|
}
|