createSystemPermissions(); $this->createSystemDictionaries(); $this->createSystemConfigs(); $this->command->info('System module data seeded successfully!'); // 恢复外键约束 if (DB::getDriverName() === 'sqlite') { DB::statement('PRAGMA foreign_keys = ON;'); } else { DB::statement('SET FOREIGN_KEY_CHECKS=1;'); } } /** * 创建系统管理权限节点 */ private function createSystemPermissions(): void { $permissions = [ // 系统顶级菜单 [ 'title' => '系统', 'name' => 'system', 'type' => 'menu', 'parent_id' => 0, 'path' => '/system', 'component' => null, 'meta' => json_encode([ 'icon' => 'Setting', 'hidden' => false, 'hiddenBreadcrumb' => false, ]), 'sort' => 3, 'status' => 1, ], // 系统配置 [ 'title' => '系统配置', 'name' => 'system.config', 'type' => 'menu', 'parent_id' => 0, // 稍后更新为系统菜单的ID 'path' => '/system/config', 'component' => 'system/config/index', 'meta' => json_encode([ 'icon' => 'SettingFilled', 'hidden' => false, 'hiddenBreadcrumb' => false, ]), 'sort' => 1, 'status' => 1, ], [ 'title' => '查看配置', 'name' => 'system.config.view', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统配置菜单的ID 'path' => 'admin.config.index', 'component' => null, 'meta' => null, 'sort' => 1, 'status' => 1, ], [ 'title' => '创建配置', 'name' => 'system.config.create', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统配置菜单的ID 'path' => 'admin.config.store', 'component' => null, 'meta' => null, 'sort' => 2, 'status' => 1, ], [ 'title' => '编辑配置', 'name' => 'system.config.update', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统配置菜单的ID 'path' => 'admin.config.update', 'component' => null, 'meta' => null, 'sort' => 3, 'status' => 1, ], [ 'title' => '删除配置', 'name' => 'system.config.delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统配置菜单的ID 'path' => 'admin.config.destroy', 'component' => null, 'meta' => null, 'sort' => 4, 'status' => 1, ], [ 'title' => '批量删除配置', 'name' => 'system.config.batch-delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统配置菜单的ID 'path' => 'admin.config.batch-delete', 'component' => null, 'meta' => null, 'sort' => 5, 'status' => 1, ], // 系统日志 [ 'title' => '系统日志', 'name' => 'system.logs', 'type' => 'menu', 'parent_id' => 0, // 稍后更新为系统菜单的ID 'path' => '/system/logs', 'component' => 'system/logs/index', 'meta' => json_encode([ 'icon' => 'DocumentCopy', 'hidden' => false, 'hiddenBreadcrumb' => false, ]), 'sort' => 2, 'status' => 1, ], [ 'title' => '查看日志', 'name' => 'system.logs.view', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统日志菜单的ID 'path' => 'admin.logs.index', 'component' => null, 'meta' => null, 'sort' => 1, 'status' => 1, ], [ 'title' => '删除日志', 'name' => 'system.logs.delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统日志菜单的ID 'path' => 'admin.logs.destroy', 'component' => null, 'meta' => null, 'sort' => 2, 'status' => 1, ], [ 'title' => '批量删除日志', 'name' => 'system.logs.batch-delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统日志菜单的ID 'path' => 'admin.logs.batch-delete', 'component' => null, 'meta' => null, 'sort' => 3, 'status' => 1, ], [ 'title' => '导出日志', 'name' => 'system.logs.export', 'type' => 'button', 'parent_id' => 0, // 稍后更新为系统日志菜单的ID 'path' => 'admin.logs.export', 'component' => null, 'meta' => null, 'sort' => 4, 'status' => 1, ], // 数据字典 [ 'title' => '数据字典', 'name' => 'system.dictionaries', 'type' => 'menu', 'parent_id' => 0, // 稍后更新为系统菜单的ID 'path' => '/system/dictionaries', 'component' => 'system/dictionaries/index', 'meta' => json_encode([ 'icon' => 'Notebook', 'hidden' => false, 'hiddenBreadcrumb' => false, ]), 'sort' => 3, 'status' => 1, ], [ 'title' => '查看字典', 'name' => 'system.dictionaries.view', 'type' => 'button', 'parent_id' => 0, // 稍后更新为数据字典菜单的ID 'path' => 'admin.dictionaries.index', 'component' => null, 'meta' => null, 'sort' => 1, 'status' => 1, ], [ 'title' => '创建字典', 'name' => 'system.dictionaries.create', 'type' => 'button', 'parent_id' => 0, // 稍后更新为数据字典菜单的ID 'path' => 'admin.dictionaries.store', 'component' => null, 'meta' => null, 'sort' => 2, 'status' => 1, ], [ 'title' => '编辑字典', 'name' => 'system.dictionaries.update', 'type' => 'button', 'parent_id' => 0, // 稍后更新为数据字典菜单的ID 'path' => 'admin.dictionaries.update', 'component' => null, 'meta' => null, 'sort' => 3, 'status' => 1, ], [ 'title' => '删除字典', 'name' => 'system.dictionaries.delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为数据字典菜单的ID 'path' => 'admin.dictionaries.destroy', 'component' => null, 'meta' => null, 'sort' => 4, 'status' => 1, ], [ 'title' => '批量删除字典', 'name' => 'system.dictionaries.batch-delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为数据字典菜单的ID 'path' => 'admin.dictionaries.batch-delete', 'component' => null, 'meta' => null, 'sort' => 5, 'status' => 1, ], // 定时任务 [ 'title' => '定时任务', 'name' => 'system.tasks', 'type' => 'menu', 'parent_id' => 0, // 稍后更新为系统菜单的ID 'path' => '/system/tasks', 'component' => 'system/tasks/index', 'meta' => json_encode([ 'icon' => 'Timer', 'hidden' => false, 'hiddenBreadcrumb' => false, ]), 'sort' => 4, 'status' => 1, ], [ 'title' => '查看任务', 'name' => 'system.tasks.view', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.index', 'component' => null, 'meta' => null, 'sort' => 1, 'status' => 1, ], [ 'title' => '创建任务', 'name' => 'system.tasks.create', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.store', 'component' => null, 'meta' => null, 'sort' => 2, 'status' => 1, ], [ 'title' => '编辑任务', 'name' => 'system.tasks.update', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.update', 'component' => null, 'meta' => null, 'sort' => 3, 'status' => 1, ], [ 'title' => '删除任务', 'name' => 'system.tasks.delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.destroy', 'component' => null, 'meta' => null, 'sort' => 4, 'status' => 1, ], [ 'title' => '批量删除任务', 'name' => 'system.tasks.batch-delete', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.batch-delete', 'component' => null, 'meta' => null, 'sort' => 5, 'status' => 1, ], [ 'title' => '执行任务', 'name' => 'system.tasks.execute', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.execute', 'component' => null, 'meta' => null, 'sort' => 6, 'status' => 1, ], [ 'title' => '启用任务', 'name' => 'system.tasks.enable', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.enable', 'component' => null, 'meta' => null, 'sort' => 7, 'status' => 1, ], [ 'title' => '禁用任务', 'name' => 'system.tasks.disable', 'type' => 'button', 'parent_id' => 0, // 稍后更新为定时任务菜单的ID 'path' => 'admin.tasks.disable', 'component' => null, 'meta' => null, 'sort' => 8, 'status' => 1, ], ]; foreach ($permissions as $permission) { Permission::create($permission); } // 更新parent_id $this->updateParentIds(); } /** * 更新parent_id,建立层级关系 */ private function updateParentIds(): void { $permissions = Permission::all(); // 获取系统顶级菜单ID $systemMenu = $permissions->where('name', 'system')->first(); // 获取系统子菜单ID $configMenu = $permissions->where('name', 'system.config')->first(); $logsMenu = $permissions->where('name', 'system.logs')->first(); $dictionariesMenu = $permissions->where('name', 'system.dictionaries')->first(); $tasksMenu = $permissions->where('name', 'system.tasks')->first(); // 更新系统子菜单的parent_id if ($systemMenu) { if ($configMenu) { $configMenu->update(['parent_id' => $systemMenu->id]); } if ($logsMenu) { $logsMenu->update(['parent_id' => $systemMenu->id]); } if ($dictionariesMenu) { $dictionariesMenu->update(['parent_id' => $systemMenu->id]); } if ($tasksMenu) { $tasksMenu->update(['parent_id' => $systemMenu->id]); } } // 更新按钮权限的parent_id - 系统配置 $configViewBtn = $permissions->where('name', 'system.config.view')->first(); $configCreateBtn = $permissions->where('name', 'system.config.create')->first(); $configUpdateBtn = $permissions->where('name', 'system.config.update')->first(); $configDeleteBtn = $permissions->where('name', 'system.config.delete')->first(); $configBatchDeleteBtn = $permissions->where('name', 'system.config.batch-delete')->first(); if ($configMenu) { if ($configViewBtn) { $configViewBtn->update(['parent_id' => $configMenu->id]); } if ($configCreateBtn) { $configCreateBtn->update(['parent_id' => $configMenu->id]); } if ($configUpdateBtn) { $configUpdateBtn->update(['parent_id' => $configMenu->id]); } if ($configDeleteBtn) { $configDeleteBtn->update(['parent_id' => $configMenu->id]); } if ($configBatchDeleteBtn) { $configBatchDeleteBtn->update(['parent_id' => $configMenu->id]); } } // 更新按钮权限的parent_id - 系统日志 $logsViewBtn = $permissions->where('name', 'system.logs.view')->first(); $logsDeleteBtn = $permissions->where('name', 'system.logs.delete')->first(); $logsBatchDeleteBtn = $permissions->where('name', 'system.logs.batch-delete')->first(); $logsExportBtn = $permissions->where('name', 'system.logs.export')->first(); if ($logsMenu) { if ($logsViewBtn) { $logsViewBtn->update(['parent_id' => $logsMenu->id]); } if ($logsDeleteBtn) { $logsDeleteBtn->update(['parent_id' => $logsMenu->id]); } if ($logsBatchDeleteBtn) { $logsBatchDeleteBtn->update(['parent_id' => $logsMenu->id]); } if ($logsExportBtn) { $logsExportBtn->update(['parent_id' => $logsMenu->id]); } } // 更新按钮权限的parent_id - 数据字典 $dictViewBtn = $permissions->where('name', 'system.dictionaries.view')->first(); $dictCreateBtn = $permissions->where('name', 'system.dictionaries.create')->first(); $dictUpdateBtn = $permissions->where('name', 'system.dictionaries.update')->first(); $dictDeleteBtn = $permissions->where('name', 'system.dictionaries.delete')->first(); $dictBatchDeleteBtn = $permissions->where('name', 'system.dictionaries.batch-delete')->first(); if ($dictionariesMenu) { if ($dictViewBtn) { $dictViewBtn->update(['parent_id' => $dictionariesMenu->id]); } if ($dictCreateBtn) { $dictCreateBtn->update(['parent_id' => $dictionariesMenu->id]); } if ($dictUpdateBtn) { $dictUpdateBtn->update(['parent_id' => $dictionariesMenu->id]); } if ($dictDeleteBtn) { $dictDeleteBtn->update(['parent_id' => $dictionariesMenu->id]); } if ($dictBatchDeleteBtn) { $dictBatchDeleteBtn->update(['parent_id' => $dictionariesMenu->id]); } } // 更新按钮权限的parent_id - 定时任务 $taskViewBtn = $permissions->where('name', 'system.tasks.view')->first(); $taskCreateBtn = $permissions->where('name', 'system.tasks.create')->first(); $taskUpdateBtn = $permissions->where('name', 'system.tasks.update')->first(); $taskDeleteBtn = $permissions->where('name', 'system.tasks.delete')->first(); $taskBatchDeleteBtn = $permissions->where('name', 'system.tasks.batch-delete')->first(); $taskExecuteBtn = $permissions->where('name', 'system.tasks.execute')->first(); $taskEnableBtn = $permissions->where('name', 'system.tasks.enable')->first(); $taskDisableBtn = $permissions->where('name', 'system.tasks.disable')->first(); if ($tasksMenu) { if ($taskViewBtn) { $taskViewBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskCreateBtn) { $taskCreateBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskUpdateBtn) { $taskUpdateBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskDeleteBtn) { $taskDeleteBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskBatchDeleteBtn) { $taskBatchDeleteBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskExecuteBtn) { $taskExecuteBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskEnableBtn) { $taskEnableBtn->update(['parent_id' => $tasksMenu->id]); } if ($taskDisableBtn) { $taskDisableBtn->update(['parent_id' => $tasksMenu->id]); } } } /** * 创建系统字典 */ private function createSystemDictionaries(): void { // 创建字典类型 $dictionaries = [ [ 'name' => '用户状态', 'code' => 'user_status', 'description' => '用户账号状态', 'sort' => 1, 'status' => 1, ], [ 'name' => '性别', 'code' => 'gender', 'description' => '用户性别', 'sort' => 2, 'status' => 1, ], [ 'name' => '角色状态', 'code' => 'role_status', 'description' => '角色启用状态', 'sort' => 3, 'status' => 1, ], [ 'name' => '字典状态', 'code' => 'dictionary_status', 'description' => '数据字典状态', 'sort' => 4, 'status' => 1, ], [ 'name' => '任务状态', 'code' => 'task_status', 'description' => '定时任务状态', 'sort' => 5, 'status' => 1, ], [ 'name' => '日志类型', 'code' => 'log_type', 'description' => '系统日志类型', 'sort' => 6, 'status' => 1, ], [ 'name' => '是否', 'code' => 'yes_no', 'description' => '是否选项', 'sort' => 7, 'status' => 1, ], ]; foreach ($dictionaries as $dictionary) { $dict = Dictionary::create($dictionary); $this->createDictionaryItems($dict); } } /** * 创建字典项 */ private function createDictionaryItems(Dictionary $dictionary): void { $items = []; switch ($dictionary->code) { case 'user_status': $items = [ ['label' => '正常', 'value' => 1, 'sort' => 1, 'status' => 1], ['label' => '禁用', 'value' => 0, 'sort' => 2, 'status' => 1], ]; break; case 'gender': $items = [ ['label' => '男', 'value' => 1, 'sort' => 1, 'status' => 1], ['label' => '女', 'value' => 2, 'sort' => 2, 'status' => 1], ['label' => '保密', 'value' => 0, 'sort' => 3, 'status' => 1], ]; break; case 'role_status': $items = [ ['label' => '启用', 'value' => 1, 'sort' => 1, 'status' => 1], ['label' => '禁用', 'value' => 0, 'sort' => 2, 'status' => 1], ]; break; case 'dictionary_status': $items = [ ['label' => '启用', 'value' => 1, 'sort' => 1, 'status' => 1], ['label' => '禁用', 'value' => 0, 'sort' => 2, 'status' => 1], ]; break; case 'task_status': $items = [ ['label' => '待执行', 'value' => 0, 'sort' => 1, 'status' => 1], ['label' => '执行中', 'value' => 1, 'sort' => 2, 'status' => 1], ['label' => '已完成', 'value' => 2, 'sort' => 3, 'status' => 1], ['label' => '失败', 'value' => 3, 'sort' => 4, 'status' => 1], ]; break; case 'log_type': $items = [ ['label' => '登录日志', 'value' => 'login', 'sort' => 1, 'status' => 1], ['label' => '操作日志', 'value' => 'operation', 'sort' => 2, 'status' => 1], ['label' => '异常日志', 'value' => 'error', 'sort' => 3, 'status' => 1], ['label' => '系统日志', 'value' => 'system', 'sort' => 4, 'status' => 1], ]; break; case 'yes_no': $items = [ ['label' => '是', 'value' => 1, 'sort' => 1, 'status' => 1], ['label' => '否', 'value' => 0, 'sort' => 2, 'status' => 1], ]; break; } foreach ($items as $item) { $dictionary->items()->create($item); } } /** * 创建系统配置 */ private function createSystemConfigs(): void { $configs = [ [ 'group' => 'site', 'key' => 'site_name', 'name' => '网站名称', 'value' => 'Laravel Swoole 管理系统', 'type' => 'string', 'description' => '系统显示的网站名称', 'sort' => 1, 'is_system' => true, 'status' => 1, ], [ 'group' => 'site', 'key' => 'site_logo', 'name' => '网站Logo', 'value' => '', 'type' => 'file', 'description' => '系统Logo图片地址', 'sort' => 2, 'is_system' => true, 'status' => 1, ], [ 'group' => 'site', 'key' => 'site_copyright', 'name' => '版权信息', 'value' => '© 2024 Laravel Swoole Admin', 'type' => 'string', 'description' => '网站底部版权信息', 'sort' => 3, 'is_system' => true, 'status' => 1, ], [ 'group' => 'site', 'key' => 'site_icp', 'name' => '备案号', 'value' => '', 'type' => 'string', 'description' => '网站备案号', 'sort' => 4, 'is_system' => true, 'status' => 1, ], [ 'group' => 'upload', 'key' => 'upload_max_size', 'name' => '上传最大限制', 'value' => '10', 'type' => 'number', 'description' => '文件上传最大限制(MB)', 'sort' => 1, 'is_system' => true, 'status' => 1, ], [ 'group' => 'upload', 'key' => 'upload_allowed_types', 'name' => '允许上传类型', 'value' => 'jpg,jpeg,png,gif,pdf,doc,docx,xls,xlsx', 'type' => 'string', 'description' => '允许上传的文件扩展名', 'sort' => 2, 'is_system' => true, 'status' => 1, ], [ 'group' => 'system', 'key' => 'user_default_avatar', 'name' => '默认头像', 'value' => '', 'type' => 'file', 'description' => '用户默认头像地址', 'sort' => 1, 'is_system' => true, 'status' => 1, ], [ 'group' => 'system', 'key' => 'system_timezone', 'name' => '系统时区', 'value' => 'Asia/Shanghai', 'type' => 'string', 'description' => '系统默认时区', 'sort' => 2, 'is_system' => true, 'status' => 1, ], [ 'group' => 'system', 'key' => 'system_language', 'name' => '系统语言', 'value' => 'zh-CN', 'type' => 'string', 'description' => '系统默认语言', 'sort' => 3, 'is_system' => true, 'status' => 1, ], [ 'group' => 'system', 'key' => 'enable_register', 'name' => '开启注册', 'value' => '1', 'type' => 'boolean', 'description' => '是否开启用户注册功能', 'sort' => 4, 'is_system' => true, 'status' => 1, ], ]; foreach ($configs as $config) { Config::create($config); } } }