doum
8 小时以前 59b1f0e9967902aa10f5e017d5a0bdfd1b60c9ea
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
package com.doumee.core.utils.aliyun;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.Map;
 
/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Rk
 * @create 2025/7/23 11:37
 */
@Service
@Slf4j
public class AliSmsService {
 
    private final static String ACCESS_KEY_ID = "LTAI5t835zTU4aaYpGHJCccJ";
    private final static String ACCESS_KEY_SECRET = "98sAF2NchWVuIzu62zcLq0Ns7LIQTp";
    private final static String SING_NAME = "南京三只鹤";
 
 
    public static void main(String[] args) {
        Map<String,Object> tempParam = new java.util.HashMap<>();
        tempParam.put("code","1234");
        AliSmsService.sendSms("15345690849","SMS_333770877", JSONObject.toJSONString(tempParam));
    }
 
 
    /**
     * 发短信模板(可群发)
     *
     * @param phone         订单详情
     * @param templateCode  模板code
     * @param templateParam 模板变量json字符串
     * @return 处理结果
     */
    public static String sendSms(String phone, String templateCode, String templateParam) {
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            final String product = "Dysmsapi";
            final String domain = "dysmsapi.aliyuncs.com";
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            request.setPhoneNumbers(phone);
            request.setSignName(SING_NAME);
            request.setTemplateCode(templateCode);
            request.setTemplateParam(templateParam);
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                log.info("短信发送成功:phone={}, template={}", phone, templateCode);
                return null;
            } else {
                String error = sendSmsResponse.getCode() + ":" + sendSmsResponse.getMessage();
                log.error("短信发送失败:phone={}, template={}, error={}", phone, templateCode, error);
                return error;
            }
        } catch (Exception e) {
            log.error("短信发送异常:phone={}, template={}, error={}", phone, templateCode, e.getMessage());
            return e.getMessage();
        }
    }
}