优化更新

This commit is contained in:
2026-02-09 22:51:11 +08:00
parent 9939680942
commit adf7457502
12 changed files with 695 additions and 298 deletions
@@ -79,12 +79,11 @@ public function show($id)
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:50',
'code' => 'required|string|max:100|unique:auth_permissions,code',
'title' => 'required|string|max:50',
'name' => 'required|string|max:100|unique:auth_permissions,name',
'type' => 'required|in:menu,api,button',
'route' => 'nullable|string|max:200',
'component' => 'nullable|string|max:200',
'icon' => 'nullable|string|max:50',
'parent_id' => 'nullable|integer|exists:auth_permissions,id',
'sort' => 'nullable|integer|min:0',
'status' => 'nullable|integer|in:0,1',
@@ -106,12 +105,11 @@ public function store(Request $request)
public function update(Request $request, $id)
{
$validated = $request->validate([
'name' => 'nullable|string|max:50',
'code' => 'nullable|string|max:100|unique:auth_permissions,code,' . $id,
'title' => 'nullable|string|max:50',
'name' => 'nullable|string|max:100|unique:auth_permissions,name,' . $id,
'type' => 'nullable|in:menu,api,button',
'route' => 'nullable|string|max:200',
'component' => 'nullable|string|max:200',
'icon' => 'nullable|string|max:50',
'parent_id' => 'nullable|integer|exists:auth_permissions,id',
'sort' => 'nullable|integer|min:0',
'status' => 'nullable|integer|in:0,1',
+2 -2
View File
@@ -13,11 +13,11 @@ class Permission extends Model
protected $table = 'auth_permissions';
protected $fillable = [
'title',
'name',
'code',
'type',
'parent_id',
'route',
'path',
'component',
'meta',
'sort',
+1 -1
View File
@@ -71,7 +71,7 @@ public function hasPermission(string $permissionCode): bool
{
foreach ($this->roles as $role) {
foreach ($role->permissions as $permission) {
if ($permission->code === $permissionCode) {
if ($permission->name === $permissionCode) {
return true;
}
}
+5 -4
View File
@@ -203,8 +203,9 @@ private function buildMenuTree($permissions, $parentId = 0): array
foreach ($permissions as $permission) {
if ($permission->parent_id == $parentId) {
$node = [
'path' => $permission->route,
'name' => $permission->code,
'path' => $permission->path,
'name' => $permission->name,
'title' => $permission->title,
'meta' => $permission->meta ? json_decode($permission->meta, true) : [],
];
@@ -238,8 +239,8 @@ private function getUserPermissions(User $user): array
$permissions = [];
foreach ($user->roles as $role) {
foreach ($role->permissions as $permission) {
if (!in_array($permission->code, $permissions)) {
$permissions[] = $permission->code;
if (!in_array($permission->name, $permissions)) {
$permissions[] = $permission->name;
}
}
}
+6 -6
View File
@@ -30,10 +30,10 @@ public function getUserPermissions(int $userId): array
foreach ($role->permissions as $permission) {
$permissions[$permission->id] = [
'id' => $permission->id,
'title' => $permission->title,
'name' => $permission->name,
'code' => $permission->code,
'type' => $permission->type,
'route' => $permission->route,
'path' => $permission->path,
];
}
}
@@ -51,7 +51,7 @@ public function getUserPermissionCodes(int $userId): array
return Cache::remember($cacheKey, now()->addMinutes($this->cacheMinutes), function() use ($userId) {
$permissions = $this->getUserPermissions($userId);
return array_column($permissions, 'code');
return array_column($permissions, 'name');
});
}
@@ -112,8 +112,8 @@ public function getRolePermissions(int $roleId): array
return $role->permissions->map(function($permission) {
return [
'id' => $permission->id,
'title' => $permission->title,
'name' => $permission->name,
'code' => $permission->code,
'type' => $permission->type,
];
})->toArray();
@@ -195,10 +195,10 @@ protected function buildMenuTree(array $permissions, int $parentId = 0): array
if ($permission['parent_id'] == $parentId) {
$node = [
'id' => $permission['id'],
'title' => $permission['title'],
'name' => $permission['name'],
'code' => $permission['code'],
'type' => $permission['type'],
'route' => $permission['route'],
'path' => $permission['path'],
'component' => $permission['component'],
'meta' => json_decode($permission['meta'] ?? '{}', true),
'sort' => $permission['sort'],
+23 -23
View File
@@ -18,8 +18,8 @@ public function getList(array $params): array
// 搜索条件
if (!empty($params['keyword'])) {
$query->where(function ($q) use ($params) {
$q->where('name', 'like', '%' . $params['keyword'] . '%')
->orWhere('code', 'like', '%' . $params['keyword'] . '%');
$q->where('title', 'like', '%' . $params['keyword'] . '%')
->orWhere('name', 'like', '%' . $params['keyword'] . '%');
});
}
@@ -109,15 +109,15 @@ public function getById(int $id): array
return [
'id' => $permission->id,
'title' => $permission->title,
'name' => $permission->name,
'code' => $permission->code,
'type' => $permission->type,
'parent_id' => $permission->parent_id,
'parent' => $permission->parent ? [
'id' => $permission->parent->id,
'name' => $permission->parent->name,
] : null,
'route' => $permission->route,
'path' => $permission->path,
'component' => $permission->component,
'meta' => $permission->meta,
'sort' => $permission->sort,
@@ -132,17 +132,17 @@ public function getById(int $id): array
*/
public function create(array $data): Permission
{
// 检查权限名称是否已存在
if (Permission::where('name', $data['name'])->exists()) {
// 检查权限标题是否已存在
if (isset($data['title']) && Permission::where('title', $data['title'])->exists()) {
throw ValidationException::withMessages([
'name' => ['权限名称已存在'],
'title' => ['权限标题已存在'],
]);
}
// 检查权限编码是否已存在
if (Permission::where('code', $data['code'])->exists()) {
if (Permission::where('name', $data['name'])->exists()) {
throw ValidationException::withMessages([
'code' => ['权限编码已存在'],
'name' => ['权限编码已存在'],
]);
}
@@ -157,11 +157,11 @@ public function create(array $data): Permission
}
return Permission::create([
'title' => $data['title'],
'name' => $data['name'],
'code' => $data['code'],
'type' => $data['type'] ?? 'api',
'type' => $data['type'] ?? 'menu',
'parent_id' => $data['parent_id'] ?? 0,
'route' => $data['route'] ?? null,
'path' => $data['path'] ?? null,
'component' => $data['component'] ?? null,
'meta' => $data['meta'] ?? null,
'sort' => $data['sort'] ?? 0,
@@ -182,20 +182,20 @@ public function update(int $id, array $data): Permission
]);
}
// 检查权限名称是否已被其他权限使用
if (isset($data['name']) && $data['name'] !== $permission->name) {
if (Permission::where('name', $data['name'])->exists()) {
// 检查权限标题是否已被其他权限使用
if (isset($data['title']) && $data['title'] !== $permission->title) {
if (Permission::where('title', $data['title'])->exists()) {
throw ValidationException::withMessages([
'name' => ['权限名称已存在'],
'title' => ['权限标题已存在'],
]);
}
}
// 检查权限编码是否已被其他权限使用
if (isset($data['code']) && $data['code'] !== $permission->code) {
if (Permission::where('code', $data['code'])->exists()) {
if (isset($data['name']) && $data['name'] !== $permission->name) {
if (Permission::where('name', $data['name'])->exists()) {
throw ValidationException::withMessages([
'code' => ['权限编码已存在'],
'name' => ['权限编码已存在'],
]);
}
}
@@ -218,11 +218,11 @@ public function update(int $id, array $data): Permission
}
$updateData = [
'title' => $data['title'] ?? $permission->title,
'name' => $data['name'] ?? $permission->name,
'code' => $data['code'] ?? $permission->code,
'type' => $data['type'] ?? $permission->type,
'parent_id' => $data['parent_id'] ?? $permission->parent_id,
'route' => $data['route'] ?? $permission->route,
'path' => $data['path'] ?? $permission->path,
'component' => $data['component'] ?? $permission->component,
'meta' => isset($data['meta']) ? $data['meta'] : $permission->meta,
'sort' => $data['sort'] ?? $permission->sort,
@@ -305,10 +305,10 @@ private function buildTree($permissions, $parentId = 0): array
if ($permission->parent_id == $parentId) {
$node = [
'id' => $permission->id,
'title' => $permission->title,
'name' => $permission->name,
'code' => $permission->code,
'type' => $permission->type,
'route' => $permission->route,
'path' => $permission->path,
'component' => $permission->component,
'meta' => $permission->meta,
'sort' => $permission->sort,