add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.support.sms;
 
import com.dobbinsoft.fw.core.exception.CoreExceptionDefinition;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.core.exception.ThirdPartServiceException;
import com.dobbinsoft.fw.support.properties.FwSMSProperties;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
 
/**
 * Created by rize on 2019/7/1.
 */
public class QCloudSMSClient implements SMSClient,InitializingBean {
 
    private SmsSingleSender sender;
 
    @Autowired
    private FwSMSProperties properties;
 
    private static final Logger logger = LoggerFactory.getLogger(QCloudSMSClient.class);
 
    @Override
    public void afterPropertiesSet() throws Exception {
        sender = new SmsSingleSender(properties.getQcloudAppId(), properties.getQcloudAppKey());
    }
 
    public SMSResult sendMsg(String phone, int templateId, String ...params) throws ServiceException {
        try {
            SmsSingleSenderResult smsSingleSenderResult = sender.sendWithParam("86", phone, templateId, params, properties.getQcloudSignature(), "", "");
            if (smsSingleSenderResult.result == 0) {
                SMSResult smsResult = new SMSResult();
                smsResult.setSucc(true);
                smsResult.setMsg("成功");
                return smsResult;
            } else {
                logger.info("[腾讯短信发送] 回复与预期不一致 result=" + smsSingleSenderResult.result + ";errMsg=" + smsSingleSenderResult.errMsg);
                throw new ThirdPartServiceException(smsSingleSenderResult.errMsg, smsSingleSenderResult.result);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            logger.error("[腾讯短信发送] 异常", e);
            throw new ThirdPartServiceException("腾讯云短信发送未知异常", CoreExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode());
        }
    }
 
 
    @Override
    public SMSResult sendRegisterVerify(String phone, String verifyCode) throws ServiceException {
        return sendMsg(phone, properties.getQcloudRegisterTemplateId(), verifyCode);
    }
 
    @Override
    public SMSResult sendBindPhoneVerify(String phone, String verifyCode) throws ServiceException {
        return sendMsg(phone, properties.getQcloudBindPhoneTemplateId(), verifyCode);
    }
 
    @Override
    public SMSResult sendResetPasswordVerify(String phone, String verifyCode) throws ServiceException {
        return sendMsg(phone, properties.getQcloudResetPasswordTemplateId(), verifyCode);
    }
 
    @Override
    public SMSResult sendAdminLoginVerify(String phone, String verifyCode) throws ServiceException {
        return sendMsg(phone, properties.getQcloudAdminLoginTemplateId(),verifyCode);
    }
}