From cf4fb3ff803b37333d47cf92af7315193dda38ad Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 8 Dec 2023 12:12:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE=E5=AD=97?= =?UTF-8?q?=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Components.php | 6 +++--- app/Admin/Controllers/KeywordController.php | 2 +- app/Models/Keyword.php | 13 +++++++------ app/Services/Admin/KeywordService.php | 12 ++++++------ .../2023_12_08_104022_create_keywords_table.php | 4 ++-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/Admin/Components.php b/app/Admin/Components.php index 8bb8ef5..cdb550f 100644 --- a/app/Admin/Components.php +++ b/app/Admin/Components.php @@ -11,13 +11,13 @@ class Components extends BaseRenderer { /** * 父级选择器 */ - public function parentControl($apiUrl = null, $name ='p_id', $label = null, $labelField = 'name', $valueField = 'id') + public function parentControl($apiUrl = null, $name ='parent_id', $label = null, $labelField = 'name', $valueField = 'id') { - return amisMake()->TreeSelectControl() + return amisMake()->TreeSelectControl()->source($apiUrl) ->name($name)->label($label ?? __('admin.components.parent_select')) ->showIcon(false) ->labelField($labelField) - ->valueField($valueField)->source($apiUrl); + ->valueField($valueField); } /** diff --git a/app/Admin/Controllers/KeywordController.php b/app/Admin/Controllers/KeywordController.php index a39ae2e..8b742b9 100644 --- a/app/Admin/Controllers/KeywordController.php +++ b/app/Admin/Controllers/KeywordController.php @@ -53,7 +53,7 @@ class KeywordController extends AdminController Components::make()->parentControl(admin_url('api/keywords/tree-list')), TextControl::make()->name('name')->label('名称')->required(true), TextControl::make()->name('key')->label('KEY')->required(true), - TextControl::make()->name('value')->label('值')->required(true), + TextControl::make()->name('value')->label('值'), amisMake()->NumberControl()->name('sort')->value(0)->min()->label('排序'), ]); } diff --git a/app/Models/Keyword.php b/app/Models/Keyword.php index d90ef30..df07701 100644 --- a/app/Models/Keyword.php +++ b/app/Models/Keyword.php @@ -11,9 +11,10 @@ class Keyword extends Model use HasFactory; use Filterable; - protected $fillable = ['name', 'key', 'value', 'p_id', 'p_key', 'path', 'sort', 'lv']; + protected $fillable = ['name', 'key', 'value', 'parent_id', 'parent_key', 'path', 'sort', 'lv']; - protected function serializeDate(\DateTimeInterface $date){ + protected function serializeDate(\DateTimeInterface $date) + { return $date->format('Y-m-d H:i:s'); } @@ -31,20 +32,20 @@ class Keyword extends Model } else { // 将层级设为父类目的层级 + 1 $keyword->lv = $keyword->parent->lv ++; - $keyword->p_key = $keyword->parent->key; + $keyword->parent_key = $keyword->parent->key; // 将 path 值设为父类目的 path 追加父类目 ID 以及最后跟上一个 - 分隔符 - $keyword->path = $keyword->parent->path.$keyword->p_id.'-'; + $keyword->path = $keyword->parent->path.$keyword->parent_id.'-'; } }); } public function parent() { - return $this->belongsTo(static::class, 'p_id'); + return $this->belongsTo(static::class, 'parent_id'); } public function children() { - return $this->hasMany(static::class, 'p_id'); + return $this->hasMany(static::class, 'parent_id'); } } diff --git a/app/Services/Admin/KeywordService.php b/app/Services/Admin/KeywordService.php index bef4f09..d0829c6 100644 --- a/app/Services/Admin/KeywordService.php +++ b/app/Services/Admin/KeywordService.php @@ -13,12 +13,12 @@ class KeywordService extends BaseService { protected string $modelName = Keyword::class; - public function parentIsChild($id, $p_id): bool + public function parentIsChild($id, $pid): bool { - $parent = $this->query()->find($p_id); + $parent = $this->query()->find($pid); do { - if ($parent->p_id == $id) { + if ($parent->parent_id == $id) { return true; } // 如果没有parent 则为顶级 退出循环 @@ -62,9 +62,9 @@ class KeywordService extends BaseService $columns = $this->getTableColumns(); - $p_id = Arr::get($data, 'p_id'); - if ($p_id != 0) { - if ($this->parentIsChild($primaryKey, $p_id)) { + $pid = Arr::get($data, 'parent_id'); + if ($pid != 0) { + if ($this->parentIsChild($primaryKey, $pid)) { $this->setError('父级不允许设置为当前子权限'); return false; } diff --git a/database/migrations/2023_12_08_104022_create_keywords_table.php b/database/migrations/2023_12_08_104022_create_keywords_table.php index 8ab3bff..8b87b43 100644 --- a/database/migrations/2023_12_08_104022_create_keywords_table.php +++ b/database/migrations/2023_12_08_104022_create_keywords_table.php @@ -17,8 +17,8 @@ return new class extends Migration $table->string('name')->comment('名字'); $table->string('value')->nullable(); $table->unsignedInteger('sort')->default(0)->comment('排序'); - $table->unsignedBigInteger('p_id')->default(0)->comment('上级ID'); - $table->string('p_key')->nullable('上级key'); + $table->unsignedBigInteger('parent_id')->default(0)->comment('上级ID'); + $table->string('parent_key')->nullable('上级key'); $table->unsignedInteger('lv')->default(1)->comment('层级'); $table->string('path')->default('-')->comment('所有的父级ID'); $table->timestamps();