SystemRoleLogic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\app\cache\UserMenuCache;
  9. use plugin\saiadmin\app\model\system\SystemRole;
  10. use plugin\saiadmin\basic\think\BaseLogic;
  11. use plugin\saiadmin\exception\ApiException;
  12. use plugin\saiadmin\utils\Helper;
  13. use support\think\Cache;
  14. use support\think\Db;
  15. /**
  16. * 角色逻辑层
  17. */
  18. class SystemRoleLogic extends BaseLogic
  19. {
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new SystemRole();
  26. }
  27. /**
  28. * 添加数据
  29. */
  30. public function add($data): bool
  31. {
  32. $data = $this->handleData($data);
  33. return $this->model->save($data);
  34. }
  35. /**
  36. * 修改数据
  37. */
  38. public function edit($id, $data): bool
  39. {
  40. $model = $this->model->findOrEmpty($id);
  41. if ($model->isEmpty()) {
  42. throw new ApiException('数据不存在');
  43. }
  44. $data = $this->handleData($data);
  45. return $model->save($data);
  46. }
  47. /**
  48. * 删除数据
  49. */
  50. public function destroy($ids): bool
  51. {
  52. // 越权保护
  53. $levelArr = array_column($this->adminInfo['roleList'], 'level');
  54. $maxLevel = max($levelArr);
  55. $num = SystemRole::where('level', '>=', $maxLevel)->whereIn('id', $ids)->count();
  56. if ($num > 0) {
  57. throw new ApiException('不能操作比当前账户职级高的角色');
  58. } else {
  59. return $this->model->destroy($ids);
  60. }
  61. }
  62. /**
  63. * 数据处理
  64. */
  65. protected function handleData($data)
  66. {
  67. // 越权保护
  68. $levelArr = array_column($this->adminInfo['roleList'], 'level');
  69. $maxLevel = max($levelArr);
  70. if ($data['level'] >= $maxLevel) {
  71. throw new ApiException('不能操作比当前账户职级高的角色');
  72. }
  73. return $data;
  74. }
  75. /**
  76. * 可操作角色
  77. * @param array $where
  78. * @return array
  79. */
  80. public function accessRole(array $where = []): array
  81. {
  82. $query = $this->search($where);
  83. // 越权保护
  84. $levelArr = array_column($this->adminInfo['roleList'], 'level');
  85. $maxLevel = max($levelArr);
  86. $query->where('level', '<', $maxLevel);
  87. $query->order('sort', 'desc');
  88. return $this->getAll($query);
  89. }
  90. /**
  91. * 根据角色数组获取菜单
  92. * @param $ids
  93. * @return array
  94. */
  95. public function getMenuIdsByRoleIds($ids): array
  96. {
  97. if (empty($ids))
  98. return [];
  99. return $this->model->where('id', 'in', $ids)->with([
  100. 'menus' => function ($query) {
  101. $query->where('status', 1)->order('sort', 'desc');
  102. }
  103. ])->select()->toArray();
  104. }
  105. /**
  106. * 根据角色获取菜单
  107. * @param $id
  108. * @return array
  109. */
  110. public function getMenuByRole($id): array
  111. {
  112. $role = $this->model->findOrEmpty($id);
  113. $menus = $role->menus ?: [];
  114. return [
  115. 'id' => $id,
  116. 'menus' => $menus
  117. ];
  118. }
  119. /**
  120. * 保存菜单权限
  121. * @param $id
  122. * @param $menu_ids
  123. * @return mixed
  124. */
  125. public function saveMenuPermission($id, $menu_ids): mixed
  126. {
  127. return $this->transaction(function () use ($id, $menu_ids) {
  128. $role = $this->model->findOrEmpty($id);
  129. if ($role) {
  130. $role->menus()->detach();
  131. $data = array_map(function ($menu_id) use ($id) {
  132. return ['menu_id' => $menu_id, 'role_id' => $id];
  133. }, $menu_ids);
  134. Db::name('sa_system_role_menu')->limit(100)->insertAll($data);
  135. }
  136. $cache = config('plugin.saiadmin.saithink.button_cache');
  137. $tag = $cache['role'] . $id;
  138. Cache::tag($tag)->clear(); // 清理权限缓存-角色TAG
  139. UserMenuCache::clearMenuCache(); // 清理菜单缓存
  140. return true;
  141. });
  142. }
  143. }