GroupController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\system\controller;
  3. use app\BaseController;
  4. use app\model\api\GroupModel;
  5. use think\facade\Request;
  6. use think\route\dispatch\Controller;
  7. use think\facade\View;
  8. use think\model;
  9. class GroupController extends BaseController
  10. {
  11. /**
  12. * 获取分组列表
  13. * @return string
  14. * @throws \think\db\exception\DbException
  15. */
  16. public function index()
  17. {
  18. $params = input('get.');
  19. $title = isset($params['title']) ? $params['title'] : '';
  20. $page = isset($params['page']) ? intval($params['page']) : 1;
  21. $pagesize = isset($params['pagesize']) ? intval($params['pagesize']) : 10;
  22. $groupModel = new GroupModel();
  23. $query = $groupModel->with('user')->order('gr_id ASC');
  24. if (!empty($title)) {
  25. $query->whereLike('title', '%' . $title . '%');
  26. }
  27. $list = $query->paginate([
  28. 'page' => $page,
  29. 'list_rows' => $pagesize,
  30. ]);
  31. return app('json')->success($list);
  32. }
  33. // /**
  34. // * 添加分组
  35. // *
  36. // * @return mixed
  37. // */
  38. // public function add(GroupModel $groupModel)
  39. // {
  40. // if ($this->request->isPost()) {
  41. // $data = $this->request->post();
  42. // $result = $groupModel->save($data);
  43. // if ($result) {
  44. // return $this->success('添加成功', url('index'));
  45. // } else {
  46. // return $this->error('添加失败');
  47. // }
  48. // } else {
  49. // return View::fetch();
  50. // }
  51. // }
  52. /**
  53. * 编辑分组
  54. *
  55. * @param int $id 分组ID
  56. * @return mixed
  57. */
  58. public function edit($id)
  59. {
  60. if (!$id) {
  61. return ['code' => -1, 'msg' => '参数错误'];
  62. }
  63. $groupModel = new GroupModel();
  64. $group = $groupModel->find($id);
  65. if (!$group) {
  66. return ['code' => -1, 'msg' => '分组不存在'];
  67. }
  68. if ($this->request->isPost()) {
  69. $name = $this->request->post('name/s', '');
  70. $status = $this->request->post('status/d', 0);
  71. if (!$name) {
  72. return ['code' => -1, 'msg' => '分组名称不能为空'];
  73. }
  74. $data = [
  75. 'gr_name' => $name,
  76. 'gr_status' => $status,
  77. ];
  78. $result = $groupModel->edit($id, $data);
  79. if ($result) {
  80. return ['code' => 0, 'msg' => '保存成功'];
  81. } else {
  82. return ['code' => -1, 'msg' => '保存失败'];
  83. }
  84. }
  85. return View::fetch('edit', ['group' => $group]);
  86. }
  87. /**
  88. * 修改分组审核状态
  89. *
  90. * @param Request $request
  91. * @return mixed|\think\response\Json
  92. */
  93. public function changeStatus(Request $request)
  94. {
  95. $id = $request->param('id/d', 0);
  96. if (!$id) {
  97. return json(['code' => -1, 'msg' => '参数错误']);
  98. }
  99. $groupModel = new GroupModel();
  100. $group = $groupModel->find($id);
  101. if (!$group) {
  102. return json(['code' => -1, 'msg' => '分组不存在']);
  103. }
  104. $status = $request->param('status/d', 0);
  105. $data = [
  106. 'audit' => $status,
  107. ];
  108. $result = $groupModel->where('id', $id)->update($data);
  109. if ($result) {
  110. return json(['code' => 0, 'msg' => '保存成功']);
  111. } else {
  112. return json(['code' => -1, 'msg' => '保存失败']);
  113. }
  114. }
  115. /**
  116. * 删除分组
  117. *
  118. * @param Request $request
  119. * @return mixed|\think\response\Json
  120. */
  121. public function delete(Request $request)
  122. {
  123. $id = $request->param('id/d', 0);
  124. if (!$id) {
  125. return json(['code' => -1, 'msg' => '参数错误']);
  126. }
  127. $groupModel = new GroupModel();
  128. $result = $groupModel->where('gr_id', $id)->delete();
  129. if ($result) {
  130. return json(['code' => 0, 'msg' => '删除成功']);
  131. } else {
  132. return json(['code' => -1, 'msg' => '删除失败']);
  133. }
  134. }
  135. }