RoleLogic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\RoleModel;
  5. use think\Exception;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use think\response\Json;
  9. class RoleLogic extends BaseLogic
  10. {
  11. //获取角色列表
  12. public static function list(array $data = []): Json
  13. {
  14. $db = RoleModel::where('is_del', CommonModel::$del_normal);
  15. if ($data['name'] != '') $db->whereLike('name', '%' . $data['name'] . '%');
  16. if ($data['level'] != '') $db->where('level', $data['level']);
  17. if ($data['status'] != '') $db->where('status', $data['status']);
  18. $count = $db->count('id');
  19. $list = $db
  20. ->field('id,name,level,status')
  21. ->page($data['page'], $data['size'])
  22. ->order(['id' => 'desc'])
  23. ->select()
  24. ->toArray();
  25. return json_show(CommonModel::$success, '获取角色列表成功', ['count' => $count, 'list' => $list]);
  26. }
  27. //获取全部角色
  28. public static function all(string $keyword = ''): Json
  29. {
  30. $db = RoleModel::where('is_del', CommonModel::$del_normal)
  31. ->field('id,name,level,status')
  32. ->order(['id' => 'desc']);
  33. if ($keyword != '') $db->whereLike('name', '%' . $keyword . '%');
  34. $list = $db
  35. ->select()
  36. ->toArray();
  37. return json_show(CommonModel::$success, '获取获取全部角色成功', $list);
  38. }
  39. //添加角色
  40. public static function add(array $data = []): Json
  41. {
  42. Db::startTrans();
  43. try {
  44. $rs = RoleModel::field('id')
  45. ->where(['is_del' => CommonModel::$del_normal, 'name' => $data['name']])
  46. ->findOrEmpty()
  47. ->isEmpty();
  48. if (!$rs) throw new Exception('该角色名已存在');
  49. $date = date('Y-m-d H:i:s');
  50. $roleid = Db::name('role')->strict(false)->insertGetId(array_merge($data, [
  51. 'status' => CommonModel::$status_normal,
  52. 'is_del' => CommonModel::$del_normal,
  53. 'creater' => self::$uname,
  54. 'createrid' => self::$uid,
  55. 'addtime' => $date,
  56. 'updater' => self::$uname,
  57. 'updaterid' => self::$uid,
  58. 'updatetime' => $date,
  59. ]));
  60. Db::name('role_action')->insert([
  61. 'roleid' => $roleid,
  62. 'action_data' => implode(',', $data['action_data']),
  63. 'status' => CommonModel::$status_normal,
  64. 'addtime' => $date,
  65. 'updatetime' => $date,
  66. ]);
  67. Db::commit();
  68. return json_show(CommonModel::$success, '添加角色成功');
  69. } catch (Exception $exception) {
  70. Db::rollback();
  71. return json_show(CommonModel::$error_default, $exception->getMessage());
  72. }
  73. }
  74. //获取角色详情
  75. public static function read(int $id = 0): Json
  76. {
  77. $rs = RoleModel::field(true)
  78. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  79. ->findOrEmpty()
  80. ->toArray();
  81. if (empty($rs)) throw new ValidateException('该角色详情为空');
  82. //已有的权限信息
  83. $rs['action'] = Db::name('role_action')
  84. ->where(['roleid' => $id, 'is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  85. ->value('action_data');
  86. $rs['action'] = explode(',', $rs['action']);
  87. return json_show(CommonModel::$success, '获取角色详情成功', $rs);
  88. }
  89. //编辑角色
  90. public static function edit(array $data = []): Json
  91. {
  92. Db::startTrans();
  93. try {
  94. $rs = RoleModel::field('id,name')
  95. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  96. ->findOrEmpty();
  97. if ($rs->isEmpty()) throw new Exception('该角色不存在');
  98. if ($rs->name != $data['name']) {
  99. $rs = RoleModel::field('id')
  100. ->where(['is_del' => CommonModel::$del_normal, 'name' => $data['name']])
  101. ->findOrEmpty()
  102. ->isEmpty();
  103. if (!$rs) throw new Exception('该角色名已存在');
  104. }
  105. $date = date('Y-m-d H:i:s');
  106. RoleModel::where('id', $data['id'])->strict(false)->save(array_merge($data, [
  107. 'updater' => self::$uname,
  108. 'updaterid' => self::$uid,
  109. 'updatetime' => $date,
  110. ]));
  111. Db::name('role_action')
  112. ->where('roleid', $data['id'])
  113. ->save([
  114. 'action_data' => implode(',', $data['action_data']),
  115. 'updatetime' => $date,
  116. ]);
  117. Db::commit();
  118. return json_show(CommonModel::$success, '编辑角色成功');
  119. } catch (Exception $exception) {
  120. Db::rollback();
  121. return json_show(CommonModel::$error_default, $exception->getMessage());
  122. }
  123. }
  124. //删除角色
  125. public static function delete(int $id = 0): Json
  126. {
  127. Db::startTrans();
  128. try {
  129. $date = date('Y-m-d H:i:s');
  130. RoleModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])->save([
  131. 'is_del' => CommonModel::$del_deleted,
  132. 'updater' => self::$uname,
  133. 'updaterid' => self::$uid,
  134. 'updatetime' => $date,
  135. ]);
  136. Db::name('role_action')
  137. ->where(['roleid' => $id, 'is_del' => CommonModel::$del_normal])
  138. ->save([
  139. 'is_del' => CommonModel::$del_deleted,
  140. 'updatetime' => $date,
  141. ]);
  142. Db::commit();
  143. return json_show(CommonModel::$success, '删除角色成功');
  144. } catch (Exception $exception) {
  145. Db::rollback();
  146. return json_show(CommonModel::$error_default, $exception->getMessage());
  147. }
  148. }
  149. //启禁用菜单
  150. public static function status(array $data = []): Json
  151. {
  152. Db::startTrans();
  153. try {
  154. $rs = RoleModel::field('id,name')
  155. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  156. ->where('status', '<>', $data['status'])
  157. ->findOrEmpty()
  158. ->isEmpty();
  159. if ($rs) throw new Exception('该角色不存在或重复操作');
  160. $date = date('Y-m-d H:i:s');
  161. RoleModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  162. ->where('status', '<>', $data['status'])
  163. ->save(array_merge($data, [
  164. 'updater' => self::$uname,
  165. 'updaterid' => self::$uid,
  166. 'updatetime' => $date,
  167. ]));
  168. Db::name('role_action')
  169. ->where('roleid', $data['id'])
  170. ->save([
  171. 'status' => $data['status'],
  172. 'updatetime' => $date,
  173. ]);
  174. Db::commit();
  175. return json_show(CommonModel::$success, '操作成功');
  176. } catch (Exception $exception) {
  177. Db::rollback();
  178. return json_show(CommonModel::$error_default, $exception->getMessage());
  179. }
  180. }
  181. }