Article.php 5.6 KB

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