Article.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $limit = empty($params['limit']) ? Config::get('app.page_size', 20) : (int)$params['limit'];
  68. $order = ['id desc'];
  69. if (!empty($params['order'])) {
  70. if (is_array($params['order'])) {
  71. $order = array_unique(array_merge($params['order']));
  72. } else {
  73. array_push($order, $params['order']);
  74. $order = array_unique($order);
  75. }
  76. }
  77. $list = self::where($where)
  78. ->field('id,cid,title,titlepic,username,summary,content_type,hits,sort,status,create_time')
  79. ->with(['category'])
  80. ->order($order)->paginate(['list_rows'=>$limit, 'query' => $params]);
  81. return $list;
  82. }
  83. public static function getListByCid($cid = 0, $limit = 10, $order = "")
  84. {
  85. $where = [];
  86. if ($cid != 0) {
  87. if ($cid < 0) {
  88. $where[] = ['cid', '<>', abs($cid)];
  89. } else {
  90. $where[] = ['cid', '=', $cid];
  91. }
  92. }
  93. $where[] = ['status', '=', 1];
  94. $order = $order ?? "sort ASC,id DESC";
  95. return self::with('category')->where($where)
  96. ->field('id,cid,title,titlepic,username,summary,hits,sort,status,create_time')
  97. ->order($order)->limit($limit)->select();
  98. }
  99. public static function getTop($limit)
  100. {
  101. return self::with('category')->field('id,cid,title,titlepic,summary,username,hits,sort,status,create_time')
  102. ->order('sort ASC, id DESC')->limit($limit)->select();
  103. }
  104. public static function getOne($id)
  105. {
  106. if (!$id) {
  107. throw new HttpException(404, '页面不存在');
  108. }
  109. return self::with('category')->find($id);
  110. }
  111. public static function getNextPrev($id, $cid = null)
  112. {
  113. $whereP = [];
  114. $whereN = [];
  115. $whereP[] = ['status', '=', 1];
  116. $whereN[] = ['status', '=', 1];
  117. if ($cid) {
  118. $whereP[] = ['cid', '=', $cid];
  119. $whereN[] = ['cid', '=', $cid];
  120. }
  121. $whereP[] = ['id', ">", $id];
  122. $whereN[] = ['id', "<", $id];
  123. $data_P = self::where($whereP)->order("id desc")->limit(1)->find();
  124. $data_N = self::where($whereN)->order("id desc")->limit(1)->find();
  125. return ['prev' => $data_P, 'next' => $data_N];
  126. }
  127. public static function createTimeArchive($limit = 0)
  128. {
  129. if ($limit == 0) {
  130. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->select();
  131. } else {
  132. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->limit($limit)->select();
  133. }
  134. return $timeList;
  135. }
  136. public static function getKeywordsById(int $id) {
  137. $article = self::where("id", $id)->find();
  138. return $article->keywords;
  139. }
  140. }