RoleLogic.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\auth;
  15. use app\common\{
  16. cache\AdminAuthCache,
  17. model\auth\SystemRole,
  18. logic\BaseLogic,
  19. model\auth\SystemRoleMenu
  20. };
  21. use think\facade\Db;
  22. /**
  23. * 角色逻辑层
  24. * Class RoleLogic
  25. * @package app\adminapi\logic\auth
  26. */
  27. class RoleLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 添加角色
  31. * @param array $params
  32. * @return bool
  33. * @author 段誉
  34. * @date 2021/12/29 11:50
  35. */
  36. public static function add(array $params): bool
  37. {
  38. Db::startTrans();
  39. try {
  40. $menuId = !empty($params['menu_id']) ? $params['menu_id'] : [];
  41. $role = SystemRole::create([
  42. 'name' => $params['name'],
  43. 'desc' => $params['desc'] ?? '',
  44. 'sort' => $params['sort'] ?? 0,
  45. ]);
  46. $data = [];
  47. foreach ($menuId as $item) {
  48. if (empty($item)) {
  49. continue;
  50. }
  51. $data[] = [
  52. 'role_id' => $role['id'],
  53. 'menu_id' => $item,
  54. ];
  55. }
  56. (new SystemRoleMenu)->insertAll($data);
  57. Db::commit();
  58. return true;
  59. } catch (\Exception $e) {
  60. Db::rollback();
  61. self::$error = $e->getMessage();
  62. return false;
  63. }
  64. }
  65. /**
  66. * @notes 编辑角色
  67. * @param array $params
  68. * @return bool
  69. * @author 段誉
  70. * @date 2021/12/29 14:16
  71. */
  72. public static function edit(array $params): bool
  73. {
  74. Db::startTrans();
  75. try {
  76. $menuId = !empty($params['menu_id']) ? $params['menu_id'] : [];
  77. SystemRole::update([
  78. 'id' => $params['id'],
  79. 'name' => $params['name'],
  80. 'desc' => $params['desc'] ?? '',
  81. 'sort' => $params['sort'] ?? 0,
  82. ]);
  83. if (!empty($menuId)) {
  84. SystemRoleMenu::where(['role_id' => $params['id']])->delete();
  85. $data = [];
  86. foreach ($menuId as $item) {
  87. $data[] = [
  88. 'role_id' => $params['id'],
  89. 'menu_id' => $item,
  90. ];
  91. }
  92. (new SystemRoleMenu)->insertAll($data);
  93. }
  94. (new AdminAuthCache())->deleteTag();
  95. Db::commit();
  96. return true;
  97. } catch (\Exception $e) {
  98. Db::rollback();
  99. self::$error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. /**
  104. * @notes 删除角色
  105. * @param int $id
  106. * @return bool
  107. * @author 段誉
  108. * @date 2021/12/29 14:16
  109. */
  110. public static function delete(int $id)
  111. {
  112. SystemRole::destroy(['id' => $id]);
  113. (new AdminAuthCache())->deleteTag();
  114. return true;
  115. }
  116. /**
  117. * @notes 角色详情
  118. * @param int $id
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @author 段誉
  124. * @date 2021/12/29 14:17
  125. */
  126. public static function detail(int $id): array
  127. {
  128. $detail = SystemRole::field('id,name,desc,sort')->find($id);
  129. $authList = $detail->roleMenuIndex()->select()->toArray();
  130. $menuId = array_column($authList, 'menu_id');
  131. $detail['menu_id'] = $menuId;
  132. return $detail->toArray();
  133. }
  134. /**
  135. * @notes 角色数据
  136. * @return array
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @author 段誉
  141. * @date 2022/10/13 10:39
  142. */
  143. public static function getAllData()
  144. {
  145. return SystemRole::order(['sort' => 'desc', 'id' => 'desc'])
  146. ->select()
  147. ->toArray();
  148. }
  149. }