liujie
2025-06-16 5af3d2fa829a7a454c8ddbb6ffb2a7685ceb9c77
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.panzhihua.westcommittee.utils;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
 
@Slf4j
public class AliSmsUtil {
    private static final String ACCESS_KEY_ID = "LTAI5t8EwfjWRWs7tJGimAQG";
    private static final String ACCESS_KEY_SECRET = "UjuTDcXO14dSQ7STg8Pf3idjLqwx1M";
    private static final String SIGN_NAME = "攀枝花市西区玉泉街道";
    // 管理员模板
    private static final String TIME_OUT_TEMPLATE_CODE_ADMIN = "SMS_488715181";
    // 纪委模板
    private static final String TIME_OUT_TEMPLATE_CODE = "SMS_488700190";
 
    // 分派模板
    private static final String ASSIGN_TEMPLATE_CODE = "SMS_488670038";
 
    // 办结模板
    private static final String SUCCESS_TEMPLATE_CODE = "SMS_488650016";
 
 
    private static final String EXPIRE_TEMPLATE_CODE = "SMS_479510160";
    /**
     * 发送诉求超时提醒短信
     * @param phoneList
     * @param title
     */
    public static void  sendTimeoutMessage(List<String> phoneList, String title) {
 
        Map<String, String> param = new HashMap<>(3);
        param.put("nomber", title);
        sendBatchSms(phoneList, SIGN_NAME, TIME_OUT_TEMPLATE_CODE, param);
    }
    public static void  sendTimeoutMessageAdmin(List<String> phoneList, String title) {
 
        Map<String, String> param = new HashMap<>(3);
        param.put("nomber", title);
        sendBatchSms(phoneList, SIGN_NAME, TIME_OUT_TEMPLATE_CODE_ADMIN, param);
    }
 
    public static void  sendAssignMessageAdmin(List<String> phoneList, String title) {
        Map<String, String> param = new HashMap<>(3);
        sendBatchSms(phoneList, SIGN_NAME, ASSIGN_TEMPLATE_CODE, param);
    }
 
    /**
     * 发送诉求临期短信
     * @param phoneList
     * @param title
     * @param closingTime
     * @param days
     */
    public static void sendExpireMessage(List<String> phoneList, String title, Date closingTime, Integer days) {
        Map<String, String> param = new HashMap<>(3);
        param.put("title", title);
        param.put("time", DateUtil.format(closingTime, "yyyy-MM-dd HH:mm:ss"));
        param.put("times", String.valueOf(days));
        sendBatchSms(phoneList, SIGN_NAME, EXPIRE_TEMPLATE_CODE, param);
    }
 
    public static void sendSuccessMessage(String phone) {
        Map<String, String> param = new HashMap<>(3);
        sendSms(phone,SIGN_NAME,SUCCESS_TEMPLATE_CODE,param);
    }
 
    public static void main(String[] args) {
        sendSuccessMessage("19915944902");
    }
 
    /**
     * 阿里发送短信
     *
     * @param phone
     * @param signName
     * @param templateCode
     * @param param
     * @return
     */
    private static boolean sendSms(String phone, String signName, String templateCode, Map<String, String> param) {
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
        System.setProperty("sun.net.client.defaultReadTimeout", "30000");
        try {
 
            // 初始化请求客户端
            Client client = createClient();
 
            // 构造请求对象,请填入请求参数值
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(signName)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(JSON.toJSONString(param));
            //if (aliSmsConfig.isDebug()) {
            //    log.info("短信 DEBUG code= {}", param);
            //    return true;
            //}
            // 获取响应对象
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            // hint 此处可能会抛出异常,注意catch
            if (StringUtils.equalsIgnoreCase("ok", sendSmsResponse.getBody().getCode())) {
                log.info("{}短信发送成功:{}", phone, sendSmsResponse.getBody().getMessage());
                return true;
            } else {
                log.error("{}短信发送失败:{}", phone, sendSmsResponse.getBody().getMessage());
            }
        } catch (Exception e) {
            log.error("{}短信发送失败:{}", phone, e.getMessage(),e);
            throw new RuntimeException("短信发送失败", e);
        }
        return false;
    }
    private static boolean sendBatchSms(List<String> phones, String signName, String templateCode, Map<String, String> param) {
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
        System.setProperty("sun.net.client.defaultReadTimeout", "30000");
        try {
 
            // 初始化请求客户端
            Client client = createClient();
 
            // 构造请求对象,请填入请求参数值
            SendBatchSmsRequest request = new SendBatchSmsRequest()
                    .setPhoneNumberJson(JSON.toJSONString(phones)) // 手机号数组JSON
                    .setSignNameJson(JSON.toJSONString(Collections.nCopies(phones.size(), SIGN_NAME)))
                    .setTemplateCode(templateCode)
                    .setTemplateParamJson(JSON.toJSONString(Collections.nCopies(phones.size(), param))); // 参数数组JSON
            //if (aliSmsConfig.isDebug()) {
            //    log.info("短信 DEBUG code= {}", param);
            //    return true;
            //}
            // 获取响应对象
            SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSms(request);
            // hint 此处可能会抛出异常,注意catch
            if (StringUtils.equalsIgnoreCase("ok", sendBatchSmsResponse.getBody().getCode())) {
                return true;
            } else {
                log.error("{}短信发送失败:{}", JSON.toJSONString(phones), sendBatchSmsResponse.getBody().getMessage());
            }
        } catch (Exception e) {
            log.error("{}短信发送失败:{}", JSON.toJSONString(phones), e.getMessage(),e);
            throw new RuntimeException("短信发送失败", e);
        }
        return false;
    }
    public static Client createClient() throws Exception {
        Config config = new Config()
                // 配置 AccessKey ID
                .setAccessKeyId(ACCESS_KEY_ID)
                // 配置 AccessKey Secret
                .setAccessKeySecret(ACCESS_KEY_SECRET);
 
        // 配置 Endpoint
        config.endpoint = "dysmsapi.aliyuncs.com";
 
        return new Client(config);
    }
 
 
}