GroupController.php 4.1 KB

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