GroupController.php 4.3 KB

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