GroupController.php 4.3 KB

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