quanwei
2025-12-31 48d31672b4d88900080093cd1632f9d2eb978d4d
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
<?php
 
namespace app\common\library\sms;
 
use think\Exception;
 
/**
 * 短信通知模块驱动
 */
class Driver
{
    private $config;    // 配置信息
    private $engine;    // 当前短信引擎类
    private $engineName;    // 当前短信引擎名称
 
    /**
     * 构造方法
     */
    public function __construct($config)
    {
        // 配置信息
        $this->config = $config;
        // 当前引擎名称
        $this->engineName = $this->config['default'];
        // 实例化当前存储引擎
        $this->engine = $this->getEngineClass();
    }
 
    /**
     * 发送短信通知
     */
    public function sendSms($mobile, $template_code, $templateParams)
    {
        return $this->engine->sendSms($mobile, $template_code, $templateParams);
    }
 
    /**
     * 获取错误信息
     */
    public function getError()
    {
        return $this->engine->getError();
    }
 
    /**
     * 获取当前的存储引擎
     */
    private function getEngineClass()
    {
        $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($this->engineName);
        if (!class_exists($classSpace)) {
            throw new Exception('未找到存储引擎类: ' . $this->engineName);
        }
        return new $classSpace($this->config['engine'][$this->engineName]);
    }
 
}