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; 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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use EloquentFilter\Filterable; use Illuminate\Support\Str;
use App\Admin\Components;
use Illuminate\Support\Facades\Str;
class Keyword extends Model class Keyword extends Model
{ {
@ -23,26 +24,27 @@ class Keyword extends Model
protected static function boot() protected static function boot()
{ {
parent::boot(); parent::boot();
// 监听 Keyword 的创建事件,用于初始化 path 和 lv 字段值 // 监听 Keyword 的创建事件,用于初始化 path 和 lv 字段值
static::saving(function ($keyword) { static::saving(function (Keyword $keyword) {
// 如果创建的是一个根类目 if (is_null($parent = $keyword->parent)) {
if (! $keyword->parent_id) { $keyword->forceFill([
// 将层级设为 1 'path' => '-',
$keyword->lv = 1; 'lv' => 1,
// 将 path 设为 - ]);
$keyword->path = '-';
if(empty($keyword->key)){ if((string) $keyword->key === ''){
$keyword->key = Str::quickRandom($length = 16); $keyword->key = Str::random(16);
} }
} else { } else {
// 将层级设为父类目的层级 + 1 $keyword->forceFill([
$keyword->lv = $keyword->parent->lv + 1; 'parent_key' => $parent->lv > 1 ? $parent->parent_key : $parent->key,
$keyword->parent_key = $keyword->parent->key; 'path' => $parent->full_path,
// 将 path 值设为父类目的 path 追加父类目 ID 以及最后跟上一个 - 分隔符 'lv' => $parent->lv + 1,
$keyword->path = $keyword->parent->path.$keyword->parent_id.'-'; ]);
//当前key是否为空
if(empty($keyword->key)){ if((string) $keyword->key === ''){
$keyword->key = $keyword->parent_key . '_' . (self::where('parent_key', $keyword->parent_key)->count() + 1); $keyword->key = $parent->key . '_' . ($parent->children()->count() + 1);
} }
} }
}); });
@ -73,4 +75,11 @@ class Keyword extends Model
}); });
return $mapArr; 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 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() public function run()
{ {
Keyword::truncate(); $keywords = [
$list = [ [
['key' => 'article_category', 'name' => '文章分类', 'list' => [ 'key' => 'article_category',
'name' => '文章分类',
]], 'children' => [],
['key' => 'article_tag', 'name' => '文章标签', 'list' => [//标签value填写色号指定标签颜色 ],
[
]], 'key' => 'article_tag',
['key' => 'banner_address', 'name' => '广告位置', 'list' => [ 'name' => '文章标签',
'children' => [],
]], ],
[
'key' => 'banner_address',
'name' => '广告位置',
'children' => [],
],
]; ];
foreach ($list as $item) { $this->insertKeywors($keywords);
$type = Keyword::create(Arr::except($item, 'list')); }
if (isset($item['list'])) { protected function insertKeywors(array $data = [], ?Keyword $parent = null): void
$keywords = []; {
foreach ($item['list'] as $index => $name) { /** @var array */
$template = [ foreach ($data as $i => $item) {
'key' => $type->key . ($index + 1), if (! is_array($item)) {
'parent_key' => $type->key, $item = ['name' => $item];
'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);
} }
$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));
} }
} }
} }