request->isAjax) { if (Yii::$app->request->isGet) { $params = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['page', 'limit'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'status', StatusEnum::ENABLED]; $order = 'created_at desc'; $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']); $list = LinkCategory::listPage($params, false, true); foreach ($list['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } $this->success('获取成功', $list); } } else { return $this->render($this->action->id); } } /** * 链接分类编辑页 * @return mixed|string|void */ public function actionCateEdit() { $retuest = Yii::$app->request; if ($retuest->isAjax) { if ($retuest->isPost) { $params = Yii::$app->request->post('detail'); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['title','name'], 'required'], [['pid','is_list'], 'integer'], [['title','name'], 'string'], [['id'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 保存数据 $category = new LinkCategory(); $res = $category::setData($params); if ($res == false) return $this->error('保存失败,msg=' . $category->getError()); $this->success('编辑成功'); } else { $params = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['id'], 'integer'], [['id'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 获取所有分类 $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid,is_list,remark'); // 对分类进行树形拼接 $menu = LinkCategory::getTreeMenu($cate); // 获取数据 $detail = (object) array(); if (!empty($params['id'])) { $cate = array_column($cate, null, 'id'); $detail = $cate[$params['id']] ?? (object)[]; } $this->success('获取成功', compact('detail', 'menu')); } } else { return $this->render($this->action->id); } } /** * 链接分类删除 * @return mixed|void */ public function actionCateDelete() { $request = Yii::$app->request; if ($request->isPost) { try{ $params = $request->post(); $res = Yii::$app->services->validate->validate($params,[ [['id'], 'required'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $detail = LinkCategory::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]); if (empty($detail)) throw new \Exception("页面不存在"); $has_link = Link::findOne(['cat_id' => $detail->id]); if ($has_link) throw new \Exception("该分类下存在链接,不可删除"); $detail->status = StatusEnum::DELETE; if (!$detail->save()) throw new \Exception("删除失败"); $this->success('删除成功'); } catch (\Exception $e) { $this->error($e->getMessage()); } } } /** * 链接管理页 * @return mixed|string|void */ public function actionIndex() { if (Yii::$app->request->isAjax) { if (Yii::$app->request->isPost) { $params = Yii::$app->request->post(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['cat_id' ,'page', 'limit'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'status', StatusEnum::ENABLED]; $order = 'id asc'; $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']); $list = Link::listPage($params, false, true); if (empty($list)) $this->success('获取成功', $list); // 获取分类 $catName = LinkCategory::getCategoryName(); foreach ($list['list'] as &$item) { $item['cat_name'] = $catName[$item['cat_id']]; $item['h5_route'] = Yii::$app->request->hostInfo . '/h5/#' . $item['route']; } $this->success('获取成功', $list); } } else { return $this->render($this->action->id); } } /** * 链接编辑页 * @return mixed|string|void */ public function actionEdit() { $retuest = Yii::$app->request; if ($retuest->isAjax) { if ($retuest->isPost) { $params = Yii::$app->request->post('detail'); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['title','cat_id','open_type'], 'required'], [['title','open_type','route'], 'string'], [['id','route'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 判断是编辑还是新增数据 if (empty($params['id'])) { $detail = new Link(); } else { $detail = Link::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]); if (empty($detail)) $this->error('无法查询到分类数据'); } // 数据赋值 $detail->attributes = $params; if (!$detail->save()) $this->error('编辑失败'); $this->success('编辑成功'); } else { $params = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($params,[ [['id'], 'integer'], [['id'], 'safe'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 获取数据 $detail = []; if (!empty($params['id'])) { $detail = Link::find()->where(['id' => $params['id'], 'status' => StatusEnum::ENABLED])->asArray()->one(); } // 获取所有分类 $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid'); // 对分类进行树形拼接 $menu = LinkCategory::getTreeMenu($cate); // 类型 $open_type_list = Link::OPEN_TYPE; $this->success('获取成功', compact('detail', 'menu' ,'open_type_list')); } } else { return $this->render($this->action->id); } } }