RoleLogic.php 6.9 KB

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