'SORT ASC, CREATED_AT DESC', ]); return static::notice($data); } /** * 添加分类 * @return mixed * @throws \yii\web\HttpException */ public function actionCategoryAdd(){ if(Yii::$app->request->isPost) { return parent::edit(ArticleCategoryForm::class, 'Successful'); } } /** * 删除文章分类 * @return mixed * @throws \yii\db\Exception * @throws \yii\web\HttpException */ public function actionCategoryDelete(){ $result = static::delete(ArticleCategory::class); return $result; } /** * 分类排序 * @return mixed * @throws \yii\web\HttpException */ public function actionCategorySort(){ if(Yii::$app->request->get('id')){ $formModel = new ArticleCategoryForm(); $formModel->scenario = 'sort'; if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){ return static::notice('Successful'); } else { return static::notice('Fail', 400); } } } /** * 文章 * @return mixed * @throws \yii\web\HttpException */ public function actionIndex(){ $data = Article::lists('', [], [ 'select' => 'ID,TITLE,CID,STATUS,SORT,CREATED_AT', 'orderBy' => 'SORT ASC,CREATED_AT DESC', ]); // 全部分类 $data['allCategory'] = ArticleCategory::getAllCategory(); return static::notice($data); } /** * 添加文章 * @return mixed * @throws \yii\web\HttpException */ public function actionAdd(){ if(Yii::$app->request->isPost) { return parent::edit(ArticleForm::class, 'Successful'); } // 获取全部分类 $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all(); return static::notice(['allCategory'=>$allCategory]); } /** * 编辑文章 * @return mixed * @throws \yii\base\Exception * @throws \yii\web\HttpException */ public function actionEdit(){ $id = Yii::$app->request->get('id'); if(Yii::$app->request->isPost){ return parent::edit(ArticleForm::class, 'Successful'); } $oneData = Article::findOneAsArray(['ID'=>$id]); $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : ''; // 暂时先从文件中取内容 $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID']; if(!file_exists($path)){ $oneData['CONTENT'] = ''; } else { $oneData['CONTENT'] = file_get_contents($path); } // 获取全部分类 $allCategory = ArticleCategory::getAllCategory(); return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]); } /** * 删除文章 * @return mixed * @throws \yii\db\Exception * @throws \yii\web\HttpException */ public function actionArticleDelete(){ $result = static::delete(Article::class); return $result; } /** * 获取文章详细 * @return mixed * @throws \yii\web\HttpException */ public function actionDetail() { $id = \Yii::$app->request->get('id'); $data = null; if($id){ $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,TITLE,CID,SORT,CREATED_AT'); } if($data){ // 暂时先从文件中取内容 $path = \Yii::getAlias('@common/runtime/articleContent/').$data['ID']; if(!file_exists($path)){ $data['CONTENT'] = ''; } else { $data['CONTENT'] = file_get_contents($path); } return static::notice($data); } else { return static::notice('Article not exists!', 400); // 文章不存在 } } /** * 上传图片 * @return mixed * @throws \yii\base\Exception * @throws \yii\db\Exception * @throws \yii\web\HttpException */ public function actionUpload() { if (\Yii::$app->request->isPost) { $formModel = new UploadForm(); $formModel->scenario = 'article'; $formModel->file = UploadedFile::getInstanceByName('file'); $formModel->token = \Yii::$app->request->request('uploadToken');; if($formModel->file && $uploader = $formModel->upload()){ return static::notice($uploader->URL); } else { return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400); } } else { $token = Cache::setUploadToken(); return static::notice($token); } } /** * 排序 * @return mixed * @throws \yii\web\HttpException */ public function actionSort() { if (Yii::$app->request->get('id')) { $formModel = new ArticleForm(); $formModel->scenario = 'sort'; if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) { return static::notice('Success'); } else { return static::notice('Fail', 400); } } } }