15815213711
2022-02-14 1c3164c096d405471ad7bb50fce86e321157d8a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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);
 
    }
 
}