add:新增短信平台管理

This commit is contained in:
JaguarJack 2020-09-17 21:14:00 +08:00
parent e30de13d35
commit 4f461bb673
12 changed files with 782 additions and 2 deletions

View File

@ -8,12 +8,219 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\sms; namespace catchAdmin\sms;
use catchAdmin\sms\model\SmsConfig;
use catcher\exceptions\FailedException;
use Overtrue\EasySms\EasySms;
use think\helper\Str;
class Sms 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);
}
}

23
catch/sms/SmsService.php Normal file
View File

@ -0,0 +1,23 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} 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 catcher\ModuleService;
class SmsService extends ModuleService
{
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
}

View File

@ -0,0 +1,89 @@
<?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\controller;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\sms\model\SmsConfig as SmsConfigModel;
class Config extends CatchController
{
protected $model;
public function __construct(SmsConfigModel $model)
{
$this->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));
}
}

View File

@ -8,3 +8,79 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | 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));
}
}

View File

@ -0,0 +1,43 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class SmsConfig extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}

View File

@ -0,0 +1,43 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class SmsTemplate extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}

View File

@ -0,0 +1,79 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Seeder;
class SmsMenusSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
\catcher\Utils::importTreeData($this->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,
),
),
),
);
}
}

View File

@ -0,0 +1,141 @@
<?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\model;
use catcher\base\CatchModel as Model;
class SmsConfig extends Model
{
protected $name = 'sms_config';
protected $field = [
'id', //
'name', // 运营商名称
'pid', // 父级ID
'key', // key
'value', // value
'creator_id', // 创建人ID
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
public function hasConfig()
{
return $this->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;
}
}

View File

@ -0,0 +1,37 @@
<?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\model;
use catchAdmin\sms\model\search\SmsTemplateSearch;
use catcher\base\CatchModel as Model;
class SmsTemplate extends Model
{
use SmsTemplateSearch;
protected $name = 'sms_template';
protected $field = [
'id', //
'operator', // 运营商
'name', // 模版名称
'identify', // 模版标识
'code', // 模版CODE
'creator_id', // 创建人ID
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
protected $paginate = false;
}

View File

@ -8,3 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\sms\model\search;
trait SmsTemplateSearch
{
public function searchOperatorAttr($query, $value, $data)
{
return $query->where('operator', $value);
}
}

15
catch/sms/module.json Normal file
View File

@ -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
}

18
catch/sms/route.php Normal file
View File

@ -0,0 +1,18 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
// you should use `$router`
$router->group('sms', function () use ($router){
// config路由
$router->resource('config', '\catchAdmin\sms\controller\Config');
// template 路由
$router->resource('template', '\catchAdmin\sms\controller\Template');
})->middleware('auth');