*/ namespace app\admin\controller; use app\model\AdminApp; use app\model\AdminAppGroup; use app\util\ReturnCode; use app\util\Tools; class AppGroup extends Base { /** * 获取应用组列表 * @return array * @throws \think\exception\DbException * @author zhaoxiang */ public function index() { $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT')); $start = $this->request->get('page', 1); $keywords = $this->request->get('keywords', ''); $type = $this->request->get('type', ''); $status = $this->request->get('status', ''); $obj = new AdminAppGroup(); if (strlen($status)) { $obj = $obj->where('status', $status); } if ($type) { switch ($type) { case 1: $obj = $obj->where('hash', $keywords); break; case 2: $obj = $obj->whereLike('name', "%{$keywords}%"); break; } } $listObj = $obj->paginate($limit, false, ['page' => $start])->toArray(); return $this->buildSuccess([ 'list' => $listObj['data'], 'count' => $listObj['total'] ]); } /** * 获取全部有效的应用组 * @author zhaoxiang * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getAll() { $listInfo = (new AdminAppGroup())->where(['status' => 1])->select(); return $this->buildSuccess([ 'list' => $listInfo ]); } /** * 应用组状态编辑 * @return array * @author zhaoxiang */ public function changeStatus() { $id = $this->request->get('id'); $status = $this->request->get('status'); $res = AdminAppGroup::get($id); if ($res['is_official'] == 1) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '官方数据,禁止操作'); } else { $res = AdminAppGroup::update([ 'status' => $status ], [ 'id' => $id ]); } if ($res === false) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); } else { return $this->buildSuccess([]); } } /** * 添加应用组 * @author zhaoxiang * @return array */ public function add() { $postData = $this->request->post(); $res = AdminAppGroup::create($postData); if ($res === false) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); } else { return $this->buildSuccess([]); } } /** * 应用组编辑 * @author zhaoxiang * @return array */ public function edit() { $postData = $this->request->post(); $res = AdminAppGroup::get($postData['id']); if ($res['is_official'] == 1) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '官方数据,禁止操作'); } else { AdminAppGroup::update($postData); } if ($res === false) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); } else { return $this->buildSuccess([]); } } /** * 应用组删除 * @author zhaoxiang * @return array */ public function del() { $hash = $this->request->get('hash'); if (!$hash) { return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); } $has = (new AdminApp())->where(['app_group' => $hash])->count(); if ($has) { return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '当前分组存在' . $has . '个应用,禁止删除'); } AdminAppGroup::destroy(['hash' => $hash, 'is_official' => 0]); return $this->buildSuccess([]); } }