ArticleController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use common\models\Article;
  10. use common\models\ArticleCategory;
  11. use common\models\forms\ArticleCategoryForm;
  12. use common\models\forms\ArticleForm;
  13. use Yii;
  14. class ArticleController extends BaseController
  15. {
  16. public $modelClass = Article::class;
  17. public function behaviors() {
  18. $behaviors = parent::behaviors();
  19. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  20. return $behaviors;
  21. }
  22. /**
  23. * 文章分类
  24. * @return mixed
  25. * @throws \yii\web\HttpException
  26. */
  27. public function actionCategory(){
  28. $data = ArticleCategory::lists('', [], [
  29. 'orderBy' => 'SORT ASC, CREATED_AT DESC',
  30. ]);
  31. return static::notice($data);
  32. }
  33. /**
  34. * 添加分类
  35. * @return mixed
  36. * @throws \yii\web\HttpException
  37. */
  38. public function actionCategoryAdd(){
  39. if(Yii::$app->request->isPost) {
  40. return parent::edit(ArticleCategoryForm::class, '文章分类添加成功');
  41. }
  42. }
  43. /**
  44. * 删除文章分类
  45. * @return mixed
  46. * @throws \yii\db\Exception
  47. * @throws \yii\web\HttpException
  48. */
  49. public function actionCategoryDelete(){
  50. $result = static::delete(ArticleCategory::class);
  51. return $result;
  52. }
  53. /**
  54. * 分类排序
  55. * @return mixed
  56. * @throws \yii\web\HttpException
  57. */
  58. public function actionCategorySort(){
  59. if(Yii::$app->request->get('id')){
  60. $formModel = new ArticleCategoryForm();
  61. $formModel->scenario = 'sort';
  62. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  63. return static::notice('排序成功');
  64. } else {
  65. return static::notice('排序失败', 400);
  66. }
  67. }
  68. }
  69. /**
  70. * 文章
  71. * @return mixed
  72. * @throws \yii\web\HttpException
  73. */
  74. public function actionIndex(){
  75. $data = Article::lists('', [], [
  76. 'select' => 'ID,TITLE,CID,STATUS,CREATED_AT',
  77. 'orderBy' => 'CREATED_AT',
  78. ]);
  79. // 全部分类
  80. $data['allCategory'] = ArticleCategory::getAllCategory();
  81. return static::notice($data);
  82. }
  83. /**
  84. * 添加文章
  85. * @return mixed
  86. * @throws \yii\web\HttpException
  87. */
  88. public function actionAdd(){
  89. if(Yii::$app->request->isPost) {
  90. return parent::edit(ArticleForm::class, '文章添加成功');
  91. }
  92. // 获取全部分类
  93. $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
  94. return static::notice(['allCategory'=>$allCategory]);
  95. }
  96. /**
  97. * 编辑文章
  98. * @return mixed
  99. * @throws \yii\base\Exception
  100. * @throws \yii\web\HttpException
  101. */
  102. public function actionEdit(){
  103. $id = Yii::$app->request->get('id');
  104. if(Yii::$app->request->isPost){
  105. return parent::edit(ArticleForm::class, '编辑成功');
  106. }
  107. $oneData = Article::findOneAsArray(['ID'=>$id]);
  108. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  109. // 暂时先从文件中取内容
  110. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  111. if(!file_exists($path)){
  112. $oneData['CONTENT'] = '';
  113. } else {
  114. $oneData['CONTENT'] = file_get_contents($path);
  115. }
  116. // 获取全部分类
  117. $allCategory = ArticleCategory::getAllCategory();
  118. return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
  119. }
  120. /**
  121. * 删除文章
  122. * @return mixed
  123. * @throws \yii\db\Exception
  124. * @throws \yii\web\HttpException
  125. */
  126. public function actionArticleDelete(){
  127. $result = static::delete(Article::class);
  128. return $result;
  129. }
  130. }