110 lines
2.8 KiB
PHP
110 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use think\Model;
|
|
|
|
class News extends Base
|
|
{
|
|
// 设置当前模型对应的完整数据表名称
|
|
protected $table = 'news';
|
|
|
|
// 设置字段自动写入时间戳
|
|
protected $autoWriteTimestamp = true;
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'create_time';
|
|
protected $updateTime = 'update_time';
|
|
|
|
/**
|
|
* 获取列表(分页)
|
|
*/
|
|
public static function getList($where, $field = '*', $order = 'publish_time desc', $pageSize = "15")
|
|
{
|
|
$list = self::where($where)
|
|
->field($field)
|
|
->order($order)
|
|
->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
|
|
$page = $list->render();
|
|
$data['list'] = $list->toArray()['data'];
|
|
$data['count'] = $list->total();
|
|
$data['last_page'] = $list->toArray()['last_page'];
|
|
// $data['page'] = $page;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取全部列表(不分页)
|
|
*/
|
|
public static function getAllList($where = [], $field = '*', $order = 'publish_time desc', $limit = '0')
|
|
{
|
|
$data = self::where($where)
|
|
->field($field)
|
|
->order($order)
|
|
->limit($limit)
|
|
->select();
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取单条数据
|
|
*/
|
|
public static function getInfo($where = [], $field = '*')
|
|
{
|
|
$data = self::where($where)
|
|
->field($field)
|
|
->find();
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取热榜资讯
|
|
*/
|
|
public static function getHotNews($limit = 10, $field = '*')
|
|
{
|
|
return self::where('status', 1)
|
|
->where('is_hot', 1)
|
|
->field($field)
|
|
->order('publish_time desc')
|
|
->limit($limit)
|
|
->select();
|
|
}
|
|
|
|
/**
|
|
* 获取精选资讯
|
|
*/
|
|
public static function getFeaturedNews($limit = 10, $field = '*')
|
|
{
|
|
return self::where('status', 1)
|
|
->where('is_featured', 1)
|
|
->field($field)
|
|
->order('publish_time desc')
|
|
->limit($limit)
|
|
->select();
|
|
}
|
|
|
|
/**
|
|
* 根据作者ID获取资讯
|
|
*/
|
|
public static function getNewsByAuthor($authorId, $limit = 10, $field = '*')
|
|
{
|
|
return self::where('status', 1)
|
|
->where('author_id', $authorId)
|
|
->field($field)
|
|
->order('publish_time desc')
|
|
->limit($limit)
|
|
->select();
|
|
}
|
|
|
|
/**
|
|
* 搜索资讯
|
|
*/
|
|
public static function searchNews($keyword, $field = '*', $pageSize = 15)
|
|
{
|
|
return self::where('status', 1)
|
|
->where('title', 'like', '%' . $keyword . '%')
|
|
->field($field)
|
|
->order('publish_time desc')
|
|
->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
|
|
}
|
|
} |