Article.php 5.3 KB

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