chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
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
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace app\common\service\sms\engine;
 
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\facade\Config;
 
/**
 * 腾讯云短信
 * Class TencentSms
 * @package app\common\service\sms\engine
 */
class TencentSms {
    protected $error = NULL;
    protected $config;
    protected $mobile;
    protected $templateId;
    protected $templateParams;
    
    public function __construct($config) {
        if (empty($config)) {
            $this->error = '请联系管理员配置参数';
            return FALSE;
        }
        $this->config = $config;
    }
    
    
    /**
     * @notes 设置手机号
     * @param $mobile
     */
    public function setMobile($mobile) {
        $this->mobile = $mobile;
        return $this;
    }
    
    
    /**
     * @notes 设置模板id
     * @param $templateId
     */
    public function setTemplateId($templateId) {
        $this->templateId = $templateId;
        return $this;
    }
    
    
    /**
     * @notes 设置模板参数
     * @param $templateParams
     */
    public function setTemplateParams($templateParams) {
        $this->templateParams = $templateParams;
        return $this;
    }
    
    /**
     * @notes 短信内容
     * @param $sceneContent
     */
    public function setSceneContent($sceneContent) {
        $this->sceneContent = $sceneContent;
        return $this;
    }
    
    /**
     * @notes 国家区号
     * @param $countryCode
     */
    public function setCountryCode($countryCode) {
        $this->countryCode = $countryCode;
        return $this;
    }
    
    
    /**
     * @notes 获取错误信息
     */
    public function getError() {
        return $this->error;
    }
    
    
    /**
     * @notes 发送短信
     */
    public function send() {
        try {
            $cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
            $httpProfile = new HttpProfile();
//            $httpProfile->setReqMethod("GET");
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");
            
            $clientProfile = new ClientProfile();
            $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法
            $clientProfile->setHttpProfile($httpProfile);
            
            $client = new SmsClient($cred, 'ap-guangzhou', $clientProfile);
            $req = new SendSmsRequest();
            $req->PhoneNumberSet = [$this->countryCode . $this->mobile];
            $req->SignName = $this->config['SignName'];
            $req->TemplateId = $this->templateId;
            $req->TemplateParamSet = $this->templateParams;
            $req->SmsSdkAppId = $this->config['SmsSdkAppid'];
            $req->SessionContext = "";
            $req->ExtendCode = "";
            $req->SenderId = "";
            $resp = json_decode($client->SendSms($req)->toJsonString(), TRUE);
            if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
                return $resp;
            } else {
                $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp);
                throw new \Exception('腾讯云短信错误:' . $message);
            }
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return FALSE;
        }
    }
}