全栈小学生 0e47055ccb v1.0.0-beta.1
2023-04-15 17:12:49 +08:00

101 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace extend\driver\sms;
use Exception;
use extend\exception\CommonException;
use extend\exception\MessageException;
/**
* 短信驱动
* Class SmsDriver
* @package app\common\driver\sms
*/
class SmsDriver
{
/**
* 错误信息
* @var
*/
protected $error = null;
/**
* 短信引擎
* @var
*/
protected $driver;
/**
* 初始化短信驱动
* SmsDriver constructor.
* @param $config
*/
public function __construct($config)
{
// 初始化
$this->initialize($config);
}
public function initialize($config)
{
try {
$classSpace = __NAMESPACE__ . '\\driver\\' . ucfirst(strtolower($config['sms_type'])) . 'Sms';
if (!class_exists($classSpace)) {
throw new CommonException(204003);
}
$this->driver = new $classSpace($config);
if(!is_null($this->driver->getError())) {
throw new CommonException($this->driver->getError());
}
return true;
} catch ( Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/**
* 获取短信错误
* @return null
*/
public function getError()
{
return $this->error;
}
/**
* 发送短信
* @param $mobile
* @param $data
* @return bool
*/
public function send($mobile, $data)
{
try {
// 开始发送
$result = $this->driver
->setMobile($mobile)
->setTemplateId($data['template_id'])
->setTemplateParams($data['params'])
->send();
if(false === $result) {
throw new MessageException($this->driver->getError());
}
return $result;
} catch( Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
}