医院废弃物信息化管理平台
“mitao”
2026-04-21 2f18b3a28054c3917010f95ba84f7425280a2622
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
package cn.besteam.modules.system.biz;
 
import cn.besteam.config.AliSmsConfig;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author: lizhiheng
 * @date: 2020-06-18
 * @since v1.0.0
 */
@Service
@Slf4j
public class AliSmsService {
 
    /**
     * 产品名称:云通信短信API产品,开发者无需替换
     */
    private static final String PRODUCT = "Dysmsapi";
 
    /**
     * 产品域名,开发者无需替换
     */
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
 
    @Resource
    private AliSmsConfig aliSmsConfig;
 
    /**
     * 发送验证码短信
     *
     * @param phone 手机号
     * @param code  验证码
     * @return
     */
    public boolean sendLoginCode(String phone, String code) {
 
        Map<String, String> param = new HashMap<>(3);
        param.put("code", code);
        return sendSms(phone, aliSmsConfig.getSignName(), aliSmsConfig.getLoginTemplateCode(), param);
    }
 
    /**
     * 发送通知短信
     *
     * @param phone 手机号
     * @param param 参数
     * @return
     */
    public boolean sendMessage(String phone, Map<String, String> param) {
        return sendSms(phone, aliSmsConfig.getSignName(), aliSmsConfig.getLoginTemplateCode(), param);
    }
 
 
    /**
     * 发送月报通知短信
     * @param phone
     * @param param
     * @return
     */
    public boolean sendMonthlySMS(String phone, Map<String, String> param){
        return sendSms(phone, aliSmsConfig.getSignName(), "SMS_246010544", param);
    }
 
 
    /**
     * 阿里发送短信
     *
     * @param phone
     * @param signName
     * @param templateCode
     * @param param
     * @return
     */
    private boolean sendSms(String phone, String signName, String templateCode, Map<String, String> param) {
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", aliSmsConfig.getConnectTimeout());
        System.setProperty("sun.net.client.defaultReadTimeout", aliSmsConfig.getReadTimeout());
        try {
            // 初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliSmsConfig.getAccessKeyId(), aliSmsConfig.getAccessKeySecret());
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            // 组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            // 必填:待发送手机号
            request.setPhoneNumbers(phone);
            // 必填:短信签名-可在短信控制台中找到
            request.setSignName(signName);
            // 必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(templateCode);
            // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            request.setTemplateParam(JSON.toJSONString(param));
            if (aliSmsConfig.isDebug()) {
                log.info("短信 DEBUG code= {}", param);
                return true;
            }
            // hint 此处可能会抛出异常,注意catch
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (StringUtils.equalsIgnoreCase("ok", sendSmsResponse.getCode())) {
                return true;
            } else {
                log.error("{}短信发送失败:{}", phone, sendSmsResponse.getMessage());
            }
        } catch (ClientException e) {
            throw new RuntimeException("短信发送失败", e);
        }
        return false;
    }
 
}