Pu Zhibing
2 天以前 e5bdd46a51ebfda768262ff51c17879241ea2b14
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.supersavedriving.user.modular.system.util.qianyuntong;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.open.common.util.OpenApiClient;
import com.open.common.util.SystemParameterNames;
import com.supersavedriving.user.modular.system.util.SpringContextsUtil;
import com.supersavedriving.user.modular.system.util.qianyuntong.model.SendSmsRequest;
import lombok.extern.slf4j.Slf4j;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
/**
 * 短信工具类
 * @author zhibing.pu
 * @Date 2025/6/10 10:48
 */
@Slf4j
public class SMSUtil {
    
    private static QianYunTongConfig qianYunTongConfig = SpringContextsUtil.getBean(QianYunTongConfig.class).getQianYunTongConfig();
    
    
    /**
     * 获取易信验证码
     * @param mobile
     * @return
     */
    public static String sendVerifyCode(String mobile) {
        //请求路径
        String url = qianYunTongConfig.getApiUrl() + "/openapi/rest/1.0/sendVerifyCode";
        //私钥文件
        String skprivateKeyFile = qianYunTongConfig.getPrivateKeyPath();
        //注意:私钥文件需要开发者手动新建.pem文件,将委办局提供的私钥串复制进文件里用于sign加密
        String appKey = qianYunTongConfig.getAppkey();//appkey
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String, Object> contentMap = new HashMap<String, Object>();
        Date nowdate = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
        
        String timeStamp = date.format(nowdate);
        String messageId = UUID.randomUUID().toString().replaceAll("-", "");
        contentMap.put(SystemParameterNames.getAppKey(), appKey);
        contentMap.put(SystemParameterNames.getMessage_id(), messageId);
        contentMap.put(SystemParameterNames.getUserName(), qianYunTongConfig.getUserName());
        contentMap.put(SystemParameterNames.getStatus(), qianYunTongConfig.getStatus());
        contentMap.put("content", "{\"mobile\":\"" + mobile + "\"}");
        log.info("【获取易信验证码】请求地址:" + url);
        log.info("【获取易信验证码】请求参数:" + JSON.toJSONString(contentMap));
        String result = OpenApiClient.sendCommonHttpRequst(url, headers, "POST", skprivateKeyFile, timeStamp, contentMap);
        log.info("【获取易信验证码】请求结果:" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        String retCode = jsonObject.getString("retCode");
        if (!"0".equals(retCode)) {
            log.error("【获取易信验证码】请求失败:" + result);
            throw new RuntimeException("【获取易信验证码】请求失败:" + result);
        }
        JSONObject object = jsonObject.getJSONObject("object");
        String status = object.getString("status");
        if (!"0".equals(status)) {
            log.error("【获取易信验证码】失败:" + object.toJSONString());
            throw new RuntimeException("【获取易信验证码】失败:" + object.toJSONString());
        }
        return object.getString("code");
    }
    
    public static void main(String[] args) {
        SendSmsRequest request = new SendSmsRequest();
        request.setDestAddress("15828353127");
        request.setTemplateId("TPL202507300002");
        Map<String, String> templateParams = new HashMap<>();
//        templateParams.put("code", "1234");
        request.setTemplateParams(templateParams);
//        request.setCode("code");
        request.setSpId("Y86asr7J");
        SMSUtil.sendSms(request);
    }
    
    /**
     * 根据模板发送短信
     *
     * @param request
     * @return
     */
    public static Boolean sendSms(SendSmsRequest request) {
        //请求路径
        String url = qianYunTongConfig.getApiUrl() + "/openapi/rest/1.0/sendSmsByTpl";
        //私钥文件
        String skprivateKeyFile = qianYunTongConfig.getPrivateKeyPath();
        //注意:私钥文件需要开发者手动新建.pem文件,将委办局提供的私钥串复制进文件里用于sign加密
        String appKey = qianYunTongConfig.getAppkey();//appkey
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String, Object> contentMap = new HashMap<String, Object>();
        Date nowdate = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
        
        String timeStamp = date.format(nowdate);
        String messageId = UUID.randomUUID().toString().replaceAll("-", "");
        contentMap.put(SystemParameterNames.getAppKey(), appKey);
        contentMap.put(SystemParameterNames.getMessage_id(), messageId);
        contentMap.put(SystemParameterNames.getUserName(), qianYunTongConfig.getUserName());
        contentMap.put(SystemParameterNames.getStatus(), qianYunTongConfig.getStatus());
        contentMap.put("content", new Gson().toJson(request));
        log.info("【根据模板发送短信】请求地址:" + url);
        log.info("【根据模板发送短信】请求参数:" + JSON.toJSONString(contentMap));
        String result = OpenApiClient.sendCommonHttpRequst(url, headers, "POST", skprivateKeyFile, timeStamp, contentMap);
        log.info("【根据模板发送短信】请求结果:" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        String retCode = jsonObject.getString("retCode");
        if (!"0".equals(retCode)) {
            log.error("【根据模板发送短信】请求失败:" + result);
            throw new RuntimeException("【根据模板发送短信】请求失败:" + result);
        }
        JSONObject object = jsonObject.getJSONObject("object");
        String status = object.getString("status");
        if (!"0".equals(status)) {
            log.error("【根据模板发送短信】失败:" + object.toJSONString());
            throw new RuntimeException("【根据模板发送短信】失败:" + object.toJSONString());
        }
        return true;
    }
}