inleft
2022-03-02 42b7d05dfdf60ee67c876084a3240ff48a3bf3a5
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.sms.modular.tencent;
26
27 import cn.hutool.core.util.ArrayUtil;
28 import com.tencentcloudapi.common.Credential;
29 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
30 import com.tencentcloudapi.common.profile.ClientProfile;
31 import com.tencentcloudapi.common.profile.HttpProfile;
32 import com.tencentcloudapi.sms.v20190711.SmsClient;
33 import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
34 import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
35 import com.tencentcloudapi.sms.v20190711.models.SendStatus;
36 import vip.xiaonuo.core.sms.SmsSender;
37 import vip.xiaonuo.core.sms.modular.tencent.exp.TencentSmsException;
38 import vip.xiaonuo.core.sms.modular.tencent.prop.TencentSmsProperties;
39
40 import java.util.Collection;
41 import java.util.LinkedList;
42 import java.util.Map;
43
44 /**
45  * 腾讯云短信发送
46  *
47  * @author xuyuxiang
48  * @date 2020/5/24 17:58
49  */
50 public class TencentSmsSender implements SmsSender {
51
52     private TencentSmsProperties tencentSmsProperties;
53
54     public TencentSmsSender(TencentSmsProperties tencentSmsProperties) {
55         this.tencentSmsProperties = tencentSmsProperties;
56     }
57
58     @Override
59     public void sendSms(String phone, String templateCode, Map<String, Object> params) {
60         try {
61
62             // 实例化一个认证对象
63             Credential cred = new Credential(
64                     tencentSmsProperties.getSecretId(), tencentSmsProperties.getSecretKey());
65
66             // 实例化一个 http 选项,可选,无特殊需求时可以跳过
67             HttpProfile httpProfile = new HttpProfile();
68             ClientProfile clientProfile = new ClientProfile();
69             clientProfile.setSignMethod("HmacSHA256");
70             clientProfile.setHttpProfile(httpProfile);
71
72             // 实例化 SMS 的 client 对象
73             SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
74
75             // 构建请求参数
76             SendSmsRequest req = new SendSmsRequest();
77
78             // 设置应用id
79             req.setSmsSdkAppid(tencentSmsProperties.getSdkAppId());
80
81             // 设置签名
82             req.setSign(tencentSmsProperties.getSign());
83
84             // 设置模板id
85             req.setTemplateID(templateCode);
86
87             // 默认发送一个手机短信
88             String[] phoneNumbers = {"+86" + phone};
89             req.setPhoneNumberSet(phoneNumbers);
90
91             // 模板参数
92             if (params != null && params.size() > 0) {
93                 LinkedList<String> strings = new LinkedList<>();
94                 Collection<Object> values = params.values();
95                 for (Object value : values) {
96                     strings.add(value.toString());
97                 }
98                 req.setTemplateParamSet(ArrayUtil.toArray(strings, String.class));
99             }
100
101             SendSmsResponse res = client.SendSms(req);
102
103             SendStatus[] sendStatusSet = res.getSendStatusSet();
104             if (sendStatusSet != null && sendStatusSet.length > 0) {
105                 if (!sendStatusSet[0].getCode().equals("Ok")) {
106                     throw new TencentSmsException(sendStatusSet[0].getCode(), sendStatusSet[0].getMessage());
107                 }
108             }
109         } catch (TencentCloudSDKException e) {
110             throw new TencentSmsException("500", e.getMessage());
111         }
112     }
113 }