ArticleController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $news = ArticleCategory::find()->select('ID,CATE_NAME')->asArray()->all();
  24. foreach ($news as &$value){
  25. $value['LISTS']=Article::find()
  26. ->select('ID,TITLE,CID,CREATED_AT')
  27. ->where('CID=:CID AND STATUS=1 AND COUNTRY_ID=:COUNTRY_ID', [':CID'=>$value['ID'], ':COUNTRY_ID'=>$baseInfo['COUNTRY_ID']])
  28. ->orderBy('SORT DESC, CREATED_AT DESC')
  29. ->limit(5)
  30. ->asArray()
  31. ->all();
  32. }
  33. return static::notice($news);
  34. }
  35. /**
  36. * 文章分类
  37. * @return mixed
  38. * @throws \yii\web\HttpException
  39. */
  40. public function actionCategory(){
  41. return static::notice(ArticleCategory::getAllCategory());
  42. }
  43. /**文章前10条列表
  44. * @return mixed
  45. * @throws \yii\web\HttpException
  46. */
  47. public function actionGetNewArticle(){
  48. $data = Article::find()->select('ID,TITLE,CID,CREATED_AT')->where('STATUS=1')->orderBy('CREATED_AT DESC')->limit(10)->asArray()->all();
  49. return static::notice($data);
  50. }
  51. /**
  52. * 文章列表
  53. * @return mixed
  54. * @throws \yii\web\HttpException
  55. */
  56. public function actionList()
  57. {
  58. $userInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  59. $cid = \Yii::$app->request->get('cid');
  60. $condition = ' AND A.STATUS=1 AND A.COUNTRY_ID=:COUNTRY_ID';
  61. $params = [':COUNTRY_ID' => $userInfo['COUNTRY_ID']];
  62. if($cid){
  63. $condition .= ' AND CID=:CID';
  64. $params[':CID'] = $cid;
  65. }
  66. $data = Article::lists($condition, $params, [
  67. 'select' => 'A.ID,A.TITLE,A.CID,A.CREATED_AT,C.CATE_NAME',
  68. 'from' => Article::tableName().' AS A',
  69. 'join' => [
  70. ['LEFT JOIN', ArticleCategory::tableName() . ' AS C', 'A.CID = C.ID'],
  71. ],
  72. 'orderBy' => 'A.CREATED_AT DESC',
  73. 'useSlaves' => true,
  74. ]);
  75. // 全部分类
  76. $data['allCategory'] = ArticleCategory::getAllCategory();
  77. return static::notice($data);
  78. }
  79. /**
  80. * 获取文章详细
  81. * @return mixed
  82. * @throws \yii\web\HttpException
  83. */
  84. public function actionDetail(){
  85. $id = \Yii::$app->request->get('id');
  86. $data = null;
  87. if($id){
  88. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,TITLE,CID,CREATED_AT');
  89. }
  90. if($data){
  91. // 暂时先从文件中取内容
  92. $path = \Yii::getAlias('@common/runtime/articleContent/').$data['ID'];
  93. if(!file_exists($path)){
  94. $data['CONTENT'] = '';
  95. } else {
  96. $data['CONTENT'] = file_get_contents($path);
  97. }
  98. return static::notice($data);
  99. } else {
  100. return static::notice(Yii::t('app', 'articleDoesNotExist'), 400);// 文章不存在
  101. }
  102. }
  103. }