Article.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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']);
  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($limit, false, ['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. $order = !empty($order) ? array_merge($order, ['id' => 'desc']) : ['id' => 'desc'];
  93. return self::where($where)
  94. ->field('id,cid,title,titlepic,username,summary,hits,sort,status,create_time')
  95. ->order($order)->limit($limit)->select();
  96. }
  97. public static function getTop($limit)
  98. {
  99. return self::with('category')->field('id,cid,title,titlepic,summary,username,hits,sort,status,create_time')
  100. ->order('sort desc')->limit($limit)->select();
  101. }
  102. public static function getOne($id)
  103. {
  104. if (!$id) {
  105. throw new HttpException(404, '页面不存在');
  106. }
  107. return self::with('category')->find($id);
  108. }
  109. public static function getNextPrev($id, $cid = null)
  110. {
  111. if ($cid) {
  112. $id_list = self::where(['status' => 1, 'cid' => $cid])
  113. ->order(['sort' => 'desc', 'id' => 'desc'])
  114. ->column('id');
  115. } else {
  116. $id_list = self::where(['status' => 1])->column('id');
  117. }
  118. if (count($id_list)==1) {
  119. $data_p = null;
  120. $data_N = null;
  121. } else {
  122. $key = array_search($id, $id_list);
  123. if ($key == 0) {
  124. $data_p = null;
  125. $N = $id_list[1];
  126. $data_N = self::field('id,title,create_time')->find($N);
  127. } elseif ($key == count($id_list) - 1) {
  128. $P = $id_list[count($id_list) - 2];
  129. $data_p = self::field('id,title,create_time')->find($P);
  130. $data_N = null;
  131. } else {
  132. $P = $id_list[$key - 1];
  133. $data_p = self::field('id,title,create_time')->find($P);
  134. $N = $id_list[$key + 1];
  135. $data_N = self::field('id,title,create_time')->find($N);
  136. }
  137. }
  138. return ['prev' => $data_p, 'next' => $data_N];
  139. }
  140. public static function createTimeArchive()
  141. {
  142. $create_times = self::order('create_time desc')->column('create_time');
  143. $timeList = [];
  144. foreach ($create_times as $value) {
  145. $yearAndMonth = date("Y-m", $value);
  146. $timeList[] = $yearAndMonth;
  147. }
  148. $timeList = array_unique($timeList);
  149. return $timeList;
  150. }
  151. }