<?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;
|
}
|
}
|
|
}
|