GroupController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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('title', '%' . $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|\think\response\Json
  42. */
  43. public function add(GroupModel $groupModel)
  44. {
  45. if (Request::isPost()) {
  46. $data = Request::post();
  47. $result = $groupModel->save($data);
  48. if ($result) {
  49. return app('json')->success('添加成功');
  50. } else {
  51. return app('json')->fail('添加失败');
  52. }
  53. } else {
  54. return app('json')->fail('请求方式错误');
  55. }
  56. }
  57. /**
  58. * 编辑分组
  59. *
  60. * @param int $gr_id 分组ID
  61. * @return mixed
  62. */
  63. public function edit($gr_id)
  64. {
  65. if (!$gr_id) {
  66. return json(['code' => -1, 'msg' => '参数错误']);
  67. }
  68. $groupModel = new GroupModel();
  69. $group = $groupModel->find($gr_id);
  70. if (!$group) {
  71. return json(['code' => -1, 'msg' => '分组不存在']);
  72. }
  73. if ($this->request->isPost()) {
  74. $data = [
  75. 'uname' => $group['uname'],
  76. 'audit' => $group['audit'],
  77. ];
  78. $result = $groupModel->edit($gr_id, $data);
  79. if ($result) {
  80. return json(['code' => 0, 'msg' => '保存成功']);
  81. } else {
  82. return json(['code' => -1, 'msg' => '保存失败']);
  83. }
  84. }
  85. return json(['code' => 0, 'data' => $group]);
  86. }
  87. // /**
  88. // * 修改审核状态
  89. // *
  90. // * @return mixed|\think\response\Json
  91. // */
  92. // public function changeStatus()
  93. // {
  94. // $id = Request::param('gr_id', 0, '');
  95. // if (!$id) {
  96. // return app('json')->fail('参数错误');
  97. // }
  98. //
  99. // $groupModel = new GroupModel();
  100. // $group = $groupModel->find($id);
  101. // if (!$group) {
  102. // return app('json')->fail('分组不存在');
  103. // }
  104. //
  105. // $status = Request::param('audit', 0, '');
  106. // $data = [
  107. // 'audit' => $status,
  108. // ];
  109. //
  110. // $result = $groupModel->where('gr_id', $id)->update($data);
  111. //
  112. // if ($result) {
  113. // return app('json')->success('保存成功');
  114. // } else {
  115. // return app('json')->fail('保存失败');
  116. // }
  117. // }
  118. /**
  119. * 修改审核状态
  120. * @return mixed
  121. */
  122. public function changeStatus()
  123. {
  124. $gr_id = Request::param('gr_id');
  125. $audit = Request::param('audit');
  126. if($audit == 1){
  127. $audit = 0;
  128. }else{
  129. $audit = 1;
  130. }
  131. var_dump('审核状态:',$audit);
  132. var_dump('id:',$gr_id);
  133. $courseModel = new GroupModel();
  134. $result = $courseModel->updateAuditStatus($gr_id, ['audit' => $audit]);
  135. if ($result !== false) {
  136. return app('json')->success();
  137. } else {
  138. return app('json')->fail();
  139. }
  140. }
  141. /**
  142. * 修改是否轮播状态
  143. *
  144. * @return mixed|\think\response\Json
  145. */
  146. public function changeRecommend()
  147. {
  148. $id = Request::param('gr_id/d', 0);
  149. if (!$id) {
  150. return app('json')->fail('参数错误');
  151. }
  152. $groupModel = new GroupModel();
  153. $group = $groupModel->find($id);
  154. if (!$group) {
  155. return app('json')->fail('分组不存在');
  156. }
  157. $recommend = Request::param('recommend/d', 0);
  158. $data = [
  159. 'recommend' => $recommend,
  160. ];
  161. $result = $groupModel->where('gr_id', $id)->update($data);
  162. if ($result) {
  163. return app('json')->success('保存成功');
  164. } else {
  165. return app('json')->fail('保存失败');
  166. }
  167. }
  168. /**
  169. * 删除分组
  170. *
  171. * @param Request $request
  172. * @return mixed|\think\response\Json
  173. */
  174. public function delete(Request $request)
  175. {
  176. $id = $request->param('id/d', 0);
  177. if (!$id) {
  178. return json(['code' => -1, 'msg' => '参数错误']);
  179. }
  180. $groupModel = new GroupModel();
  181. $result = $groupModel->where('gr_id', $id)->delete();
  182. if ($result) {
  183. return json(['code' => 0, 'msg' => '删除成功']);
  184. } else {
  185. return json(['code' => -1, 'msg' => '删除失败']);
  186. }
  187. }
  188. }