| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- /**
- * Created by PhpStorm.
- * User: leo
- * Date: 2018/2/24
- * Time: 下午12:48
- */
- namespace backendApi\modules\v1\controllers;
- use common\helpers\Cache;
- use common\helpers\Form;
- use common\models\Article;
- use common\models\ArticleCategory;
- use common\models\forms\ArticleCategoryForm;
- use common\models\forms\ArticleForm;
- use common\models\forms\UploadForm;
- use Yii;
- use yii\web\UploadedFile;
- class ArticleController extends BaseController
- {
- public $modelClass = Article::class;
- public function behaviors() {
- $behaviors = parent::behaviors();
- //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
- return $behaviors;
- }
- /**
- * 文章分类
- * @return mixed
- * @throws \yii\web\HttpException
- */
- public function actionCategory(){
- $data = ArticleCategory::lists('', [], [
- 'orderBy' => '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, Yii::t('ctx', 'successfully'));
- }
- }
- /**
- * 删除文章分类
- * @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(Yii::t('ctx', 'successfully'));
- } else {
- return static::notice(Yii::t('ctx', 'failed'), 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, Yii::t('ctx', 'successfully'));
- }
- // 获取全部分类
- $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, Yii::t('ctx', 'successfully'));
- }
- $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\db\Exception
- * @throws \yii\web\HttpException
- */
- public function actionArticleHide(){
- $adForm = new ArticleForm();
- $result = static::hide(Article::class, 'hide', function ($selected) use ($adForm) {
- }, function ($selected) use ($adForm) {
- }, true);
- return $result;
- }
- /**
- * 取消隐藏
- * @return mixed
- * @throws \yii\db\Exception
- * @throws \yii\web\HttpException
- */
- public function actionArticleUnHide(){
- $adForm = new ArticleForm();
- $result = static::hide(Article::class, 'un-hide', function ($selected) use ($adForm) {
- }, function ($selected) use ($adForm) {
- }, true);
- 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->FILE_NAME);
- } 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(Yii::t('ctx', 'successfully'));
- } else {
- return static::notice(Yii::t('ctx', 'failed'), 400);
- }
- }
- }
- }
|