NewsWeirdo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:42
  7. */
  8. namespace common\models;
  9. class NewsWeirdo extends Common
  10. {
  11. public function rules()
  12. {
  13. return [
  14. ['thumb','required','message'=>'不能为空','on'=>'add'],
  15. [['subject','abstract','source','content'],'required','message'=>'{attribute}不能为空'],
  16. [['sort','click','click_true'],'number'],
  17. ['thumb','string','max'=>50],
  18. ['author','string','max'=>30],
  19. // ['open_time','date', 'format'=>'yyyy-mm-dd','message'=>'时间格式为:1990-01-01'],
  20. ['short_subject','string','max'=>50],
  21. ];
  22. }
  23. public function attributeLabels()
  24. {
  25. return [
  26. 'thumb'=>'缩略图',
  27. 'subject'=>'资讯标题',
  28. 'abstract'=>'简单描述',
  29. 'author'=>'作者',
  30. 'clicks'=>'点击数',
  31. 'content'=>'资讯内容',
  32. 'source'=>'来源',
  33. 'short_subject'=>'短标题'
  34. ];
  35. }
  36. public function FindById($id)
  37. {
  38. return self::findOne($id);
  39. }
  40. public function Authenticator($input)
  41. {
  42. $this->load($input,'');
  43. if($this->validate()) return $this;
  44. return $this->errors;
  45. }
  46. public function getList($page)
  47. {
  48. $query = self::find();
  49. $query->andWhere(['del'=>$this->setDel]);
  50. $query->andFilterWhere(['like','subject',$this->subject]);
  51. if(!empty($page['page']))
  52. {
  53. $query->offset = ($page['page'] - 1) * $page['limit'];
  54. $query->limit = $page['limit'];
  55. }
  56. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  57. }
  58. public function getListTotal()
  59. {
  60. $query = self::find();
  61. $query->andWhere(['del'=>$this->setDel]);
  62. $query->andFilterWhere(['like','subject',$this->subject]);
  63. return $query->count();
  64. }
  65. //前台数据展示
  66. public function HomeList($pages)
  67. {
  68. $query = self::find();
  69. $query->andWhere(['is_show'=>1]);
  70. $query->andWhere(['del'=>$this->setDel]);
  71. $query->select(['subject','click','abstract','thumb','short_subject','id']);
  72. if(is_object($pages))
  73. {
  74. $query->offset($pages->offset);
  75. $query->limit($pages->limit);
  76. }
  77. $query->orderBy(['click'=>SORT_DESC,'create_at'=>SORT_DESC]);
  78. return $query->asArray()->all();
  79. }
  80. public function HomeListTotal()
  81. {
  82. $query = self::find();
  83. $query->andWhere(['is_show'=>1]);
  84. $query->andWhere(['del'=>$this->setDel]);
  85. return $query->count();
  86. }
  87. //移动端
  88. public function Mhomelist($page)
  89. {
  90. $query = self::find();
  91. $query->andWhere(['is_show'=>1]);
  92. $query->andWhere(['del'=>$this->setDel]);
  93. $query->select(['subject','click','abstract','thumb','short_subject','id']);
  94. if(!empty($page['page']))
  95. {
  96. $query->offset = ($page['page'] - 1) * $page['limit'];
  97. $query->limit = $page['limit'];
  98. }
  99. $query->orderBy(['click'=>SORT_DESC,'create_at'=>SORT_DESC]);
  100. return $query->asArray()->all();
  101. }
  102. // public function RandList($limit,$select = null)
  103. // {
  104. // $query = self::find();
  105. // $query->andWhere(['is_show'=>1]);
  106. // $query->andWhere(['del'=>$this->setDel]);
  107. //// ['subject','thumb','short_subject','id']
  108. // $query->select($select);
  109. // $query->limit = $limit;
  110. // $query->orderBy('RAND()');
  111. // return $query->asArray()->all();
  112. // }
  113. public function Clickadd($id)
  114. {
  115. $query = self::findOne($id);
  116. $query->click = ++$query->click;
  117. $query->click_true = ++$query->click_true;
  118. return $query->save();
  119. }
  120. }