ArticleController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\helpers\Cache;
  10. use common\helpers\Form;
  11. use common\models\Article;
  12. use common\models\ArticleCategory;
  13. use common\models\forms\ArticleCategoryForm;
  14. use common\models\forms\ArticleForm;
  15. use common\models\forms\UploadForm;
  16. use Yii;
  17. use yii\web\UploadedFile;
  18. class ArticleController extends BaseController
  19. {
  20. public $modelClass = Article::class;
  21. public function behaviors() {
  22. $behaviors = parent::behaviors();
  23. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  24. return $behaviors;
  25. }
  26. /**
  27. * 文章分类
  28. * @return mixed
  29. * @throws \yii\web\HttpException
  30. */
  31. public function actionCategory(){
  32. $data = ArticleCategory::lists('', [], [
  33. 'orderBy' => 'SORT ASC, CREATED_AT DESC',
  34. ]);
  35. return static::notice($data);
  36. }
  37. /**
  38. * 添加分类
  39. * @return mixed
  40. * @throws \yii\web\HttpException
  41. */
  42. public function actionCategoryAdd(){
  43. if(Yii::$app->request->isPost) {
  44. return parent::edit(ArticleCategoryForm::class, 'Successful');
  45. }
  46. }
  47. /**
  48. * 删除文章分类
  49. * @return mixed
  50. * @throws \yii\db\Exception
  51. * @throws \yii\web\HttpException
  52. */
  53. public function actionCategoryDelete(){
  54. $result = static::delete(ArticleCategory::class);
  55. return $result;
  56. }
  57. /**
  58. * 分类排序
  59. * @return mixed
  60. * @throws \yii\web\HttpException
  61. */
  62. public function actionCategorySort(){
  63. if(Yii::$app->request->get('id')){
  64. $formModel = new ArticleCategoryForm();
  65. $formModel->scenario = 'sort';
  66. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  67. return static::notice('Successful');
  68. } else {
  69. return static::notice('Fail', 400);
  70. }
  71. }
  72. }
  73. /**
  74. * 文章
  75. * @return mixed
  76. * @throws \yii\web\HttpException
  77. */
  78. public function actionIndex(){
  79. $data = Article::lists('', [], [
  80. 'select' => 'ID,TITLE,CID,STATUS,SORT,CREATED_AT',
  81. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  82. ]);
  83. // 全部分类
  84. $data['allCategory'] = ArticleCategory::getAllCategory();
  85. return static::notice($data);
  86. }
  87. /**
  88. * 添加文章
  89. * @return mixed
  90. * @throws \yii\web\HttpException
  91. */
  92. public function actionAdd(){
  93. if(Yii::$app->request->isPost) {
  94. return parent::edit(ArticleForm::class, 'Successful');
  95. }
  96. // 获取全部分类
  97. $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
  98. return static::notice(['allCategory'=>$allCategory]);
  99. }
  100. /**
  101. * 编辑文章
  102. * @return mixed
  103. * @throws \yii\base\Exception
  104. * @throws \yii\web\HttpException
  105. */
  106. public function actionEdit(){
  107. $id = Yii::$app->request->get('id');
  108. if(Yii::$app->request->isPost){
  109. return parent::edit(ArticleForm::class, 'Successful');
  110. }
  111. $oneData = Article::findOneAsArray(['ID'=>$id]);
  112. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  113. // 暂时先从文件中取内容
  114. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  115. if(!file_exists($path)){
  116. $oneData['CONTENT'] = '';
  117. } else {
  118. $oneData['CONTENT'] = file_get_contents($path);
  119. }
  120. // 获取全部分类
  121. $allCategory = ArticleCategory::getAllCategory();
  122. return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
  123. }
  124. /**
  125. * 删除文章
  126. * @return mixed
  127. * @throws \yii\db\Exception
  128. * @throws \yii\web\HttpException
  129. */
  130. public function actionArticleDelete(){
  131. $result = static::delete(Article::class);
  132. return $result;
  133. }
  134. /**
  135. * 获取文章详细
  136. * @return mixed
  137. * @throws \yii\web\HttpException
  138. */
  139. public function actionDetail()
  140. {
  141. $id = \Yii::$app->request->get('id');
  142. $data = null;
  143. if($id){
  144. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,TITLE,CID,SORT,CREATED_AT');
  145. }
  146. if($data){
  147. // 暂时先从文件中取内容
  148. $path = \Yii::getAlias('@common/runtime/articleContent/').$data['ID'];
  149. if(!file_exists($path)){
  150. $data['CONTENT'] = '';
  151. } else {
  152. $data['CONTENT'] = file_get_contents($path);
  153. }
  154. return static::notice($data);
  155. } else {
  156. return static::notice('Article not exists!', 400); // 文章不存在
  157. }
  158. }
  159. /**
  160. * 上传图片
  161. * @return mixed
  162. * @throws \yii\base\Exception
  163. * @throws \yii\db\Exception
  164. * @throws \yii\web\HttpException
  165. */
  166. public function actionUpload() {
  167. if (\Yii::$app->request->isPost) {
  168. $formModel = new UploadForm();
  169. $formModel->scenario = 'article';
  170. $formModel->file = UploadedFile::getInstanceByName('file');
  171. $formModel->token = \Yii::$app->request->request('uploadToken');;
  172. if($formModel->file && $uploader = $formModel->upload()){
  173. return static::notice($uploader->URL);
  174. } else {
  175. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  176. }
  177. } else {
  178. $token = Cache::setUploadToken();
  179. return static::notice($token);
  180. }
  181. }
  182. /**
  183. * 排序
  184. * @return mixed
  185. * @throws \yii\web\HttpException
  186. */
  187. public function actionSort()
  188. {
  189. if (Yii::$app->request->get('id')) {
  190. $formModel = new ArticleForm();
  191. $formModel->scenario = 'sort';
  192. if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) {
  193. return static::notice('Success');
  194. } else {
  195. return static::notice('Fail', 400);
  196. }
  197. }
  198. }
  199. }