初始化项目

This commit is contained in:
2026-02-08 22:38:13 +08:00
commit 334d2c6312
201 changed files with 32724 additions and 0 deletions

View File

@@ -0,0 +1,570 @@
<?php
namespace Database\Seeders;
use App\Models\Auth\User;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
use App\Models\Auth\Department;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
class AuthSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// SQLite 不支持 SET FOREIGN_KEY_CHECKS使用 PRAGMA
if (DB::getDriverName() === 'sqlite') {
DB::statement('PRAGMA foreign_keys = OFF;');
} else {
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
}
// 清空表数据
DB::table('auth_role_permission')->truncate();
DB::table('auth_user_role')->truncate();
Permission::truncate();
Role::truncate();
User::truncate();
Department::truncate();
$this->createPermissions();
$this->createRoles();
$this->createDepartments();
$this->createUsers();
$this->assignPermissionsToRole();
$this->assignRoleToUser();
$this->command->info('Auth module data seeded successfully!');
// 恢复外键约束
if (DB::getDriverName() === 'sqlite') {
DB::statement('PRAGMA foreign_keys = ON;');
} else {
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
}
/**
* 创建权限节点
*/
private function createPermissions(): void
{
$permissions = [
// 系统管理
[
'name' => '系统管理',
'code' => 'system',
'type' => 'menu',
'parent_id' => 0,
'route' => '/system',
'component' => 'Layout',
'meta' => json_encode([
'icon' => 'Setting',
'hidden' => false,
'hiddenBreadcrumb' => false,
'affix' => null,
]),
'sort' => 1,
'status' => 1,
],
// 用户管理
[
'name' => '用户管理',
'code' => 'system.users',
'type' => 'menu',
'parent_id' => 0,
'route' => '/system/users',
'component' => 'system/users/index',
'meta' => json_encode([
'icon' => 'User',
'hidden' => false,
'hiddenBreadcrumb' => false,
]),
'sort' => 2,
'status' => 1,
],
[
'name' => '查看用户',
'code' => 'system.users.view',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.index',
'component' => null,
'meta' => null,
'sort' => 1,
'status' => 1,
],
[
'name' => '创建用户',
'code' => 'system.users.create',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.store',
'component' => null,
'meta' => null,
'sort' => 2,
'status' => 1,
],
[
'name' => '编辑用户',
'code' => 'system.users.update',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.update',
'component' => null,
'meta' => null,
'sort' => 3,
'status' => 1,
],
[
'name' => '删除用户',
'code' => 'system.users.delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.destroy',
'component' => null,
'meta' => null,
'sort' => 4,
'status' => 1,
],
[
'name' => '批量删除用户',
'code' => 'system.users.batch-delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.batch-delete',
'component' => null,
'meta' => null,
'sort' => 5,
'status' => 1,
],
[
'name' => '导出用户',
'code' => 'system.users.export',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.export',
'component' => null,
'meta' => null,
'sort' => 6,
'status' => 1,
],
[
'name' => '导入用户',
'code' => 'system.users.import',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.users.import',
'component' => null,
'meta' => null,
'sort' => 7,
'status' => 1,
],
// 角色管理
[
'name' => '角色管理',
'code' => 'system.roles',
'type' => 'menu',
'parent_id' => 0,
'route' => '/system/roles',
'component' => 'system/roles/index',
'meta' => json_encode([
'icon' => 'UserFilled',
'hidden' => false,
'hiddenBreadcrumb' => false,
]),
'sort' => 3,
'status' => 1,
],
[
'name' => '查看角色',
'code' => 'system.roles.view',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.index',
'component' => null,
'meta' => null,
'sort' => 1,
'status' => 1,
],
[
'name' => '创建角色',
'code' => 'system.roles.create',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.store',
'component' => null,
'meta' => null,
'sort' => 2,
'status' => 1,
],
[
'name' => '编辑角色',
'code' => 'system.roles.update',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.update',
'component' => null,
'meta' => null,
'sort' => 3,
'status' => 1,
],
[
'name' => '删除角色',
'code' => 'system.roles.delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.destroy',
'component' => null,
'meta' => null,
'sort' => 4,
'status' => 1,
],
[
'name' => '批量删除角色',
'code' => 'system.roles.batch-delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.batch-delete',
'component' => null,
'meta' => null,
'sort' => 5,
'status' => 1,
],
[
'name' => '分配权限',
'code' => 'system.roles.assign-permissions',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.roles.assign-permissions',
'component' => null,
'meta' => null,
'sort' => 6,
'status' => 1,
],
// 权限管理
[
'name' => '权限管理',
'code' => 'system.permissions',
'type' => 'menu',
'parent_id' => 0,
'route' => '/system/permissions',
'component' => 'system/permissions/index',
'meta' => json_encode([
'icon' => 'Lock',
'hidden' => false,
'hiddenBreadcrumb' => false,
]),
'sort' => 4,
'status' => 1,
],
[
'name' => '查看权限',
'code' => 'system.permissions.view',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.permissions.index',
'component' => null,
'meta' => null,
'sort' => 1,
'status' => 1,
],
[
'name' => '创建权限',
'code' => 'system.permissions.create',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.permissions.store',
'component' => null,
'meta' => null,
'sort' => 2,
'status' => 1,
],
[
'name' => '编辑权限',
'code' => 'system.permissions.update',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.permissions.update',
'component' => null,
'meta' => null,
'sort' => 3,
'status' => 1,
],
[
'name' => '删除权限',
'code' => 'system.permissions.delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.permissions.destroy',
'component' => null,
'meta' => null,
'sort' => 4,
'status' => 1,
],
[
'name' => '批量删除权限',
'code' => 'system.permissions.batch-delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.permissions.batch-delete',
'component' => null,
'meta' => null,
'sort' => 5,
'status' => 1,
],
// 部门管理
[
'name' => '部门管理',
'code' => 'system.departments',
'type' => 'menu',
'parent_id' => 0,
'route' => '/system/departments',
'component' => 'system/departments/index',
'meta' => json_encode([
'icon' => 'OfficeBuilding',
'hidden' => false,
'hiddenBreadcrumb' => false,
]),
'sort' => 5,
'status' => 1,
],
[
'name' => '查看部门',
'code' => 'system.departments.view',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.departments.index',
'component' => null,
'meta' => null,
'sort' => 1,
'status' => 1,
],
[
'name' => '创建部门',
'code' => 'system.departments.create',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.departments.store',
'component' => null,
'meta' => null,
'sort' => 2,
'status' => 1,
],
[
'name' => '编辑部门',
'code' => 'system.departments.update',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.departments.update',
'component' => null,
'meta' => null,
'sort' => 3,
'status' => 1,
],
[
'name' => '删除部门',
'code' => 'system.departments.delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.departments.destroy',
'component' => null,
'meta' => null,
'sort' => 4,
'status' => 1,
],
[
'name' => '批量删除部门',
'code' => 'system.departments.batch-delete',
'type' => 'button',
'parent_id' => 0,
'route' => 'admin.departments.batch-delete',
'component' => null,
'meta' => null,
'sort' => 5,
'status' => 1,
],
];
foreach ($permissions as $permission) {
Permission::create($permission);
}
}
/**
* 创建角色
*/
private function createRoles(): void
{
Role::insert([
[
'name' => '超级管理员',
'code' => 'super_admin',
'description' => '拥有系统所有权限',
'sort' => 1,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => '管理员',
'code' => 'admin',
'description' => '拥有系统管理权限',
'sort' => 2,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => '普通用户',
'code' => 'user',
'description' => '普通用户角色',
'sort' => 3,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
]);
}
/**
* 创建部门
*/
private function createDepartments(): void
{
Department::insert([
[
'name' => '总公司',
'parent_id' => 0,
'leader' => '张三',
'phone' => '13800138000',
'sort' => 1,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => '技术部',
'parent_id' => 0,
'leader' => '李四',
'phone' => '13800138001',
'sort' => 1,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => '运营部',
'parent_id' => 0,
'leader' => '王五',
'phone' => '13800138002',
'sort' => 2,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => '财务部',
'parent_id' => 0,
'leader' => '赵六',
'phone' => '13800138003',
'sort' => 3,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
],
]);
// 更新parent_id为实际ID
$departments = Department::all();
$rootDept = $departments->where('name', '总公司')->first();
$techDept = $departments->where('name', '技术部')->first();
$opsDept = $departments->where('name', '运营部')->first();
$financeDept = $departments->where('name', '财务部')->first();
if ($rootDept) {
$techDept->update(['parent_id' => $rootDept->id]);
$opsDept->update(['parent_id' => $rootDept->id]);
$financeDept->update(['parent_id' => $rootDept->id]);
}
}
/**
* 创建用户
*/
private function createUsers(): void
{
$departments = Department::all();
$techDept = $departments->where('name', '技术部')->first();
User::insert([
[
'username' => 'admin',
'password' => Hash::make('admin888'),
'real_name' => '超级管理员',
'email' => 'admin@example.com',
'phone' => '13800138888',
'department_id' => $techDept ? $techDept->id : null,
'avatar' => null,
'status' => 1,
'last_login_at' => null,
'last_login_ip' => null,
'created_at' => now(),
'updated_at' => now(),
],
[
'username' => 'manager',
'password' => Hash::make('123456789'),
'real_name' => '部门经理',
'email' => 'manager@example.com',
'phone' => '13800138889',
'department_id' => $techDept ? $techDept->id : null,
'avatar' => null,
'status' => 1,
'last_login_at' => null,
'last_login_ip' => null,
'created_at' => now(),
'updated_at' => now(),
],
]);
}
/**
* 分配权限给角色
*/
private function assignPermissionsToRole(): void
{
$permissions = Permission::all();
$superAdminRole = Role::where('code', 'super_admin')->first();
if ($superAdminRole) {
$permissionIds = $permissions->pluck('id')->toArray();
$superAdminRole->permissions()->attach($permissionIds);
}
}
/**
* 分配角色给用户
*/
private function assignRoleToUser(): void
{
$superAdminRole = Role::where('code', 'super_admin')->first();
$adminRole = Role::where('code', 'admin')->first();
$adminUser = User::where('username', 'admin')->first();
$managerUser = User::where('username', 'manager')->first();
if ($adminUser && $superAdminRole) {
$adminUser->roles()->attach([$superAdminRole->id]);
}
if ($managerUser && $adminRole) {
$managerUser->roles()->attach([$adminRole->id]);
}
}
}