main
panliang 2024-03-22 12:29:09 +08:00
commit 5d4cd329bf
4 changed files with 91 additions and 54 deletions

View File

@ -2,11 +2,12 @@
namespace App\Models;
use App\Admin\Components;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use EloquentFilter\Filterable;
use App\Admin\Components;
use Illuminate\Support\Facades\Str;
use Illuminate\Support\Str;
class Keyword extends Model
{
@ -14,7 +15,7 @@ class Keyword extends Model
use Filterable;
protected $fillable = ['name', 'key', 'value', 'parent_id', 'parent_key', 'path', 'sort', 'lv'];
protected function serializeDate(\DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
@ -23,26 +24,27 @@ class Keyword extends Model
protected static function boot()
{
parent::boot();
// 监听 Keyword 的创建事件,用于初始化 path 和 lv 字段值
static::saving(function ($keyword) {
// 如果创建的是一个根类目
if (! $keyword->parent_id) {
// 将层级设为 1
$keyword->lv = 1;
// 将 path 设为 -
$keyword->path = '-';
if(empty($keyword->key)){
$keyword->key = Str::quickRandom($length = 16);
static::saving(function (Keyword $keyword) {
if (is_null($parent = $keyword->parent)) {
$keyword->forceFill([
'path' => '-',
'lv' => 1,
]);
if((string) $keyword->key === ''){
$keyword->key = Str::random(16);
}
} else {
// 将层级设为父类目的层级 + 1
$keyword->lv = $keyword->parent->lv + 1;
$keyword->parent_key = $keyword->parent->key;
// 将 path 值设为父类目的 path 追加父类目 ID 以及最后跟上一个 - 分隔符
$keyword->path = $keyword->parent->path.$keyword->parent_id.'-';
//当前key是否为空
if(empty($keyword->key)){
$keyword->key = $keyword->parent_key . '_' . (self::where('parent_key', $keyword->parent_key)->count() + 1);
$keyword->forceFill([
'parent_key' => $parent->lv > 1 ? $parent->parent_key : $parent->key,
'path' => $parent->full_path,
'lv' => $parent->lv + 1,
]);
if((string) $keyword->key === ''){
$keyword->key = $parent->key . '_' . ($parent->children()->count() + 1);
}
}
});
@ -60,7 +62,7 @@ class Keyword extends Model
public function scopeAllChildrenOfKey($q, $parentKey)
{
$q->where('path','like', '%-'.
$q->where('path','like', '%-'.
static::where('key', $parentKey)->value('id')
. '-%' ?? '');
}
@ -73,4 +75,11 @@ class Keyword extends Model
});
return $mapArr;
}
protected function fullPath(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes) => $attributes['path'].$attributes['id'].'-',
);
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class AdminSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call([
KeywordSeeder::class,
]);
}
}

View File

@ -12,7 +12,6 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
$this->call(AdminMenuSeeder::class);
$this->call(KeywordSeeder::class);
//
}
}

View File

@ -15,40 +15,49 @@ class KeywordSeeder extends Seeder
*/
public function run()
{
Keyword::truncate();
$list = [
['key' => 'article_category', 'name' => '文章分类', 'list' => [
]],
['key' => 'article_tag', 'name' => '文章标签', 'list' => [//标签value填写色号指定标签颜色
]],
['key' => 'banner_address', 'name' => '广告位置', 'list' => [
]],
$keywords = [
[
'key' => 'article_category',
'name' => '文章分类',
'children' => [],
],
[
'key' => 'article_tag',
'name' => '文章标签',
'children' => [],
],
[
'key' => 'banner_address',
'name' => '广告位置',
'children' => [],
],
];
foreach ($list as $item) {
$type = Keyword::create(Arr::except($item, 'list'));
$this->insertKeywors($keywords);
}
if (isset($item['list'])) {
$keywords = [];
foreach ($item['list'] as $index => $name) {
$template = [
'key' => $type->key . ($index + 1),
'parent_key' => $type->key,
'lv' => $type->lv + 1,
'sort' => $index + 1
];
if (is_array($name)) {
$template = array_merge($template, $name);
} else {
$template['name'] = $name;
}
array_push($keywords, $template);
}
$type->children()->createMany($keywords);
protected function insertKeywors(array $data = [], ?Keyword $parent = null): void
{
/** @var array */
foreach ($data as $i => $item) {
if (! is_array($item)) {
$item = ['name' => $item];
}
$item['parent_id'] = $parent?->id ?: 0;
if (! array_key_exists('sort', $item)) {
$item['sort'] = $i + 1;
}
$key = Arr::pull($item, 'key', $parent?->key . '_' . ($i + 1));
/** @var \App\Models\Keyword */
$keyword = Keyword::updateOrCreate(
['key' => $key], Arr::except($item, ['children'])
);
$this->insertKeywors($item['children'] ?? [], $keyword->setRelation('parent', $parent));
}
}
}