puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
package com.sinata.rest.core.aliyun.sms;
 
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.sinata.common.keys.AliyunConfig;
import com.sinata.rest.core.aliyun.sms.model.AliSmsCodeParam;
import com.sinata.rest.core.aliyun.sms.model.AliSmsResponse;
import lombok.extern.slf4j.Slf4j;
 
/**
 * 阿里云短信接口
 * <dependency>
 * <groupId>com.aliyun</groupId>
 * <artifactId>aliyun-java-sdk-core</artifactId>
 * <version>4.0.3</version>
 * </dependency>
 */
@Slf4j
public class AliSmsApi {
 
    /**
     * 阿里云账号配置
     */
    private static final String accessKeyId = AliyunConfig.accessKeyId;
    private static final String accessSecret = AliyunConfig.accessKeySecret;
 
    // 签名
    private static final String signName = AliyunConfig.signName;
 
    // 短信验证码模板code
    public static final String templateCodeVerify = AliyunConfig.templateCodeVerify;
    public static final String templateCodeVerify_international = AliyunConfig.templateCodeVerify;
 
    /**
     * 发送短信验证码
     *
     * @param areaNo
     * @param phone
     * @param code
     * @return
     */
    public static String sendSms(String areaNo, String phone, String code) {
        // 默认发送短信验证码
        String templateCode = templateCodeVerify;
        if (isInternationalSms(areaNo)) {
            // 使用发送短信验证码(国际/港澳台)
            templateCode = templateCodeVerify_international;
        }
        return send(areaNo, phone, JSON.toJSONString(new AliSmsCodeParam(code)), templateCode);
    }
 
    /**
     * 发送短信验证码、短信通知
     *
     * @param areaNo        区号,非86则发送国际/港澳台短信
     * @param phone         手机号
     * @param templateParam 短信参数
     * @param templateCode  短信模板
     */
    public static String send(String areaNo, String phone, String templateParam, String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
 
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
 
        if (isInternationalSms(areaNo)) {
            // 国际/港澳台消息:国际区号+号码,例如85200000000
            request.putQueryParameter("PhoneNumbers", areaNo + phone);
        } else {
            // 国内短信:11位手机号码,例如15951955195
            request.putQueryParameter("PhoneNumbers", phone);
        }
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
 
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.debug(response.getData());
            AliSmsResponse data = JSON.parseObject(response.getData(), AliSmsResponse.class);
            return data.getMessage() == null ? "NO" : data.getMessage();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "NO";
    }
 
    /**
     * 判断是否使用(国际/港澳台)短信,默认(否)使用国内短信
     *
     * @return
     */
    public static boolean isInternationalSms(String areaNo) {
        if (areaNo != null && !"86".equals(areaNo)) {
            return true;
        } else {
            return false;
        }
    }
}