增加初始化admin账号密码命令行;修改部分声明;增加表注释生成关联的功能;

This commit is contained in:
augushong
2021-11-29 18:26:07 +08:00
parent 6f461fa6b6
commit 10046031e6
8 changed files with 157 additions and 74 deletions

View File

@@ -6,13 +6,13 @@ DEFAULT_TIMEZONE=Asia/Shanghai
[DATABASE]
TYPE=mysql
HOSTNAME=host.docker.internal
DATABASE=easyadmin
DATABASE=ulthon
USERNAME=root
PASSWORD=root
HOSTPORT=3306
CHARSET=utf8
DEBUG=true
PREFIX=ea_
PREFIX=ul_
[LANG]
default_lang=zh-cn

View File

@@ -128,10 +128,10 @@ class Admin extends AdminController
*/
public function password($id)
{
$this->checkPostRequest();
$row = $this->model->find($id);
empty($row) && $this->error('数据不存在');
if ($this->request->isAjax()) {
$this->checkPostRequest();
$post = $this->request->post();
$rule = [
'password|登录密码' => 'require',

View File

@@ -15,7 +15,7 @@ namespace app\common\command;
use app\admin\model\SystemNode;
use EasyAdmin\console\CliEcho;
use EasyAdmin\curd\BuildCurd;
use app\common\tools\BuildCurd;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
@@ -32,26 +32,6 @@ class Curd extends Command
->addOption('controllerFilename', 'c', Option::VALUE_REQUIRED, '控制器文件名', null)
->addOption('modelFilename', 'm', Option::VALUE_REQUIRED, '主表模型文件名', null)
#
->addOption('checkboxFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '复选框字段后缀', null)
->addOption('radioFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '单选框字段后缀', null)
->addOption('imageFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '单图片字段后缀', null)
->addOption('imagesFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '多图片字段后缀', null)
->addOption('fileFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '单文件字段后缀', null)
->addOption('filesFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '多文件字段后缀', null)
->addOption('dateFieldSuffix', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '时间字段后缀', null)
->addOption('switchFields', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '开关的字段', null)
->addOption('selectFileds', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '下拉的字段', null)
->addOption('editorFields', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '富文本的字段', null)
->addOption('sortFields', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '排序的字段', null)
->addOption('ignoreFields', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '忽略的字段', null)
#
->addOption('relationTable', 'r', Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联表名', null)
->addOption('foreignKey', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联外键', null)
->addOption('primaryKey', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联主键', null)
->addOption('relationModelFilename', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联模型文件名', null)
->addOption('relationOnlyFileds', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联模型中只显示的字段', null)
->addOption('relationBindSelect', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, '关联模型中的字段用于主表外键的表单下拉选择', null)
#
->addOption('force', 'f', Option::VALUE_REQUIRED, '强制覆盖模式', 0)
->addOption('delete', 'd', Option::VALUE_REQUIRED, '删除模式', 0)
->setDescription('一键curd命令服务');
@@ -64,39 +44,10 @@ class Curd extends Command
$controllerFilename = $input->getOption('controllerFilename');
$modelFilename = $input->getOption('modelFilename');
$checkboxFieldSuffix = $input->getOption('checkboxFieldSuffix');
$radioFieldSuffix = $input->getOption('radioFieldSuffix');
$imageFieldSuffix = $input->getOption('imageFieldSuffix');
$imagesFieldSuffix = $input->getOption('imagesFieldSuffix');
$fileFieldSuffix = $input->getOption('fileFieldSuffix');
$filesFieldSuffix = $input->getOption('filesFieldSuffix');
$dateFieldSuffix = $input->getOption('dateFieldSuffix');
$switchFields = $input->getOption('switchFields');
$selectFileds = $input->getOption('selectFileds');
$sortFields = $input->getOption('sortFields');
$ignoreFields = $input->getOption('ignoreFields');
$relationTable = $input->getOption('relationTable');
$foreignKey = $input->getOption('foreignKey');
$primaryKey = $input->getOption('primaryKey');
$relationModelFilename = $input->getOption('relationModelFilename');
$relationOnlyFileds = $input->getOption('relationOnlyFileds');
$relationBindSelect = $input->getOption('relationBindSelect');
$force = $input->getOption('force');
$delete = $input->getOption('delete');
$relations = [];
foreach ($relationTable as $key => $val) {
$relations[] = [
'table' => $relationTable[$key],
'foreignKey' => isset($foreignKey[$key]) ? $foreignKey[$key] : null,
'primaryKey' => isset($primaryKey[$key]) ? $primaryKey[$key] : null,
'modelFilename' => isset($relationModelFilename[$key]) ? $relationModelFilename[$key] : null,
'onlyFileds' => isset($relationOnlyFileds[$key]) ? explode(",", $relationOnlyFileds[$key]) : [],
'relationBindSelect' => isset($relationBindSelect[$key]) ? $relationBindSelect[$key] : null,
];
}
if (empty($table)) {
CliEcho::error('请设置主表');
@@ -108,23 +59,40 @@ class Curd extends Command
->setTable($table)
->setForce($force);
$columns = $build->getTableColumns();
$relations = [];
foreach ($columns as $field => $column) {
if (isset($column['formType']) && $column['formType'] == 'relation') {
$define = $column['define'];
if (!isset($define['table'])) {
CliEcho::error("关联字段{$field}没有设置关联表名称");
return false;
}
$relations[] = [
'table' => $define['table'],
'foreignKey' => $field,
'primaryKey' => $define['primaryKey'] ?? null,
'modelFilename' => $define['modelFilename'] ?? null,
'onlyFileds' => isset($define['onlyFileds']) ? explode("|", $define['onlyFileds']) : [],
'relationBindSelect' => $define['relationBindSelect'] ?? null,
];
}
}
!empty($controllerFilename) && $build = $build->setControllerFilename($controllerFilename);
!empty($modelFilename) && $build = $build->setModelFilename($modelFilename);
!empty($checkboxFieldSuffix) && $build = $build->setCheckboxFieldSuffix($checkboxFieldSuffix);
!empty($radioFieldSuffix) && $build = $build->setRadioFieldSuffix($radioFieldSuffix);
!empty($imageFieldSuffix) && $build = $build->setImageFieldSuffix($imageFieldSuffix);
!empty($imagesFieldSuffix) && $build = $build->setImagesFieldSuffix($imagesFieldSuffix);
!empty($fileFieldSuffix) && $build = $build->setFileFieldSuffix($fileFieldSuffix);
!empty($filesFieldSuffix) && $build = $build->setFilesFieldSuffix($filesFieldSuffix);
!empty($dateFieldSuffix) && $build = $build->setDateFieldSuffix($dateFieldSuffix);
!empty($switchFields) && $build = $build->setSwitchFields($switchFields);
!empty($selectFileds) && $build = $build->setSelectFileds($selectFileds);
!empty($sortFields) && $build = $build->setSortFields($sortFields);
!empty($ignoreFields) && $build = $build->setIgnoreFields($ignoreFields);
foreach ($relations as $relation) {
$build = $build->setRelation($relation['table'], $relation['foreignKey'], $relation['primaryKey'], $relation['modelFilename'], $relation['onlyFileds'],$relation['relationBindSelect']);
$build = $build->setRelation($relation['table'], $relation['foreignKey'], $relation['primaryKey'], $relation['modelFilename'], $relation['onlyFileds'], $relation['relationBindSelect']);
}
$build = $build->render();
@@ -132,7 +100,7 @@ class Curd extends Command
if (!$delete) {
$result = $build->create();
if($force){
if ($force) {
$output->info(">>>>>>>>>>>>>>>");
foreach ($fileList as $key => $val) {
$output->info($key);
@@ -169,6 +137,4 @@ class Curd extends Command
return false;
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace app\common\command\admin;
use app\admin\model\SystemAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class ResetPassword extends Command
{
protected function configure()
{
// 指令配置
$this->setName('admin:resetPassword')
->setDescription('the admin:resetPassword command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('admin:resetPassword');
$model_admin = SystemAdmin::where('username', 'admin')->find();
if (empty($model_admin)) {
$output->writeln('管理员不存在');
return false;
}
$model_admin->save([
'password' => password(123456)
]);
$output->writeln('修改成功');
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace app\common\tools;
use EasyAdmin\curd\BuildCurd as CurdBuildCurd;
class BuildCurd extends CurdBuildCurd
{
/**
* 表单类型
* @var array
*/
protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation'];
public function getTableColumns()
{
return $this->tableColumns;
}
/**
* 构建初始化字段信息
* @param $colum
* @return mixed
*/
protected function buildColum(&$colum)
{
$string = $colum['comment'];
// 处理定义类型
preg_match('/{[\s\S]*?}/i', $string, $formTypeMatch);
if (!empty($formTypeMatch) && isset($formTypeMatch[0])) {
$colum['comment'] = str_replace($formTypeMatch[0], '', $colum['comment']);
$formType = trim(str_replace('}', '', str_replace('{', '', $formTypeMatch[0])));
if (in_array($formType, $this->formTypeArray)) {
$colum['formType'] = $formType;
}
}
// 处理默认定义
preg_match('/\([\s\S]*?\)/i', $string, $defineMatch);
if (!empty($formTypeMatch) && isset($defineMatch[0])) {
$colum['comment'] = str_replace($defineMatch[0], '', $colum['comment']);
if (isset($colum['formType']) && in_array($colum['formType'], ['images', 'files', 'select', 'switch', 'radio', 'checkbox', 'date', 'relation'])) {
$define = str_replace(')', '', str_replace('(', '', $defineMatch[0]));
if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox', 'relation'])) {
$formatDefine = [];
$explodeArray = explode(',', $define);
foreach ($explodeArray as $vo) {
$voExplodeArray = explode(':', $vo);
if (count($voExplodeArray) == 2) {
$formatDefine[trim($voExplodeArray[0])] = trim($voExplodeArray[1]);
}
}
!empty($formatDefine) && $colum['define'] = $formatDefine;
} else {
$colum['define'] = $define;
}
}
}
$colum['comment'] = trim($colum['comment']);
return $colum;
}
}

View File

@@ -2,11 +2,15 @@
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
use app\common\command\admin\ResetPassword;
return [
// 指令定义
'commands' => [
'curd' => 'app\common\command\Curd',
'node' => 'app\common\command\Node',
'OssStatic' => 'app\common\command\OssStatic',
ResetPassword::class
],
];

View File

@@ -24,7 +24,7 @@ return [
// 服务器地址
'hostname' => Env::get('database.hostname', 'host.docker.internal'),
// 数据库名
'database' => Env::get('database.database', 'easyadmin'),
'database' => Env::get('database.database', 'ulthon'),
// 用户名
'username' => Env::get('database.username', 'root'),
// 密码
@@ -36,7 +36,7 @@ return [
// 数据库编码默认采用utf8
'charset' => Env::get('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'ea_'),
'prefix' => Env::get('database.prefix', 'ul_'),
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,