RoleLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\tenantapi\logic\auth;
  15. use app\common\logic\BaseLogic;
  16. use app\common\model\auth\TenantSystemRole;
  17. use app\common\model\auth\TenantSystemRoleMenu;
  18. use app\common\{
  19. cache\TenantAdminAuthCache
  20. };
  21. use think\facade\Db;
  22. /**
  23. * 角色逻辑层
  24. * Class RoleLogic
  25. * @package app\tenantapi\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 = TenantSystemRole::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 TenantSystemRoleMenu)->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. TenantSystemRole::update([
  78. 'name' => $params['name'],
  79. 'desc' => $params['desc'] ?? '',
  80. 'sort' => $params['sort'] ?? 0,
  81. ], ['id' => $params['id']]);
  82. if (!empty($menuId)) {
  83. TenantSystemRoleMenu::where(['role_id' => $params['id']])->delete();
  84. $data = [];
  85. foreach ($menuId as $item) {
  86. $data[] = [
  87. 'role_id' => $params['id'],
  88. 'menu_id' => $item,
  89. ];
  90. }
  91. (new TenantSystemRoleMenu)->insertAll($data);
  92. }
  93. (new TenantAdminAuthCache())->deleteTag();
  94. Db::commit();
  95. return true;
  96. } catch (\Exception $e) {
  97. Db::rollback();
  98. self::$error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. /**
  103. * @notes 删除角色
  104. * @param int $id
  105. * @return bool
  106. * @author 段誉
  107. * @date 2021/12/29 14:16
  108. */
  109. public static function delete(int $id)
  110. {
  111. TenantSystemRole::destroy(['id' => $id]);
  112. (new TenantAdminAuthCache())->deleteTag();
  113. return true;
  114. }
  115. /**
  116. * @notes 角色详情
  117. * @param int $id
  118. * @return array
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @author 段誉
  123. * @date 2021/12/29 14:17
  124. */
  125. public static function detail(int $id): array
  126. {
  127. $detail = TenantSystemRole::field('id,name,desc,sort')->find($id);
  128. $authList = $detail->roleMenuIndex()->select()->toArray();
  129. $menuId = array_column($authList, 'menu_id');
  130. $detail['menu_id'] = $menuId;
  131. return $detail->toArray();
  132. }
  133. /**
  134. * @notes 角色数据
  135. * @return array
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @author 段誉
  140. * @date 2022/10/13 10:39
  141. */
  142. public static function getAllData()
  143. {
  144. return TenantSystemRole::order(['sort' => 'desc', 'id' => 'desc'])
  145. ->select()
  146. ->toArray();
  147. }
  148. }