调整数据字典

mall
vine_liutk 2023-12-08 12:12:00 +08:00
parent 9cf0ac0e44
commit cf4fb3ff80
5 changed files with 19 additions and 18 deletions

View File

@ -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);
}
/**

View File

@ -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('排序'),
]);
}

View File

@ -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');
}
}

View File

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

View File

@ -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();