Menu.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Models\System;
  3. use App\Exceptions\ApiException;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Spatie\Permission\Contracts\Permission as PermissionContract;
  9. use Spatie\Permission\Exceptions\PermissionAlreadyExists;
  10. use Spatie\Permission\Guard;
  11. use Spatie\Permission\Models\Permission;
  12. use Symfony\Component\HttpFoundation\Response;
  13. class Menu extends Permission
  14. {
  15. protected $attributes = [
  16. 'title' => 'name'
  17. // 'name' => 'title',
  18. // 'permission' => 'name'
  19. ];
  20. public static $snakeAttributes = false;
  21. protected $appends = ['title'];
  22. protected $casts = [
  23. 'visible' => 'bool',
  24. 'keepAlive' => 'bool',
  25. 'alwaysShow' => 'bool',
  26. // 'parent_id' => 'camel'
  27. ];
  28. /**
  29. * @throws ApiException
  30. */
  31. public static function create(array $attributes = [])
  32. {
  33. $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
  34. if (isset($attributes['permission']) && $attributes['permission']) {
  35. $permission = static::getPermission(['permission' => $attributes['permission'], 'guard_name' => $attributes['guard_name']]);
  36. if ($permission) {
  37. throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'PERMISSION_ALREADY_EXISTS']);
  38. }
  39. }
  40. return static::query()->create($attributes);
  41. }
  42. /**
  43. * @param array $attributes
  44. * @param array $options
  45. * @throws ApiException
  46. */
  47. public function update(array $attributes = [], array $options = []): void
  48. {
  49. $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
  50. if (isset($attributes['permission']) && $attributes['permission']) {
  51. $permission = static::getPermission(['permission' => $attributes['permission'], 'guard_name' => $attributes['guard_name']]);
  52. if ($permission && $permission->id !== $attributes['id']) {
  53. throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'PERMISSION_ALREADY_EXISTS']);
  54. }
  55. }
  56. static::query()->where('id', $attributes['id'])->update($attributes, $options);
  57. Cache::forget(config('permission.cache.key'));
  58. }
  59. public function setTitleAttribute($value)
  60. {
  61. $this->attributes['name'] = $value;
  62. }
  63. public function getTitleAttribute()
  64. {
  65. return $this->attributes['name'];
  66. }
  67. public function setNameAttribute($value)
  68. {
  69. $this->attributes['permission'] = $value;
  70. }
  71. public function getNameAttribute()
  72. {
  73. return $this->attributes['permission'];
  74. }
  75. public function setComponentNameAttribute($value)
  76. {
  77. $this->attributes['component_name'] = $value;
  78. }
  79. public function setVisibleAttribute($value)
  80. {
  81. $this->attributes['visible'] = $value ? 1 : 0;
  82. }
  83. public function setAlwaysShowAttribute($value)
  84. {
  85. $this->attributes['always_show'] = $value ? 1 : 0;
  86. }
  87. public function setKeepAliveAttribute($value)
  88. {
  89. $this->attributes['keep_alive'] = $value ? 1 : 0;
  90. }
  91. }