wang-hao-jie
2021-10-19 e48043a2df9ca0c73fe18298bab3c4d42ca5c0c7
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
package cn.exrick.xboot.core.common.sms;
 
import cn.exrick.xboot.core.common.constant.SettingConstant;
import cn.exrick.xboot.core.common.exception.XbootException;
import cn.exrick.xboot.core.common.utils.NameUtil;
import cn.exrick.xboot.core.entity.Setting;
import cn.exrick.xboot.core.service.SettingService;
import cn.exrick.xboot.core.vo.SmsSetting;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Map;
import java.util.Set;
 
/**
 * @author exrick
 */
@Slf4j
@Component
public class TencentSms implements Sms {
 
    @Autowired
    private SettingService settingService;
 
    @Override
    public SmsSetting getSmsSetting() {
 
        Setting setting = settingService.get(SettingConstant.TENCENT_SMS);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            throw new XbootException("您还未配置腾讯云短信服务");
        }
        return new Gson().fromJson(setting.getValue(), SmsSetting.class);
    }
 
    @Override
    public void sendSms(String mobile, String params, String templateCode) {
 
        SmsSetting s = getSmsSetting();
        try {
            Credential cred = new Credential(s.getAccessKey(), s.getSecretKey());
            SmsClient client = new SmsClient(cred, "");
            SendSmsRequest req = new SendSmsRequest();
 
            // 短信应用 ID: 在 [短信控制台-应用管理] 添加应用后生成的实际 SDKAppID,例如1400006666
            req.setSmsSdkAppid(s.getAppId());
            // 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息
            req.setSign(s.getSignName());
            // 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID
            req.setTemplateID(templateCode);
            /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
             * 例如+8613800000000, 其中前面有一个+号 ,86为国家码,13800000000为手机号,最多不要超过200个手机号*/
            String[] phoneNumbers = {"+86" + mobile};
            req.setPhoneNumberSet(phoneNumbers);
            /* 模板参数: 若无模板参数,则设置为空*/
            req.setTemplateParamSet(getParams(params));
 
            SendStatus sendStatus = client.SendSms(req).getSendStatusSet()[0];
            if (!"Ok".equals(sendStatus.getCode())) {
                throw new XbootException("请求发送验证码失败," + sendStatus.getMessage());
            }
        } catch (TencentCloudSDKException e) {
            throw new XbootException("请求发送验证码失败,错误码:" + e.getErrorCode());
        }
    }
 
    public static String[] getPhoneNumbers(String mobile) {
 
        String[] phoneNumbers = mobile.split(",");
        for (int i = 0; i < phoneNumbers.length; i++) {
            String number = phoneNumbers[i];
            if (NameUtil.mobile(number)) {
                phoneNumbers[i] = "+86" + number;
            } else {
                phoneNumbers[i] = "+" + number;
            }
        }
        return phoneNumbers;
    }
 
    public static String[] getParams(String params) {
 
        Set<Map.Entry<String, JsonElement>> entries = JsonParser.parseString(params).getAsJsonObject().entrySet();
        String[] templateParams = new String[entries.size()];
        int i = 0;
        for (Map.Entry<String, JsonElement> entry : entries) {
            String value = entry.getValue().getAsString();
            templateParams[i] = value;
            i++;
        }
        return templateParams;
    }
}