<?php
namespace addons\qiniusms\library;
use fast\Http;
use Overtrue\EasySms\EasySms;
/**
* 华为云短信类
*/
class Qiniusms
{
private $_params = [];
public $error = '';
protected $config = [];
protected $sms_config = [];
protected static $instance;
public function __construct($options = [])
{
if ($config = get_addon_config('qiniusms')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
if(!$this->sms_config){
$config = [];
$config['timeout'] = 5.0; // HTTP 请求的超时时间(秒)
// 默认发送配置
$config['default']["strategy"] = \Overtrue\EasySms\Strategies\OrderStrategy::class; // 网关调用策略,默认:顺序调用
$config['default']["gateways"] = [ "qiniu" ];
$config['gateways']["errorlog"]["file"] = "/tmp/easy-sms.log";
// 七牛云
$config['gateways']["qiniu"]["access_key"] = $this->config["key"];
$config['gateways']["qiniu"]["secret_key"] = $this->config["secret"];
$this->sms_config = $config;
}
}
/**
* 单例
* @param array $options 参数
* @return Hwsms
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* 设置签名
* @param string $sign
* @return Hwsms
*/
public function sign($sign = '')
{
$this->_params['signature'] = $sign;
return $this;
}
/**
* 设置参数
* @param array $param
* @return Hwsms
*/
public function param(array $param = [])
{
$this->_params['templateParas'] = $param;
// foreach ($param as $k => &$v) {
// $v = (string)$v;
// }
// unset($v);
// if ($param) {
// $this->_params['templateParas'] = json_encode(array_values($param));
// }
return $this;
}
/**
* 设置模板
* @param string $code 短信模板
* @return Hwsms
*/
public function template($code = '')
{
$this->_params['templateId'] = $code;
return $this;
}
/**
* 接收手机
* @param string $mobile 手机号码
* @return Hwsms
*/
public function mobile($mobile = '')
{
$this->_params['to'] = $mobile;
return $this;
}
/**
* 发送方
* @param string $code 国内短信填写为短信平台为短信签名分配的通道号码
* @return Hwsms
*/
public function from($code = '')
{
$this->_params['from'] = $code;
return $this;
}
/**
* 立即发送
* @return boolean
*/
public function send()
{
$this->error = '';
$params = $this->_params();
$easySms = new EasySms($this->sms_config);
$mobile = $params["to"];
$template_id = $params["templateId"];
$parme = $params["templateParas"];
try{
$result = $easySms->send($mobile, [
'content' => '验证码6379,您正在进行身份验证,打死不要告诉别人哦!',
'template' => $template_id,
'data' => $parme,
]);
}catch (Exception $e){
$this->error = 'Network error';
}
if (config('app_debug')) {
\think\Log::record('qiniusms:' . $this->error);
}
return false;
}
/**
* 获取错误信息
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* 获取发送的参数信息
* @return array
*/
private function _params()
{
$params = array_merge([
'signature' => isset($this->config['sign']) ? $this->config['sign'] : '',
'from' => isset($this->config['sender']) ? $this->config['sender'] : '',
], $this->_params);
if (isset($params['to']) && $params['to']) {
$toArr = explode(',', $params['to']);
foreach ($toArr as $index => &$item) {
if (substr($item, 0, 1) !== '+') {
$item = '+86' . $item;
}
}
$params['to'] = implode(',', $toArr);
}
return $params;
}
}
评论0