chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
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
<?php
namespace app\common\service\sms;
 
use app\common\service\sms\engine\TencentSms;
use think\facade\Config;
 
 
class SmsDriver
{
    /**
     * 错误信息
     * @var
     */
    protected $error = null;
 
    /**
     * 默认短信引擎
     * @var
     */
    protected $defaultEngine;
 
    /**
     * 短信引擎
     * @var
     */
    protected $engine;
    
    protected $config;
 
    /**
     * 架构方法
     * SmsDriver constructor.
     */
    public function __construct($options=[])
    {
        // 初始化
        $this->config['secret_id'] = isset($options['secret_id']) ? $options['secret_id'] : Config::get('qcloud.Qcloud.SecretId');
        $this->config['secret_key'] = isset($options['secret_key']) ? $options['secret_key'] : Config::get('qcloud.Qcloud.SecretKey');
        $this->config['SmsSdkAppid'] = isset($options['SmsSdkAppid']) ? $options['SmsSdkAppid'] : Config::get('qcloud.Qcloud.SmsSdkAppId');
        $this->config['SignName'] = isset($options['SignName']) ? $options['SignName'] : Config::get('qcloud.Qcloud.SignName');
        try {
            $this->engine = new TencentSms($this->config);
            if(!is_null($this->engine->getError())) {
                throw new \Exception($this->engine->getError());
            }
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * @notes 获取错误信息
     */
    public function getError()
    {
        return $this->error;
    }
 
 
    /**
     * @notes 发送短信
     * @param $mobile
     */
    public function send($mobile, $data)
    {
        try {
            // 开始发送
            $result = $this->engine
                ->setMobile($mobile)// 接收短信手机号
                ->setCountryCode($data['country_code'])// 国家区号
                ->setTemplateId($data['template_id'])//接收短信模板id
                ->setTemplateParams($data['params'])// 短信模板参数
                ->send();
            if(false === $result) {
                throw new \Exception($this->engine->getError());
            }
            return $result;
        } catch(\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    
}