GroupController.php 4.0 KB

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