Article.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\model;
  4. use think\exception\HttpException;
  5. use app\model\Base;
  6. use think\facade\Config;
  7. class Article extends Base
  8. {
  9. protected $pk = 'id';
  10. protected $schema = [
  11. "id" => "integer",
  12. "cid" => "integer",
  13. "title" => "varchar",
  14. "writer" => "varchar",
  15. "source" => "varchar",
  16. "titlepic" => "varchar",
  17. "keywords" => "varchar",
  18. "summary" => "varchar",
  19. "content" => "varchar",
  20. "discussed" => "integer",
  21. "status" => "integer",
  22. "top" => "integer",
  23. "sort" => "integer",
  24. "hits" => "integer",
  25. "likes" => "integer",
  26. "content_type"=> "integer",
  27. "userid" => "integer",
  28. "username" => "varchar",
  29. "create_time" => "int",
  30. "update_time" => "int"
  31. ];
  32. protected $disuse = ['top','discussed'];
  33. public function category()
  34. {
  35. return $this->belongsTo('Category', 'cid')->bind(['category_name' => 'name', 'category_url' => 'url', 'route' => 'route']);
  36. }
  37. public static function queryPage($params)
  38. {
  39. $where = [];
  40. if (isset($params['status'])) {
  41. $where[] = ['status', '=', (int) $params['status']];
  42. }
  43. if (!empty($params['cid'])) {
  44. $where[] = ['cid', '=', (int) $params['cid']];
  45. }
  46. if (!empty($params['key'])) {
  47. $where[] = ['title|keywords', 'LIKE', '%' . (string)$params['key'] . '%'];
  48. }
  49. if (!empty($params['yearMonth'])) {
  50. if (!preg_match("/^([0-9]{4})\/([0-9]{1,2})$/", $params['yearMonth']) && !preg_match("/^([0-9]{4})-([0-9]{1,2})$/", $params['yearMonth'])) {
  51. throw new HttpException(404, '日期格式不正确');
  52. }
  53. $separator = strrpos('/', $params['yearMonth']) ? '/' : '-';
  54. $year = $month = '';
  55. if (strnatcasecmp(PHP_VERSION, '7.0.0') >= 0) {
  56. list($year, $month) = explode($separator, $params['yearMonth']);
  57. } else {
  58. list($month, $year) = explode($separator, $params['yearMonth']);
  59. }
  60. if ((int) $month < 1 || (int) $month > 12) {
  61. throw new HttpException(404, '日期格式不正确');
  62. }
  63. $days = month_frist_and_last_day($year, $month);
  64. // $where['create_time'] = ['between', [$days['firstday'], $days['lastday']]];
  65. $where[] = ['create_time','between', [$days['firstday'], $days['lastday']]];
  66. }
  67. if (!empty($params['year']) && !empty($params['month'])) {
  68. $days = month_frist_and_last_day($params['year'], $params['month']);
  69. $where[] = ['create_time','between', [$days['firstday'], $days['lastday']]];
  70. }
  71. $limit = empty($params['limit']) ? Config::get('app.page_size', 20) : (int)$params['limit'];
  72. $order = ['id desc'];
  73. if (!empty($params['order'])) {
  74. if (is_array($params['order'])) {
  75. $order = array_unique(array_merge($params['order']));
  76. } else {
  77. array_push($order, $params['order']);
  78. $order = array_unique($order);
  79. }
  80. }
  81. $list = self::where($where)
  82. ->field('id,cid,title,titlepic,username,summary,content_type,hits,sort,status,create_time')
  83. ->with(['category'])
  84. ->order($order)->paginate(['list_rows'=>$limit, 'query' => $params]);
  85. return $list;
  86. }
  87. public static function getListByCid($cid = 0, $limit = 10, $order = "")
  88. {
  89. $where = [];
  90. if ($cid != 0) {
  91. if ($cid < 0) {
  92. $where[] = ['cid', '<>', abs($cid)];
  93. } else {
  94. $where[] = ['cid', '=', $cid];
  95. }
  96. }
  97. $where[] = ['status', '=', 1];
  98. $order = $order ?? "sort ASC,id DESC";
  99. return self::with('category')->where($where)
  100. ->field('id,cid,title,titlepic,username,summary,hits,sort,status,create_time')
  101. ->order($order)->limit($limit)->select();
  102. }
  103. public static function getTop($limit)
  104. {
  105. return self::with('category')->field('id,cid,title,titlepic,summary,username,hits,sort,status,create_time')
  106. ->order('sort ASC, id DESC')->limit($limit)->select();
  107. }
  108. public static function getOne($id)
  109. {
  110. if (!$id) {
  111. throw new HttpException(404, '页面不存在');
  112. }
  113. return self::with('category')->find($id);
  114. }
  115. public static function getNextPrev($id, $cid = null)
  116. {
  117. $whereP = [];
  118. $whereN = [];
  119. $whereP[] = ['status', '=', 1];
  120. $whereN[] = ['status', '=', 1];
  121. if ($cid) {
  122. $whereP[] = ['cid', '=', $cid];
  123. $whereN[] = ['cid', '=', $cid];
  124. }
  125. $whereP[] = ['id', ">", $id];
  126. $whereN[] = ['id', "<", $id];
  127. $data_P = self::where($whereP)->order("id desc")->limit(1)->find();
  128. $data_N = self::where($whereN)->order("id desc")->limit(1)->find();
  129. return ['prev' => $data_P, 'next' => $data_N];
  130. }
  131. public static function createTimeArchive($limit = 0)
  132. {
  133. if ($limit == 0) {
  134. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->select();
  135. } else {
  136. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->limit($limit)->select();
  137. }
  138. return $timeList;
  139. }
  140. public static function getKeywordsById(int $id) {
  141. $article = self::where("id", $id)->find();
  142. return $article->keywords;
  143. }
  144. }