RoleLogic.php 6.4 KB

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