修改登陆事件

This commit is contained in:
JaguarJack 2020-04-28 16:34:17 +08:00
parent 760ad2a51b
commit 435e1b6399
17 changed files with 954 additions and 37 deletions

View File

@ -2,6 +2,7 @@
namespace catchAdmin\login;
use catchAdmin\permissions\model\Users;
use catchAdmin\system\model\LoginLog;
use think\facade\Db;
class LoginLogEvent
@ -12,7 +13,7 @@ class LoginLogEvent
$username = Users::where('email', $params['email'])->value('username');
Db::name('login_log')->insert([
app(LoginLog::class)->storeBy([
'login_name' => $username ? : $params['email'],
'login_ip' => request()->ip(),
'browser' => $this->getBrowser($agent),

View File

@ -0,0 +1 @@
<?php

View File

@ -1,18 +0,0 @@
<?php
/**
* @filename Reply.php
* @createdAt 2020/2/13
* @project https://github.com/yanwenwu/catch-admin
* @document http://doc.catchadmin.com
* @author JaguarJack <njphper@gmail.com>
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
*/
namespace catchAdmin\wechat\controller;
use catcher\base\CatchController;
class Reply extends CatchController
{
}

View File

@ -1,13 +0,0 @@
{
"name": "",
"alias": "wechat",
"description": "",
"keywords": [],
"order": 0,
"services": [
""
],
"aliases": {},
"files": [],
"requires": []
}

View File

@ -1,5 +0,0 @@
<?php
// you should user `$router`
// $router->get('index', 'controller@method');

View File

@ -0,0 +1,25 @@
<?php
namespace JaguarJack\Generator;
use catcher\CatchAdmin;
use JaguarJack\Generator\Factory\Controller;
use JaguarJack\Generator\Factory\SQl;
class Generator
{
public function done($params)
{
$params = \json_decode($params['data'], true);
//var_dump((new Controller())->generate($params['controller']));die;
(new Controller())->done($params['controller']);
}
public function preview($type, $params)
{
$class = ucfirst($type);
(new $class)->done();
}
}

View File

@ -0,0 +1,140 @@
<?php
namespace JaguarJack\Generator\Factory;
use catcher\exceptions\FailedException;
use JaguarJack\Generator\Template\Controller as Template;
use think\helper\Str;
class Controller extends Factory
{
protected $methods = [];
/**
*
* @time 2020年04月27日
* @param $params
* @return bool|string|string[]
*/
public function done($params)
{
if (!$params['controller']) {
return false;
}
$template = new Template();
// parse controller
[$className, $namespace] = $this->parseFilename($params['controller']);
if (!$className) {
throw new FailedException('未填写控制器名称');
}
// parse model
[$model, $modelNamespace] = $this->parseFilename($params['model']);
$content = $template->header() .
$template->nameSpace($namespace) .
str_replace('{USE}', $model ? 'use ' . $params['model'] .';' : '', $template->uses()) .
$template->createClass($className);
$content = str_replace('{CONTENT}', ($model ? $template->construct($model) : '') . rtrim($this->content($params, $template), "\r\n"), $content);
// 写入成功之后
if (file_put_contents($this->getGeneratePath($params['controller']), $content)) {
(new Route())->controller($params['controller'])
->restful($params['restful'])
->methods($this->parseOtherMethods($params['other_function']))
->done();
}
}
/**
* content
*
* @time 2020年04月27日
* @param $params
* @param $template
* @return string
*/
protected function content($params, $template)
{
$content = '';
if ($params['restful']) {
$methods = $this->restful();
$this->methods = array_merge($this->methods, $methods);
foreach ($methods as $method) {
$content .= $template->{$method[0]}();
}
}
if (!empty($params['other_function'])) {
$others = $this->parseOtherMethods($params['other_function']);
$this->methods = array_merge($this->methods, $others);
foreach ($others as $other) {
$content .= $template->otherFunction($other[0], $other[1]);
}
}
return $content;
}
/**
* parse filename
*
* @time 2020年04月27日
* @param $filename
* @return array
*/
public function parseFilename($filename)
{
$namespace = explode('\\', $filename);
$className = ucfirst(array_pop($namespace));
$namespace = implode('\\', $namespace);
return [$className, $namespace];
}
/**
* parse $method
* class_method/http_method
* @time 2020年04月27日
* @param $methods
* @return false|string[]
*/
public function parseOtherMethods($methods)
{
$_methods = [];
foreach ($methods as $method) {
if (Str::contains($method, '/')) {
$_methods[] = explode('/', $method);
} else {
// 默认使用 Get 方式
$_methods[] = [$method, 'get'];
}
}
return $_methods;
}
/**
* restful 路由
*
* @time 2020年04月27日
* @return \string[][]
*/
public function restful()
{
return [
['index', 'get'],
['save', 'post'],
['read', 'get'],
['update', 'put'],
['delete', 'delete'],
];
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace JaguarJack\Generator\Factory;
use catcher\CatchAdmin;
abstract class Factory
{
abstract public function done($param);
/**
* parse psr4 path
*
* @time 2020年04月27日
* @return mixed
*/
public function parsePsr4()
{
$composer = \json_decode(file_get_contents(root_path() . 'composer.json'), true);
return $composer['autoload']['psr-4'];
}
/**
* get generate path
*
* @time 2020年04月27日
* @param $filePath
* @return string
*/
protected function getGeneratePath($filePath)
{
$path = explode('\\', $filePath);
$projectRootNamespace = array_shift($path);
$filename = array_pop($path);
$psr4 = $this->parsePsr4();
$filePath = root_path() . $psr4[$projectRootNamespace.'\\'] . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $path);
CatchAdmin::makeDirectory($filePath);
return $filePath . DIRECTORY_SEPARATOR . $filename . '.php';
}
/**
* 获取模块地址
*
* @time 2020年04月28日
* @param $filePath
* @return string
*/
public function getModulePath($filePath)
{
$path = explode('\\', $filePath);
$projectRootNamespace = array_shift($path);
$module = array_shift($path);
$psr4 = $this->parsePsr4();
return root_path() . $psr4[$projectRootNamespace.'\\'] . DIRECTORY_SEPARATOR. $module . DIRECTORY_SEPARATOR;
}
}

View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1,81 @@
<?php
namespace JaguarJack\Generator\Factory;
use JaguarJack\Generator\Template\Content;
class Route extends Factory
{
use Content;
protected $controllerName;
protected $controller;
protected $restful;
protected $methods = [];
public function done($params = [])
{
$route = [];
if ($this->restful) {
$route[] = sprintf("\$router->resource('%s', '\%s')", $this->controllerName, $this->controller);
}
if (!empty($this->methods)) {
foreach ($this->methods as $method) {
$route[] = sprintf("\$router->%s('%s/%s', '\%s@%s')", $method[1], $this->controllerName, $method[0], $this->controller, $method[0] );
}
}
return file_put_contents($this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php',
$this->header() . implode(';'. PHP_EOL , $route) . ';');
}
/**
* set class
*
* @time 2020年04月28日
* @param $class
* @return $this
*/
public function controller($class)
{
$this->controller = $class;
$class = explode('\\', $class);
$this->controllerName = lcfirst(array_pop($class));
return $this;
}
/**
* set restful
*
* @time 2020年04月28日
* @param $restful
* @return $this
*/
public function restful($restful)
{
$this->restful = $restful;
return $this;
}
/**
* set methods
*
* @time 2020年04月28日
* @param $methods
* @return $this
*/
public function methods($methods)
{
$this->methods = $methods;
return $this;
}
}

View File

@ -0,0 +1,151 @@
<?php
namespace JaguarJack\Generator\Factory;
class SQL
{
protected $index = '';
public function done($params)
{
if ($params['controller']['table'] ?? false) {
return false;
}
$table = \config('database.connections.mysql.prefix') . $params['controller']['table'];
$extra = $params['model']['extra'];
// 主键
$createSql = $this->primaryKey($extra['primary_key']);
// 字段
$ifHaveNotFields = true;
foreach ($params['model']['data'] as $sql) {
if (!$sql['field'] || !$sql['type']) {
continue;
}
$ifHaveNotFields = false;
$createSql .= $this->parseSQL($sql);
}
// 如果没有设置数据库字段
if ($ifHaveNotFields) {
return false;
}
// 创建时间
if ($extra['created_at'] ?? false) {
$createSql .= $this->parseCreatedAt();
}
// 软删除
if ($extra['soft_delete'] ?? false) {
$createSql .= $this->parseDeletedAt();
}
// 索引
if ($this->index) {
$createSql .= $this->index;
}
// 创建表 SQL
return $this->createTable($table, $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
}
/**
* parse sql
*
* @time 2020年04月27日
* @param $sql
* @return string
*/
protected function parseSQL($sql)
{
// 解析索引
if ($sql['index']) {
$this->parseIndex($sql['index'], $sql['field']);
}
return implode(' ', [
sprintf('`%s`', $sql['field']),
$sql['type'],
$sql['length'] ? sprintf('(%s)', $sql['length']) : '',
$sql['unsigned'] ? 'unsigned' : '',
$sql['default'] ? 'default ' . $sql['default']: '',
$sql['nullable'] ? 'not null' : '',
$sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : ''
]) . ','. PHP_EOL;
}
/**
* parse primary key
*
* @time 2020年04月27日
* @param $id
* @return string
*/
protected function primaryKey($id)
{
return sprintf('`%s`', $id) . ' int unsigned not null auto_increment primary key,'. PHP_EOL;
}
/**
* parse created_at & updated_at
*
* @time 2020年04月27日
* @return string
*/
protected function parseCreatedAt()
{
return sprintf('`created_at` int unsigned not null default 0 comment \'%s\',', '创建时间') . PHP_EOL .
sprintf('`updated_at` int unsigned not null default 0 comment \'%s\',', '更新时间') . PHP_EOL;
}
/**
* parse deleted_at
*
* @time 2020年04月27日
* @return string
*/
protected function parseDeletedAt()
{
return sprintf('`deleted_at` int unsigned not null default 0 comment \'%s\',', '软删除') . PHP_EOL;
}
/**
* created table
*
* @time 2020年04月27日
* @param $table
* @param $sql
* @param string $engine
* @param string $charset
* @param string $comment
* @return string
*/
protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '')
{
return sprintf('create table `%s`(' . PHP_EOL.
'%s)'.PHP_EOL .
'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment);
}
/**
* parse index
*
* @time 2020年04月27日
* @param $index
* @param $field
* @return void
*/
protected function parseIndex($index, $field)
{
if ($index == 'unique') {
$this->index .= "unique index unique_ . $field($field)";
} elseif ($index == 'index') {
$this->index .= "index($field)";
} elseif ($index == 'fulltext') {
$this->index .= "fulltext key fulltext_ .$field($field)";
} elseif ($index == 'spatial') {
$this->index .= "spatial index spatial_.$field($field)";
}
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace JaguarJack\Generator\Template;
trait Content
{
public function header()
{
$year = date('Y', time());
return <<<TMP
<?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 ]
// +----------------------------------------------------------------------
TMP;
}
/**
* set namespace
*
* @time 2020年04月27日
* @param $namespace
* @return string
*/
public function nameSpace($namespace)
{
return <<<TMP
namespace {$namespace};
TMP;
}
}

View File

@ -0,0 +1,246 @@
<?php
namespace JaguarJack\Generator\Template;
class Controller
{
use Content;
/**
* use
*
* @time 2020年04月27日
* @return string
*/
public function uses()
{
return <<<TMP
use think\Request;
use catcher\CatchResponse;
{USE}
TMP;
}
/**
* construct
*
* @time 2020年04月27日
* @param $model
* @return string
*/
public function construct($model)
{
return <<<TMP
protected \$model;
public function __construct({$model} \$model)
{
\$this->model = \$model;
}
TMP;
}
public function createClass($class)
{
return <<<TMP
class {$class}
{
{CONTENT}
}
TMP;
}
/**
* list template
*
* @time 2020年04月24日
* @return string
*/
public function index()
{
return <<<TMP
{$this->controllerFunctionComment('列表', 'Request $request')}
public function index(Request \$request)
{
return CatchResponse::paginate(\$this->model->getList(\$request->param()));
}
TMP;
}
/**
* create template
*
* @time 2020年04月24日
* @return string
*/
public function create()
{
return <<<TMP
{$this->controllerFunctionComment('单页')}
public function create()
{
//
}
TMP;
}
/**
* save template
*
* @time 2020年04月24日
* @param $createRequest
* @return string
*/
public function save($createRequest = '')
{
$request = $createRequest ? 'CreateRequest' : 'Request';
return <<<TMP
{$this->controllerFunctionComment('保存', 'Request ' . $request)}
public function save({$request} \$request)
{
return CatchResponse::success(\$this->model->storeBy(\$request->post()));
}
TMP;
}
/**
* read template
*
* @time 2020年04月24日
* @return string
*/
public function read()
{
return <<<TMP
{$this->controllerFunctionComment('读取', '$id')}
public function read(\$id)
{
return CatchResponse::success(\$this->model->findBy(\$id));
}
TMP;
}
/**
* edit template
*
* @time 2020年04月24日
* @return string
*/
public function edit()
{
return <<<TMP
{$this->controllerFunctionComment('编辑', '\$id')}
public function edit(\$id)
{
//
}
TMP;
}
/**
* update template
*
* @time 2020年04月24日
* @param $updateRequest
* @return string
*/
public function update($updateRequest = '')
{
$updateRequest = ($updateRequest ? 'UpdateRequest' : 'Request') . ' $request';
return <<<TMP
{$this->controllerFunctionComment('更新', $updateRequest)}
public function update({$updateRequest}, \$id)
{
return CatchResponse::success(\$this->model->updateBy(\$id, \$request->post()));
}
TMP;
}
/**
* delete template
*
* @time 2020年04月24日
* @return string
*/
public function delete()
{
return <<<TMP
{$this->controllerFunctionComment('删除', '$id')}
public function delete(\$id)
{
return CatchResponse::success(\$this->model->deleteBy(\$id));
}
TMP;
}
/**
* 其他方法
*
* @time 2020年04月27日
* @param $function
* @param string $method
* @return string
*/
public function otherFunction($function, $method = 'get')
{
$params = $method === 'delete' ? '$id' : 'Request $request';
return <<<TMP
{$this->controllerFunctionComment('', $params)}
public function {$function}({$params})
{
// todo
}
TMP;
}
/**
* 控制器方法注释
*
* @time 2020年04月24日
* @param $des
* @param $params
* @return string
*/
protected function controllerFunctionComment($des, $params = '')
{
$now = date('Y/m/d H:i', time());
$params = $params ? '@param ' . $params : '';
return <<<TMP
/**
* {$des}
*
* @time {$now}
* {$params}
* @return \\think\\Response
*/
TMP;
}
}

View File

@ -0,0 +1,155 @@
<?php
namespace JaguarJack\Generator\Template;
trait Model
{
use Content;
public function getList()
{
return <<<TMP
public function getList()
{
return \$this->catchSearch()
->order(\$this->getPk(), 'desc')
->paginate();
}
TMP;
}
/**
* 一对一关联
*
* @time 2020年04月24日
* @param $model
* @param string $foreignKey
* @param string $pk
* @return string
*/
public function hasOne($model, $foreignKey = '', $pk = '')
{
$func = lcfirst($model);
return <<<TMP
public function {$func}()
{
return \$this->hasOne({$model}::class{$this->keyRelate($foreignKey, $pk)});
}
TMP;
}
/**
*
*
* @time 2020年04月24日
* @param $model
* @param string $foreignKey
* @param string $pk
* @return string
*/
public function hasMany($model, $foreignKey = '', $pk = '')
{
$func = lcfirst($model);
return <<<TMP
public function {$func}()
{
return \$this->hasMany({$model}::class{$this->keyRelate($foreignKey, $pk)});
}
TMP;
}
/**
* 远程一对多
*
* @time 2020年04月24日
* @param $model
* @param $middleModel
* @param string $foreignKey
* @param string $pk
* @param string $middleRelateId
* @param string $middleId
* @return string
*/
public function hasManyThrough($model, $middleModel, $foreignKey = '', $pk = '', $middleRelateId = '', $middleId = '')
{
$func = lcfirst($model);
return <<<TMP
public function {$func}()
{
return \$this->hasManyThrough({$model}::class, {$middleModel}::class{$this->keyRelate($foreignKey, $pk, $middleRelateId, $middleId)});
}
TMP;
}
/**
* 远程一对一
*
* @time 2020年04月24日
* @param $model
* @param $middleModel
* @param string $foreignKey
* @param string $pk
* @param string $middleRelateId
* @param string $middleId
* @return string
*/
public function hasOneThrough($model, $middleModel, $foreignKey = '', $pk = '', $middleRelateId = '', $middleId = '')
{
$func = lcfirst($model);
return <<<TMP
public function {$func}()
{
return \$this->hasOneThrough({$model}::class, {$middleModel}::class{$this->keyRelate($foreignKey, $pk, $middleRelateId, $middleId)});
}
TMP;
}
/**
* 多对多关联
*
* @time 2020年04月24日
* @param $model
* @param string $table
* @param string $foreignKey
* @param string $relateKey
* @return string
*/
public function belongsToMany($model, $table = '', $foreignKey = '', $relateKey = '')
{
$func = lcfirst($model);
$table = !$table ? : ','.$table;
$relateKey = !$relateKey ? : ','.$relateKey;
return <<<TMP
public function {$func}()
{
return \$this->hasOneThrough({$model}::class{$table}{$this->keyRelate($foreignKey)}{$relateKey});
}
TMP;
}
/**
* 模型关联key
*
* @time 2020年04月24日
* @param string $foreignKey
* @param string $pk
* @param string $middleRelateId
* @param string $middleId
* @return string
*/
public function keyRelate($foreignKey = '', $pk = '', $middleRelateId = '', $middleId = '')
{
return !$foreignKey ? : ',' . $foreignKey .
!$middleRelateId ? : ','. $middleRelateId .
!$pk ? : ',' . $pk .
!$middleId ? : ',' . $middleId;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace JaguarJack\Generator\Template;
trait Request
{
/**
* 规则
*
* @time 2020年04月24日
* @return string
*/
public function rules()
{
return <<<EOT
public function rules(): array
{
return [
{rules}
];
}
EOT;
}
/**
* 消息
*
* @time 2020年04月24日
* @return string
*/
public function message()
{
return <<<EOT
public function message(): array
{
return [
{messages}
];
}
EOT;
}
}