ActionLogic.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\ActionModel;
  4. use app\model\CommonModel;
  5. use think\exception\ValidateException;
  6. use think\facade\Db;
  7. use think\response\Json;
  8. class ActionLogic extends BaseLogic
  9. {
  10. //获取权限列表
  11. public static function list(int $menuid = 0): Json
  12. {
  13. $data = ActionModel::alias('a')
  14. ->field('a.*,action_name')
  15. ->leftJoin('action_list l', 'a.action_code=l.action_code')
  16. ->where(['menuid' => $menuid])
  17. ->select()
  18. ->toArray();
  19. return json_show(CommonModel::$success, '获取成功', $data);
  20. }
  21. //添加权限
  22. public static function add(array $data = []): Json
  23. {
  24. $rs = ActionModel::field('id')
  25. ->where(['menuid' => $data['menuid'], 'action_code' => $data['action_code']])
  26. ->findOrEmpty()
  27. ->isEmpty();
  28. if (!$rs) throw new ValidateException('此功能已存在');
  29. $data = array_merge($data, [
  30. 'addtime' => date('Y-m-d H:i:s'),
  31. 'updatetime' => date('Y-m-d H:i:s'),
  32. ]);
  33. return ActionModel::create($data)->save() ? json_show(CommonModel::$success, '添加成功') : json_show(CommonModel::$error_default, '添加失败');
  34. }
  35. //获取权限详情
  36. public static function read(int $id = 0): Json
  37. {
  38. $menu = ActionModel::alias('a')
  39. ->field('a.*,action_name')
  40. ->leftJoin('action_list l', 'l.is_show = ' . CommonModel::$del_normal . ' AND a.action_code=l.action_code')
  41. ->where('a.id', $id)
  42. ->where('')
  43. ->findOrEmpty()
  44. ->toArray();
  45. return empty($menu) ? json_show(CommonModel::$error_param, '未找到对应的数据') : json_show(CommonModel::$success, '获取成功!', $menu);
  46. }
  47. //编辑权限
  48. public static function edit(array $data = []): Json
  49. {
  50. $rs = ActionModel::field(true)
  51. ->where('id', $data['id'])
  52. ->findOrEmpty();
  53. if ($rs->isEmpty()) throw new ValidateException('该菜单下的按钮不存在');
  54. if ($rs->action_code != $data['action_code']) {
  55. $temp = ActionModel::field('id')
  56. ->where(['menuid' => $rs->menuid, 'action_code' => $data['action_code']])
  57. ->findOrEmpty()
  58. ->isEmpty();
  59. if (!$temp) throw new ValidateException('该按钮已存在');
  60. }
  61. $data['updatetime'] = date('Y-m-d H:i:s');
  62. $res = ActionModel::where('id', $data['id'])
  63. ->save($data);
  64. return $res ? json_show(CommonModel::$success, '修改按钮成功') : json_show(CommonModel::$error_param, '修改按钮失败');
  65. }
  66. //删除权限
  67. public static function delete(int $id = 0): Json
  68. {
  69. $rs = ActionModel::where('id', $id)->delete();
  70. return $rs ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
  71. }
  72. //启禁用权限
  73. public static function status(array $data = []): Json
  74. {
  75. $rs = ActionModel::where(['id' => $data['id']])
  76. ->where('status', '<>', $data['status'])
  77. ->save(['status' => $data['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  78. return $rs ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_default, '操作失败');
  79. }
  80. }