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