From 4f461bb6738c8ced039ee51e2e1f201c7ea6671f Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Thu, 17 Sep 2020 21:14:00 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=96=B0=E5=A2=9E=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/sms/Sms.php | 211 +++++++++++++++++- catch/sms/SmsService.php | 23 ++ catch/sms/controller/Config.php | 89 ++++++++ catch/sms/controller/Template.php | 76 +++++++ .../migrations/20200916172849_sms_config.php | 43 ++++ .../20200917081331_sms_template.php | 43 ++++ catch/sms/database/seeds/SmsMenusSeed.php | 79 +++++++ catch/sms/model/SmsConfig.php | 141 ++++++++++++ catch/sms/model/SmsTemplate.php | 37 +++ catch/sms/model/search/SmsTemplateSearch.php | 9 + catch/sms/module.json | 15 ++ catch/sms/route.php | 18 ++ 12 files changed, 782 insertions(+), 2 deletions(-) create mode 100644 catch/sms/SmsService.php create mode 100644 catch/sms/controller/Config.php create mode 100644 catch/sms/database/migrations/20200916172849_sms_config.php create mode 100644 catch/sms/database/migrations/20200917081331_sms_template.php create mode 100644 catch/sms/database/seeds/SmsMenusSeed.php create mode 100644 catch/sms/model/SmsConfig.php create mode 100644 catch/sms/model/SmsTemplate.php create mode 100644 catch/sms/module.json create mode 100644 catch/sms/route.php diff --git a/catch/sms/Sms.php b/catch/sms/Sms.php index e34a55a..6319556 100644 --- a/catch/sms/Sms.php +++ b/catch/sms/Sms.php @@ -8,12 +8,219 @@ // +---------------------------------------------------------------------- // | 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); + } } \ No newline at end of file diff --git a/catch/sms/SmsService.php b/catch/sms/SmsService.php new file mode 100644 index 0000000..155028b --- /dev/null +++ b/catch/sms/SmsService.php @@ -0,0 +1,23 @@ +model = $model; + } + + /** + * 列表 + * + * @time 2020/09/16 17:28 + * + * @return \think\Response + */ + public function index() + { + return CatchResponse::paginate($this->model->getList()); + } + + /** + * 保存 + * + * @time 2020/09/16 17:28 + * @param Request Request + * @return \think\Response + */ + public function save(Request $request) + { + return CatchResponse::success($this->model->storeBy($request->param())); + } + + /** + * 读取 + * + * @time 2020/09/16 17:28 + * @param $id + * @return \think\Response + */ + public function read($id) + { + return CatchResponse::success($this->model->findBy($id)); + } + + /** + * 更新 + * + * @time 2020/09/16 17:28 + * @param Request $request + * @return \think\Response + */ + public function update(Request $request, $id) + { + return CatchResponse::success($this->model->updateBy($id, $request->post())); + } + + /** + * 删除 + * + * @time 2020/09/16 17:28 + * @param $id + * @return \think\Response + */ + public function delete($id) + { + return CatchResponse::success($this->model->deleteBy($id)); + } + + +} \ No newline at end of file diff --git a/catch/sms/controller/Template.php b/catch/sms/controller/Template.php index ef90e0a..17a3a9f 100644 --- a/catch/sms/controller/Template.php +++ b/catch/sms/controller/Template.php @@ -8,3 +8,79 @@ // +---------------------------------------------------------------------- // | Author: JaguarJack [ njphper@gmail.com ] // +---------------------------------------------------------------------- +namespace catchAdmin\sms\controller; + +use catcher\base\CatchRequest as Request; +use catcher\CatchResponse; +use catcher\base\CatchController; +use catchAdmin\sms\model\SmsTemplate as SmsTemplateModel; + +class Template extends CatchController +{ + protected $model; + + public function __construct(SmsTemplateModel $model) + { + $this->model = $model; + } + + /** + * 列表 + * + * @time 2020/09/16 17:28 + * + * @return \think\Response + */ + public function index() + { + return CatchResponse::success($this->model->getList()); + } + + /** + * 保存 + * + * @time 2020/09/16 17:28 + * @param Request Request + * @return \think\Response + */ + public function save(Request $request) + { + return CatchResponse::success($this->model->storeBy($request->param())); + } + + /** + * 读取 + * + * @time 2020/09/16 17:28 + * @param $id + * @return \think\Response + */ + public function read($id) + { + return CatchResponse::success($this->model->findBy($id)); + } + + /** + * 更新 + * + * @time 2020/09/16 17:28 + * @param Request $request + * @return \think\Response + */ + public function update(Request $request, $id) + { + return CatchResponse::success($this->model->updateBy($id, $request->post())); + } + + /** + * 删除 + * + * @time 2020/09/16 17:28 + * @param $id + * @return \think\Response + */ + public function delete($id) + { + return CatchResponse::success($this->model->deleteBy($id)); + } +} \ No newline at end of file diff --git a/catch/sms/database/migrations/20200916172849_sms_config.php b/catch/sms/database/migrations/20200916172849_sms_config.php new file mode 100644 index 0000000..1aa0922 --- /dev/null +++ b/catch/sms/database/migrations/20200916172849_sms_config.php @@ -0,0 +1,43 @@ +table('sms_config', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => 'sms 配置' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]); + $table->addColumn('name', 'string', ['limit' => 50,'null' => true,'signed' => false,'comment' => '运营商名称',]) + ->addColumn('pid', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => false,'default' => 0,'signed' => false,'comment' => '父级ID',]) + ->addColumn('key', 'string', ['limit' => 100,'null' => false,'default' => '','signed' => false,'comment' => 'key',]) + ->addColumn('value', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => 'value',]) + ->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',]) + ->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',]) + ->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',]) + ->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',]) + ->create(); + } +} diff --git a/catch/sms/database/migrations/20200917081331_sms_template.php b/catch/sms/database/migrations/20200917081331_sms_template.php new file mode 100644 index 0000000..6ea98ca --- /dev/null +++ b/catch/sms/database/migrations/20200917081331_sms_template.php @@ -0,0 +1,43 @@ +table('sms_template', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '短信模版' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]); + $table->addColumn('operator', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => '运营商',]) + ->addColumn('name', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => '模版名称',]) + ->addColumn('identify', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => '模版标识',]) + ->addColumn('code', 'string', ['limit' => 100,'null' => false,'default' => '','signed' => false,'comment' => '模版CODE',]) + ->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',]) + ->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',]) + ->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',]) + ->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',]) + ->create(); + } +} diff --git a/catch/sms/database/seeds/SmsMenusSeed.php b/catch/sms/database/seeds/SmsMenusSeed.php new file mode 100644 index 0000000..5eabce7 --- /dev/null +++ b/catch/sms/database/seeds/SmsMenusSeed.php @@ -0,0 +1,79 @@ +getPermissions(), 'permissions', 'parent_id'); + } + + protected function getPermissions() + { + return array ( + 0 => + array ( + 'id' => 113, + 'permission_name' => '短信管理', + 'parent_id' => 0, + 'level' => '', + 'route' => '/sms', + 'icon' => 'el-icon-s-promotion', + 'module' => 'sms', + 'creator_id' => 1, + 'permission_mark' => 'sms', + 'component' => 'layout', + 'redirect' => '', + 'keepalive' => 1, + 'type' => 1, + 'hidden' => 1, + 'sort' => 1, + 'created_at' => 1600229598, + 'updated_at' => 1600229598, + 'deleted_at' => 0, + 'children' => + array ( + 0 => + array ( + 'id' => 114, + 'permission_name' => '短信配置', + 'parent_id' => 113, + 'level' => '113', + 'route' => '/sms/config', + 'icon' => 'el-icon-copy-document', + 'module' => 'sms', + 'creator_id' => 1, + 'permission_mark' => 'sms', + 'component' => 'sms', + 'redirect' => '', + 'keepalive' => 1, + 'type' => 1, + 'hidden' => 1, + 'sort' => 1, + 'created_at' => 1600229654, + 'updated_at' => 1600229778, + 'deleted_at' => 0, + ), + ), + ), +); + } +} diff --git a/catch/sms/model/SmsConfig.php b/catch/sms/model/SmsConfig.php new file mode 100644 index 0000000..f6c85c5 --- /dev/null +++ b/catch/sms/model/SmsConfig.php @@ -0,0 +1,141 @@ +hasMany(SmsConfig::class, 'pid', 'id'); + } + + /** + * 保存 + * + * @time 2020年09月16日 + * @param array $data + * @return bool|int + */ + public function storeBy(array $data) + { + $config = $this->findByName($data['name']); + + if ($config) { + unset($data['name']); + $hasConfig = $config->hasConfig()->select(); + if (empty($hasConfig)) { + return $this->insertConfig($config->id, $data); + } + $this->deleteBy(array_column($hasConfig->toArray(), 'id'), true); + $this->insertConfig($config->id, $data); + return true; + } + + if (parent::storeBy([ + 'name' => $data['name'] + ])) { + unset($data['name']); + $this->insertConfig($this->id, $data); + return true; + } + } + + /** + * 新增配置 + * + * @time 2020年09月16日 + * @param $pid + * @param $data + * @return int + */ + protected function insertConfig($pid, $data) + { + $config = []; + + $creatorId = $data['creator_id']; + unset($data['creator_id']); + + foreach ($data as $k => $v) { + $config[] = [ + 'key' => $k, + 'value' => $v, + 'pid' => $pid, + 'creator_id' => $creatorId, + 'created_at' => time(), + 'updated_at' => time(), + ]; + } + + return $this->insertAll($config); + } + + /** + * 根据 name 查找 + * + * @time 2020年09月16日 + * @param $name + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return array|\think\Model|null + */ + public function findByName($name) + { + return $this->where('name', $name)->find(); + } + + /** + * 查找配置 + * + * @time 2020年09月16日 + * @param $id + * @param array|string[] $field + * @param false $trash + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return array|mixed + */ + public function findBy($id, array $field = ['*'], $trash = false) + { + $config = []; + + if (!$this->findByName($id)) { + return []; + } + + $this->findByName($id) + ->hasConfig() + ->select() + ->each(function ($item) use (&$config){ + $config[$item['key']] = $item['value']; + }); + + return $config; + } +} \ No newline at end of file diff --git a/catch/sms/model/SmsTemplate.php b/catch/sms/model/SmsTemplate.php new file mode 100644 index 0000000..7749801 --- /dev/null +++ b/catch/sms/model/SmsTemplate.php @@ -0,0 +1,37 @@ +where('operator', $value); + } +} \ No newline at end of file diff --git a/catch/sms/module.json b/catch/sms/module.json new file mode 100644 index 0000000..b5ae58f --- /dev/null +++ b/catch/sms/module.json @@ -0,0 +1,15 @@ +{ + "name": "短信模块", + "alias": "sms", + "description": "短信,sms,阿里大于", + "version": "1.0.0", + "keywords": [], + "order": 0, + "services": [ + "\\catchAdmin\\sms\\SmsService" + ], + "aliases": {}, + "files": [], + "requires": [], + "enable": true +} \ No newline at end of file diff --git a/catch/sms/route.php b/catch/sms/route.php new file mode 100644 index 0000000..08f5c3f --- /dev/null +++ b/catch/sms/route.php @@ -0,0 +1,18 @@ +group('sms', function () use ($router){ + // config路由 + $router->resource('config', '\catchAdmin\sms\controller\Config'); + // template 路由 + $router->resource('template', '\catchAdmin\sms\controller\Template'); +})->middleware('auth'); \ No newline at end of file