ArticleController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\models\Article;
  10. use common\models\ArticleCategory;
  11. use common\models\User;
  12. use Yii;
  13. class ArticleController extends BaseController
  14. {
  15. public $modelClass = Article::class;
  16. /**
  17. * 返回所有分类并带5条新闻
  18. * @return mixed
  19. * @throws \yii\web\HttpException
  20. */
  21. public function actionIndex()
  22. {
  23. $userInfo = User::getEnCodeInfo(Yii::$app->user->id);
  24. $news = ArticleCategory::find()->select('ID,CATE_NAME')->asArray()->all();
  25. foreach ($news as &$value){
  26. $value['LISTS']=Article::find()
  27. ->select('ID,TITLE,CID,CREATED_AT')
  28. ->where('CID=:CID AND STATUS=1 AND COUNTRY_ID=:COUNTRY_ID', [':CID'=>$value['ID'], ':COUNTRY_ID'=>$userInfo['COUNTRY_ID']])
  29. ->orderBy('SORT DESC, CREATED_AT DESC')
  30. ->limit(5)
  31. ->asArray()
  32. ->all();
  33. }
  34. return static::notice($news);
  35. }
  36. /**
  37. * 文章分类
  38. * @return mixed
  39. * @throws \yii\web\HttpException
  40. */
  41. public function actionCategory(){
  42. return static::notice(ArticleCategory::getAllCategory());
  43. }
  44. /**文章前10条列表
  45. * @return mixed
  46. * @throws \yii\web\HttpException
  47. */
  48. public function actionGetNewArticle(){
  49. $data = Article::find()->select('ID,TITLE,CID,CREATED_AT')->where('STATUS=1')->orderBy('CREATED_AT DESC')->limit(10)->asArray()->all();
  50. return static::notice($data);
  51. }
  52. /**
  53. * 文章列表
  54. * @return mixed
  55. * @throws \yii\web\HttpException
  56. */
  57. public function actionList()
  58. {
  59. $userInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  60. $cid = \Yii::$app->request->get('cid');
  61. $condition = ' AND A.STATUS=1 AND A.COUNTRY_ID=:COUNTRY_ID';
  62. $params = [':COUNTRY_ID' => $userInfo['COUNTRY_ID']];
  63. if($cid){
  64. $condition .= ' AND CID=:CID';
  65. $params[':CID'] = $cid;
  66. }
  67. $data = Article::lists($condition, $params, [
  68. 'select' => 'A.ID,A.TITLE,A.CID,A.CREATED_AT,C.CATE_NAME',
  69. 'from' => Article::tableName().' AS A',
  70. 'join' => [
  71. ['LEFT JOIN', ArticleCategory::tableName() . ' AS C', 'A.CID = C.ID'],
  72. ],
  73. 'orderBy' => 'A.CREATED_AT DESC',
  74. 'useSlaves' => true,
  75. ]);
  76. // 全部分类
  77. $data['allCategory'] = ArticleCategory::getAllCategory();
  78. return static::notice($data);
  79. }
  80. /**
  81. * 获取文章详细
  82. * @return mixed
  83. * @throws \yii\web\HttpException
  84. */
  85. public function actionDetail(){
  86. $id = \Yii::$app->request->get('id');
  87. $data = null;
  88. if($id){
  89. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,TITLE,CID,CREATED_AT');
  90. }
  91. if($data){
  92. // 暂时先从文件中取内容
  93. $path = \Yii::getAlias('@common/runtime/articleContent/').$data['ID'];
  94. if(!file_exists($path)){
  95. $data['CONTENT'] = '';
  96. } else {
  97. $data['CONTENT'] = file_get_contents($path);
  98. }
  99. return static::notice($data);
  100. } else {
  101. return static::notice(Yii::t('app', 'articleDoesNotExist'), 400);// 文章不存在
  102. }
  103. }
  104. }