123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server\sms;
-
- use app\common\server\ConfigServer;
-
-
- class Driver
- {
- protected $config;
- protected $smsEngine;
- protected $defaultEngine;
- protected $error;
-
- public function getError()
- {
- return $this->error;
- }
-
-
- public function __construct()
- {
- $this->initialize();
- }
-
-
-
-
- public function initialize()
- {
- $defaultEngine = ConfigServer::get('sms_driver', 'default', '');
- if (empty($defaultEngine)) {
- throw new \Exception('请开启短信配置');
- }
- $this->defaultEngine = $defaultEngine;
-
- $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($defaultEngine.'Sms');
- if (!class_exists($classSpace)) {
- throw new \Exception('对应短信配置类不存在');
- }
-
- $engineConfig = ConfigServer::get('sms_engine', $defaultEngine, []);
- if (empty($engineConfig)) {
- throw new \Exception('请在后台设置好('.$defaultEngine.')的配置');
- }
- $this->smsEngine = new $classSpace($engineConfig);
- }
-
-
-
-
- public function send($mobile, $data)
- {
- try{
- $res = $this->smsEngine
- ->setMobile($mobile)
- ->setTemplateId($data['template_id'])
- ->setTemplateParam($data['param'])
- ->send();
-
- if (false === $res) {
- throw new \Exception($this->smsEngine->getError());
- }
- return $res;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
-
-
- }
|