RoleLogic.php 6.8 KB

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