ArticleController.php 3.0 KB

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