study-api-v2/database/seeders/SystemMenuSeeder.php

291 lines
9.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\System\SystemMenu;
use App\Models\System\SystemRole;
use App\Models\System\SystemRoleMenu;
use App\Models\System\SystemUserRole;
use App\Models\User;
use Carbon\Carbon;
class SystemMenuSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$now = Carbon::now();
$creator = 'system';
// 清空现有数据
SystemRoleMenu::query()->delete();
SystemUserRole::query()->delete();
SystemMenu::query()->delete();
SystemRole::query()->delete();
// 创建角色
$adminRole = SystemRole::create([
'name' => '超级管理员',
'code' => 'superadmin',
'sort' => 1,
'data_scope' => 1,
'status' => 0,
'type' => 1,
'remark' => '超级管理员角色',
'creator' => $creator,
'create_time' => $now,
'updater' => $creator,
'update_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
$testRole = SystemRole::create([
'name' => '测试角色',
'code' => 'test',
'sort' => 2,
'data_scope' => 2,
'status' => 0,
'type' => 1,
'remark' => '测试角色',
'creator' => $creator,
'create_time' => $now,
'updater' => $creator,
'update_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
// 创建菜单数据参考test.json的结构
$menus = [
// 练习模块
[
'id' => 2972,
'name' => '练习',
'parent_id' => 0,
'type' => 1,
'path' => '/exercise',
'icon' => 'fa:tree',
'component' => null,
'component_name' => null,
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 1,
],
[
'id' => 2892,
'name' => '同步教材详情',
'parent_id' => 2972,
'type' => 2,
'path' => 'textbook/catalog',
'icon' => 'ep:list',
'component' => '/textbook/catalog/index',
'component_name' => 'TextbookCatalog',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 1,
],
[
'id' => 2898,
'name' => '同步教材',
'parent_id' => 2972,
'type' => 2,
'path' => 'textbook/catalogList',
'icon' => 'fa-solid:book',
'component' => '/textbook/catalog/list',
'component_name' => null,
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 2,
],
// 基础数据管理模块
[
'id' => 2808,
'name' => '基础数据管理',
'parent_id' => 0,
'type' => 1,
'path' => '/infra-data',
'icon' => 'fa-solid:database',
'component' => null,
'component_name' => null,
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 2,
],
[
'id' => 2930,
'name' => '题目列表',
'parent_id' => 2808,
'type' => 2,
'path' => 'question/list',
'icon' => 'fa-solid:list-ul',
'component' => '/question/index',
'component_name' => 'InfraDataQuestion',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 1,
],
// 系统管理模块
[
'id' => 1,
'name' => '系统管理',
'parent_id' => 0,
'type' => 1,
'path' => '/system',
'icon' => 'ep:tools',
'component' => null,
'component_name' => null,
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 10,
],
[
'id' => 100,
'name' => '用户管理',
'parent_id' => 1,
'type' => 2,
'path' => 'user',
'icon' => 'ep:avatar',
'component' => 'system/user/index',
'component_name' => 'SystemUser',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 1,
],
[
'id' => 101,
'name' => '角色管理',
'parent_id' => 1,
'type' => 2,
'path' => 'role',
'icon' => 'ep:user',
'component' => 'system/role/index',
'component_name' => 'SystemRole',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 2,
],
[
'id' => 102,
'name' => '菜单管理',
'parent_id' => 1,
'type' => 2,
'path' => 'menu',
'icon' => 'ep:menu',
'component' => 'system/menu/index',
'component_name' => 'SystemMenu',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 3,
],
[
'id' => 105,
'name' => '字典管理',
'parent_id' => 1,
'type' => 2,
'path' => 'dict',
'icon' => 'ep:collection',
'component' => 'system/dict/index',
'component_name' => 'SystemDictType',
'status' => 0,
'visible' => 0,
'keep_alive' => 1,
'always_show' => 1,
'sort' => 4,
],
];
// 批量插入菜单数据
foreach ($menus as $menuData) {
$menuData['creator'] = $creator;
$menuData['create_time'] = $now;
$menuData['updater'] = $creator;
$menuData['update_time'] = $now;
$menuData['deleted'] = 0;
$menuData['tenant_id'] = 1;
SystemMenu::create($menuData);
}
// 为超级管理员分配所有菜单权限
$allMenuIds = SystemMenu::pluck('id')->toArray();
foreach ($allMenuIds as $menuId) {
SystemRoleMenu::create([
'role_id' => $adminRole->id,
'menu_id' => $menuId,
'creator' => $creator,
'create_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
}
// 为测试角色只分配部分菜单权限(系统管理模块)
$testMenuIds = [1, 100, 101, 102, 105]; // 系统管理相关菜单
foreach ($testMenuIds as $menuId) {
SystemRoleMenu::create([
'role_id' => $testRole->id,
'menu_id' => $menuId,
'creator' => $creator,
'create_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
}
// 为现有用户分配角色
$adminUser = User::where('username', 'admin')->first();
if ($adminUser) {
SystemUserRole::create([
'user_id' => $adminUser->id,
'role_id' => $adminRole->id,
'creator' => $creator,
'create_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
}
$testUser = User::where('username', 'test')->first();
if ($testUser) {
SystemUserRole::create([
'user_id' => $testUser->id,
'role_id' => $testRole->id,
'creator' => $creator,
'create_time' => $now,
'deleted' => 0,
'tenant_id' => 1,
]);
}
$this->command->info('菜单和角色权限数据创建成功!');
$this->command->info('- 创建了超级管理员角色superadmin拥有所有菜单权限');
$this->command->info('- 创建了测试角色test仅拥有系统管理模块权限');
$this->command->info('- admin用户 -> 超级管理员角色');
$this->command->info('- test用户 -> 测试角色');
}
}