Article.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <<<<<<< HEAD
  2. <?php
  3. declare(strict_types=1);
  4. namespace app\common\model;
  5. use think\exception\HttpException;
  6. use app\common\model\Base;
  7. use think\facade\Config;
  8. class Article extends Base
  9. {
  10. protected $schema = [
  11. "id" => "int",
  12. "cid" => "int",
  13. "title" => "varchar",
  14. "writer" => "varchar",
  15. "source" => "varchar",
  16. "titlepic" => "varchar",
  17. "keywords" => "varchar",
  18. "summary" => "varchar",
  19. "content" => "varchar",
  20. "discussed" => "int",
  21. "status" => "int",
  22. "top" => "int",
  23. "sort" => "int",
  24. "hits" => "int",
  25. "likes" => "int",
  26. "content_type"=> "int",
  27. "userid" => "int",
  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. }
  137. =======
  138. <?php
  139. declare(strict_types=1);
  140. namespace app\common\model;
  141. use think\exception\HttpException;
  142. use app\common\model\Base;
  143. use think\facade\Config;
  144. class Article extends Base
  145. {
  146. protected $schema = [
  147. "id" => "int",
  148. "cid" => "int",
  149. "title" => "varchar",
  150. "writer" => "varchar",
  151. "source" => "varchar",
  152. "titlepic" => "varchar",
  153. "keywords" => "varchar",
  154. "summary" => "varchar",
  155. "content" => "varchar",
  156. "discussed" => "int",
  157. "status" => "int",
  158. "top" => "int",
  159. "sort" => "int",
  160. "hits" => "int",
  161. "likes" => "int",
  162. "content_type"=> "int",
  163. "userid" => "int",
  164. "username" => "varchar",
  165. "create_time" => "int",
  166. "update_time" => "int"
  167. ];
  168. protected $disuse = ['top','discussed'];
  169. public function category()
  170. {
  171. return $this->belongsTo('Category', 'cid')->bind(['category_name' => 'name', 'category_url' => 'url', 'route' => 'route']);
  172. }
  173. public static function queryPage($params)
  174. {
  175. $where = [];
  176. if (isset($params['status'])) {
  177. $where[] = ['status', '=', (int) $params['status']];
  178. }
  179. if (!empty($params['cid'])) {
  180. $where[] = ['cid', '=', (int) $params['cid']];
  181. }
  182. if (!empty($params['key'])) {
  183. $where[] = ['title|keywords', 'LIKE', '%' . (string)$params['key'] . '%'];
  184. }
  185. if (!empty($params['yearMonth'])) {
  186. if (!preg_match("/^([0-9]{4})\/([0-9]{1,2})$/", $params['yearMonth']) && !preg_match("/^([0-9]{4})-([0-9]{1,2})$/", $params['yearMonth'])) {
  187. throw new HttpException(404, '日期格式不正确');
  188. }
  189. $separator = strrpos('/', $params['yearMonth']) ? '/' : '-';
  190. $year = $month = '';
  191. if (strnatcasecmp(PHP_VERSION, '7.0.0') >= 0) {
  192. list($year, $month) = explode($separator, $params['yearMonth']);
  193. } else {
  194. list($month, $year) = explode($separator, $params['yearMonth']);
  195. }
  196. if ((int) $month < 1 || (int) $month > 12) {
  197. throw new HttpException(404, '日期格式不正确');
  198. }
  199. $days = month_frist_and_last_day($year, $month);
  200. // $where['create_time'] = ['between', [$days['firstday'], $days['lastday']]];
  201. $where[] = ['create_time','between', [$days['firstday'], $days['lastday']]];
  202. }
  203. $limit = empty($params['limit']) ? Config::get('app.page_size', 20) : (int)$params['limit'];
  204. $order = ['id desc'];
  205. if (!empty($params['order'])) {
  206. if (is_array($params['order'])) {
  207. $order = array_unique(array_merge($params['order']));
  208. } else {
  209. array_push($order, $params['order']);
  210. $order = array_unique($order);
  211. }
  212. }
  213. $list = self::where($where)
  214. ->field('id,cid,title,titlepic,username,summary,content_type,hits,sort,status,create_time')
  215. ->with(['category'])
  216. ->order($order)->paginate(['list_rows'=>$limit, 'query' => $params]);
  217. return $list;
  218. }
  219. public static function getListByCid($cid = 0, $limit = 10, $order = "")
  220. {
  221. $where = [];
  222. if ($cid != 0) {
  223. if ($cid < 0) {
  224. $where[] = ['cid', '<>', abs($cid)];
  225. } else {
  226. $where[] = ['cid', '=', $cid];
  227. }
  228. }
  229. $where[] = ['status', '=', 1];
  230. $order = $order ?? "sort ASC,id DESC";
  231. return self::with('category')->where($where)
  232. ->field('id,cid,title,titlepic,username,summary,hits,sort,status,create_time')
  233. ->order($order)->limit($limit)->select();
  234. }
  235. public static function getTop($limit)
  236. {
  237. return self::with('category')->field('id,cid,title,titlepic,summary,username,hits,sort,status,create_time')
  238. ->order('sort ASC, id DESC')->limit($limit)->select();
  239. }
  240. public static function getOne($id)
  241. {
  242. if (!$id) {
  243. throw new HttpException(404, '页面不存在');
  244. }
  245. return self::with('category')->find($id);
  246. }
  247. public static function getNextPrev($id, $cid = null)
  248. {
  249. $whereP = [];
  250. $whereN = [];
  251. $whereP[] = ['status', '=', 1];
  252. $whereN[] = ['status', '=', 1];
  253. if ($cid) {
  254. $whereP[] = ['cid', '=', $cid];
  255. $whereN[] = ['cid', '=', $cid];
  256. }
  257. $whereP[] = ['id', ">", $id];
  258. $whereN[] = ['id', "<", $id];
  259. $data_P = self::where($whereP)->order("id desc")->limit(1)->find();
  260. $data_N = self::where($whereN)->order("id desc")->limit(1)->find();
  261. return ['prev' => $data_P, 'next' => $data_N];
  262. }
  263. public static function createTimeArchive($limit = 0)
  264. {
  265. if ($limit == 0) {
  266. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->select();
  267. } else {
  268. $timeList = self::distinct(true)->fieldRaw("FROM_UNIXTIME(`create_time`, '%Y-%m') as pubmonth")->order('pubmonth desc')->limit($limit)->select();
  269. }
  270. return $timeList;
  271. }
  272. public static function createOne($data)
  273. {
  274. $article = new static;
  275. $article->allowField(['cid','title','writer','source','titlepic','keywords','summary','content',
  276. 'status','sort','hits','content_type','userid','username'])->save($data);
  277. FileManager::saveInfoid($article->id, $data['cjid']);
  278. $keys = explode(',', $data['keywords']);
  279. Tag::saveTags($keys, (int) $article->id);
  280. }
  281. public static function updateOne($data)
  282. {
  283. $article = self::find($data['id']);
  284. $oldkeys = explode(',', $article->keywords);
  285. $article->allowField(['cid','title','writer','source','titlepic','keywords','summary','content',
  286. 'status','sort','hits','content_type','userid','username'])->save($data);
  287. FileManager::saveInfoid($article->id, $data['cjid']);
  288. $newkeys = explode(',', $data['keywords']);
  289. $keys = array_diff($newkeys, $oldkeys);
  290. $dec = array_diff($oldkeys, $newkeys);
  291. Tag::saveTags($keys, (int) $article->id);
  292. Tag::decNums($dec, (int) $article->id);
  293. }
  294. }
  295. >>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86