123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- declare(strict_types=1);
- namespace app\common\model;
- use think\exception\HttpException;
- use app\common\model\Base;
- class Article extends Base
- {
- protected $schema = [
- "id" => "int",
- "cid" => "int",
- "title" => "varchar",
- "writer" => "varchar",
- "source" => "varchar",
- "titlepic" => "varchar",
- "keywords" => "varchar",
- "summary" => "varchar",
- "content" => "varchar",
- "discussed" => "int",
- "status" => "int",
- "top" => "int",
- "sorts" => "int",
- "hits" => "int",
- "likes" => "int",
- "content_type"=> "int",
- "userid" => "int",
- "username" => "varchar",
- "create_time" => "int",
- "update_time" => "int"
- ];
- protected $disuse = ['top','discussed'];
- public function category()
- {
- return $this->belongsTo('Category', 'cid')->bind(['category_name' => 'name', 'category_url' => 'url']);
- }
- public static function queryPage($params)
- {
- $where = [];
- if (isset($params['status'])) {
- $where[] = ['status', '=', (int) $params['status']];
- }
- if (!empty($params['cid'])) {
- $where[] = ['cid', '=', (int) $params['cid']];
- }
- if (!empty($params['key'])) {
- $where[] = ['title|keywords', 'LIKE', '%' . (string)$params['key'] . '%'];
- }
- if (!empty($params['yearMonth'])) {
- if (!preg_match("/^([0-9]{4})\/([0-9]{1,2})$/", $params['yearMonth']) && !preg_match("/^([0-9]{4})-([0-9]{1,2})$/", $params['yearMonth'])) {
- throw new HttpException(404, '日期格式不正确');
- }
- $separator = strrpos('/', $params['yearMonth']) ? '/' : '-';
- $year = $month = '';
- if (strnatcasecmp(PHP_VERSION, '7.0.0') >= 0) {
- list($year, $month) = explode($separator, $params['yearMonth']);
- } else {
- list($month, $year) = explode($separator, $params['yearMonth']);
- }
- if ((int) $month < 1 || (int) $month > 12) {
- throw new HttpException(404, '日期格式不正确');
- }
- $days = month_frist_and_last_day($year, $month);
- $where['create_time'] = ['between', [$days['firstday'], $days['lastday']]];
- }
- $limit = empty($params['limit']) ? 20 : (int)$params['limit'];
- $list = self::where($where)
- ->field('id,cid,title,titlepic,username,summary,content_type,hits,sorts,status,create_time')
- ->with(['category'])
- ->order('id desc')->paginate($limit, false, ['query' => $params]);
- return $list;
- }
- public static function getListByCid($cid = 0, $limit = 10, $order = [])
- {
- $where = [];
- if ($cid != 0) {
- if ($cid < 0) {
- $where[] = ['cid', '<>', abs($cid)];
- } else {
- $where[] = ['cid', '=', $cid];
- }
- }
- $order = !empty($order) ? array_merge($order, ['id' => 'desc']) : ['id' => 'desc'];
- return self::where($where)
- ->field('id,cid,title,titlepic,username,summary,hits,sorts,status,create_time')
- ->order($order)->limit($limit)->select();
- }
- public static function getTop($limit)
- {
- return self::with('category')->field('id,cid,title,titlepic,summary,username,hits,sorts,status,create_time')
- ->order('sorts desc, hits desc')->limit($limit)->select();
- }
- public static function getOne($id)
- {
- if (!$id) {
- throw new HttpException(404, '页面不存在');
- }
- return self::with('category')->find($id);
- }
- public static function getNextPrev($id, $cid = null)
- {
- if ($cid) {
- $id_list = self::where(['status' => 1, 'cid' => $cid])
- ->order(['sorts' => 'desc', 'id' => 'desc'])
- ->column('id');
- } else {
- $id_list = self::where(['status' => 1])->column('id');
- }
- if (count($id_list)==1) {
- $data_p = null;
- $data_N = null;
- } else {
- $key = array_search($id, $id_list);
- if ($key == 0) {
- $data_p = null;
- $N = $id_list[1];
- $data_N = self::field('id,title,create_time')->find($N);
- } elseif ($key == count($id_list) - 1) {
- $P = $id_list[count($id_list) - 2];
- $data_p = self::field('id,title,create_time')->find($P);
- $data_N = null;
- } else {
- $P = $id_list[$key - 1];
- $data_p = self::field('id,title,create_time')->find($P);
- $N = $id_list[$key + 1];
- $data_N = self::field('id,title,create_time')->find($N);
- }
- }
- return ['prev' => $data_p, 'next' => $data_N];
- }
- public function createTimeArchive()
- {
- $create_times = $this->order('create_time desc')->column('create_time');
- $timeList = [];
- foreach ($create_times as $value) {
- $yearAndMonth = date("Y-m", $value);
- $timeList[] = $yearAndMonth;
- }
- $timeList = array_unique($timeList);
- return $timeList;
- }
- public static function sort($data)
- {
- $article = self::find($data['id']);
- $article->sort = $data['sort'];
- return $article->save();
- }
- }
|