| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace app\system\controller;
- use app\BaseController;
- use app\model\api\GroupModel;
- use think\facade\Request;
- use think\route\dispatch\Controller;
- use think\facade\View;
- use think\model;
- class GroupController extends BaseController
- {
- /**
- * 获取分组列表
- * @return string
- * @throws \think\db\exception\DbException
- */
- public function index()
- {
- $params = input('get.');
- $title = isset($params['title']) ? $params['title'] : '';
- $page = isset($params['page']) ? intval($params['page']) : 1;
- $pagesize = isset($params['pagesize']) ? intval($params['pagesize']) : 10;
- $groupModel = new GroupModel();
- $query = $groupModel->order('gr_id ASC');
- if (!empty($title)) {
- $query->whereLike('title', '%' . $title . '%');
- }
- $list = $query->paginate([
- 'page' => $page,
- 'list_rows' => $pagesize,
- ]);
- $data = [
- 'list' => $list->items(),
- 'total' => $list->total(),
- 'page' => $list->currentPage(),
- 'pagesize' => $list->listRows(),
- ];
- return json([
- 'success' => true,
- 'data' => $data,
- ]);
- }
- // /**
- // * 添加分组
- // *
- // * @return mixed
- // */
- // public function add(GroupModel $groupModel)
- // {
- // if ($this->request->isPost()) {
- // $data = $this->request->post();
- // $result = $groupModel->save($data);
- // if ($result) {
- // return $this->success('添加成功', url('index'));
- // } else {
- // return $this->error('添加失败');
- // }
- // } else {
- // return View::fetch();
- // }
- // }
- /**
- * 编辑分组
- *
- * @param int $id 分组ID
- * @return mixed
- */
- public function edit($id)
- {
- if (!$id) {
- return ['code' => -1, 'msg' => '参数错误'];
- }
- $groupModel = new GroupModel();
- $group = $groupModel->find($id);
- if (!$group) {
- return ['code' => -1, 'msg' => '分组不存在'];
- }
- if ($this->request->isPost()) {
- $name = $this->request->post('name/s', '');
- $status = $this->request->post('status/d', 0);
- if (!$name) {
- return ['code' => -1, 'msg' => '分组名称不能为空'];
- }
- $data = [
- 'gr_name' => $name,
- 'gr_status' => $status,
- ];
- $result = $groupModel->edit($id, $data);
- if ($result) {
- return ['code' => 0, 'msg' => '保存成功'];
- } else {
- return ['code' => -1, 'msg' => '保存失败'];
- }
- }
- return View::fetch('edit', ['group' => $group]);
- }
- /**
- * 修改分组审核状态
- *
- * @param Request $request
- * @return mixed|\think\response\Json
- */
- public function changeStatus(Request $request)
- {
- $id = $request->param('id/d', 0);
- if (!$id) {
- return json(['code' => -1, 'msg' => '参数错误']);
- }
- $groupModel = new GroupModel();
- $group = $groupModel->find($id);
- if (!$group) {
- return json(['code' => -1, 'msg' => '分组不存在']);
- }
- $status = $request->param('status/d', 0);
- $data = [
- 'audit' => $status,
- ];
- $result = $groupModel->where('id', $id)->update($data);
- if ($result) {
- return json(['code' => 0, 'msg' => '保存成功']);
- } else {
- return json(['code' => -1, 'msg' => '保存失败']);
- }
- }
- /**
- * 删除分组
- *
- * @param Request $request
- * @return mixed|\think\response\Json
- */
- public function delete(Request $request)
- {
- $id = $request->param('id/d', 0);
- if (!$id) {
- return json(['code' => -1, 'msg' => '参数错误']);
- }
- $groupModel = new GroupModel();
- $result = $groupModel->where('gr_id', $id)->delete();
- if ($result) {
- return json(['code' => 0, 'msg' => '删除成功']);
- } else {
- return json(['code' => -1, 'msg' => '删除失败']);
- }
- }
- }
|