123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- // +----------------------------------------------------------------------
- // | CatchAdmin [Just Like ~ ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
- // +----------------------------------------------------------------------
- // | Author: JaguarJack [ njphper@gmail.com ]
- // +----------------------------------------------------------------------
- namespace catchAdmin\sms;
- use catchAdmin\sms\model\SmsConfig;
- use catcher\exceptions\FailedException;
- use Overtrue\EasySms\EasySms;
- use think\helper\Str;
- class Sms
- {
- /**
- * timeout http 请求时间
- *
- * @var int
- */
- protected $timeout = 5;
- /**
- * 错误日志
- *
- * @var string
- */
- protected $errorLog;
- /**
- * 网关
- *
- * @var array
- */
- protected $gateways = [];
- /**
- * 配置
- *
- * @var array
- */
- protected $config = [];
- /**
- * 发送数据
- *
- * @var array
- */
- protected $sendData = [];
- /**
- * Sms constructor.
- * @param $config
- */
- public function __construct(array $config)
- {
- $config['timeout'] = $this->timeout;
- $config['gateways']['errorlog'] = runtime_path('log') . 'sms.log';
- $this->config = $config;
- }
- /**
- * 发送
- *
- * @time 2020年09月17日
- * @param string $phone
- * @param array $data
- * @return mixed
- * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
- * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
- */
- public function send(string $phone, array $data)
- {
- try {
- $this->sendData['data'] = $data;
- return $this->easySms()
- ->send($phone, $this->sendData);
- } catch (\Exception $exception) {
- throw new FailedException($exception->getMessage());
- }
- }
- /**
- * easy sms
- *
- * @time 2020年09月17日
- * @return EasySms
- */
- public function easySms()
- {
- return new EasySms($this->config);
- }
- /**
- * 内容
- *
- * @time 2020年09月17日
- * @param $content
- * @param string $key
- * @return $this
- */
- public function content($content, $key = 'content')
- {
- $this->sendData[$key] = $content;
- return $this;
- }
- /**
- * 模版
- *
- * @time 2020年09月17日
- * @param $template
- * @param string $key
- * @return $this
- */
- public function template($template, $key = 'template')
- {
- $this->sendData[$key] = $template;
- return $this;
- }
- /**
- * 超时间时间 s
- *
- * @time 2020年09月17日
- * @param int $timeout
- * @return $this
- */
- public function timeout(int $timeout)
- {
- $this->config['timeout'] = $timeout;
- return $this;
- }
- /**
- * 记录记录地址
- *
- * @time 2020年09月17日
- * @param string $log
- * @return $this
- */
- public function errorLog(string $log)
- {
- $this->config['gateways']['errorlog'] = $log;
- return $this;
- }
- /**
- * gateways config
- *
- * @time 2020年09月17日
- * @param $gateways
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @return array
- */
- protected static function getGatewaysConfig($gateways)
- {
- $gatewaysConfig = [];
- $smsConfig = new SmsConfig();
- foreach ($gateways as $gate) {
- $c = $smsConfig->findByName($gate);
- if ($c) {
- $c->hasConfig()
- ->select()
- ->each(function ($item) use (&$gatewaysConfig, $gate){
- $gatewaysConfig[$gate][$item['key']] = $item['value'];
- });
- }
- }
- return $gatewaysConfig;
- }
- /**
- * sms
- *
- * @time 2020年09月17日
- * @param $method
- * @param $arg
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @return Sms
- */
- public static function __callStatic($method, $arg)
- {
- $gateways = Str::snake($method);
- if (Str::contains($gateways, '_')) {
- $gateways = explode('_', $gateways);
- } else {
- $gateways = [$gateways];
- }
- $config = [
- 'default' => [
- 'gateways' => $gateways,
- ],
- 'gateways' => static::getGatewaysConfig($gateways)
- ];
- return new self($config);
- }
- }
|