2025-05-20 16:33:23 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\model\home;
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
use app\common\model\Attachment;
|
2025-05-20 16:33:23 +08:00
|
|
|
use app\common\model\BaseModel;
|
|
|
|
use think\Model;
|
|
|
|
use traits\model\SoftDelete;
|
|
|
|
|
|
|
|
class News extends BaseModel
|
|
|
|
{
|
|
|
|
|
|
|
|
use SoftDelete;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
|
|
protected $name = 'news';
|
|
|
|
|
|
|
|
// 自动写入时间戳字段
|
|
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
|
|
|
|
// 定义时间戳字段名
|
|
|
|
protected $createTime = 'createtime';
|
|
|
|
protected $updateTime = 'updatetime';
|
|
|
|
protected $deleteTime = 'deletetime';
|
|
|
|
|
|
|
|
// 追加属性
|
|
|
|
protected $append = [
|
|
|
|
'recommend_text',
|
|
|
|
'author',
|
|
|
|
'release_time_text'
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getAuthorAttr($value, $data)
|
|
|
|
{
|
|
|
|
|
|
|
|
return config("site.news_author");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static function init()
|
|
|
|
{
|
|
|
|
self::afterInsert(function ($row) {
|
|
|
|
if (!$row['weigh']) {
|
|
|
|
$pk = $row->getPk();
|
|
|
|
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getRecommendList()
|
|
|
|
{
|
|
|
|
return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getRecommendTextAttr($value, $data)
|
|
|
|
{
|
|
|
|
$value = $value ?: ($data['recommend'] ?? '');
|
|
|
|
$list = $this->getRecommendList();
|
|
|
|
return $list[$value] ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getReleaseTimeTextAttr($value, $data)
|
|
|
|
{
|
|
|
|
$value = $value ?: ($data['release_time'] ?? '');
|
|
|
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setReleaseTimeAttr($value)
|
|
|
|
{
|
|
|
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImageAttr($value, $data)
|
|
|
|
{
|
|
|
|
if (!empty($value)) return cdnurl($value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function cate()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(NewsCate::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
public function attachment()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Attachment::class, 'file', 'url', [], 'LEFT')->setEagerlyType(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**得到基础条件
|
|
|
|
* @param $status
|
|
|
|
* @param null $model
|
|
|
|
* @param string $alisa
|
|
|
|
*/
|
|
|
|
public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$model) {
|
|
|
|
$model = new static;
|
|
|
|
if ($alisa&&!$with) $model = $model->alias($alisa);
|
|
|
|
}
|
|
|
|
if ($alisa) $alisa = $alisa . '.';
|
|
|
|
$tableFields = (new static)->getTableFields();
|
|
|
|
foreach ($tableFields as $fields)
|
|
|
|
{
|
2025-05-21 18:06:43 +08:00
|
|
|
if(in_array($fields, ['cate_id',"recommend","hot","fine"]))continue;
|
2025-05-20 16:33:23 +08:00
|
|
|
// if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
|
|
|
|
|
|
|
|
if (isset($whereData[$fields]) && $whereData[$fields]){
|
|
|
|
if(is_array($whereData[$fields])){
|
|
|
|
$model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
|
|
|
|
}else{
|
|
|
|
$model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
if (isset($whereData['hot']) && $whereData['hot']!=="" && $whereData['hot']!==null){
|
|
|
|
$model = $model->where("{$alisa}hot", 'in', $whereData['hot'] );
|
|
|
|
}
|
2025-05-20 16:33:23 +08:00
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
if (isset($whereData['fine']) && $whereData['fine']!=="" && $whereData['fine']!==null){
|
|
|
|
$model = $model->where("{$alisa}fine", 'in', $whereData['fine'] );
|
2025-05-20 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
if (isset($whereData['recommend']) && $whereData['recommend']!=="" && $whereData['recommend']!==null){
|
|
|
|
$model = $model->where("{$alisa}recommend", '=', $whereData['recommend'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
|
|
|
|
if (isset($whereData['keywords'])&&$whereData['keywords']){
|
|
|
|
$model = $model->where("{$alisa}title|{$alisa}subtitle", 'LIKE', "%{$whereData['keywords']}%" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
if (isset($whereData['cate_id'])&&$whereData['cate_id']){
|
|
|
|
$model = $model->where("{$alisa}cate_id", 'in', $whereData['cate_id'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($whereData['time'])&&$whereData['time']){
|
|
|
|
|
|
|
|
$model = $model->time(["{$alisa}release_time",$whereData['time']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (isset($whereData['has_evaluate'])&&$whereData['has_evaluate']){
|
|
|
|
// //1查已评价 2查未评价
|
|
|
|
// if($whereData['has_evaluate'] == 1){
|
|
|
|
// //1查已评价
|
|
|
|
// $model = $model->where("{$alisa}classes_evaluate_id", '<>', 0);
|
|
|
|
// }else{
|
|
|
|
// //2查未评价
|
|
|
|
// $model = $model->whereExists(function ($query) use ($alisa) {
|
|
|
|
// $order_table_name = (new \app\common\model\school\classes\hourorder\Order())->getQuery()->getTable();
|
|
|
|
// $query->table($order_table_name)->where($order_table_name . '.classes_order_id=' . $alisa . 'id')->where('status', '=', '3');
|
|
|
|
// })->where("{$alisa}classes_evaluate_id", '=', 0);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function allList($page, $limit,$keywords,$recommend="",$cate_id=[],$params=[]){
|
|
|
|
$with_field = [
|
|
|
|
'cate'=>['image','name'],
|
2025-05-21 18:06:43 +08:00
|
|
|
'attachment'=>['*'],
|
2025-05-20 16:33:23 +08:00
|
|
|
'base'=>['*'],
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
$alisa = (new self)->getWithAlisaName();
|
2025-05-21 18:06:43 +08:00
|
|
|
$sort = " {$alisa}.recommend desc,{$alisa}.weigh desc,{$alisa}.id desc";
|
2025-05-20 16:33:23 +08:00
|
|
|
$serch_where = ['recommend'=>$recommend,'cate_id'=>$cate_id,'keywords'=>$keywords];
|
|
|
|
|
|
|
|
return (new self)->getBaseList(array_merge($serch_where,$params), $page, $limit,$sort,$with_field);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 通用详情(后台api版本)
|
|
|
|
* @param $params
|
|
|
|
* @param $trans
|
|
|
|
* @return $this
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-05-21 18:06:43 +08:00
|
|
|
public function detail($id,$show_field=[],$except_field=[],$user_id=0,$oper_type='user',$trans=false){
|
2025-05-20 16:33:23 +08:00
|
|
|
$row = $this->get($id);
|
|
|
|
if (!$row) {
|
|
|
|
throw new \Exception(__('No Results were found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
//上一篇,下一篇
|
|
|
|
$prev = $this->where('id', '<', $row['id'])->order('id', 'desc')->find();
|
|
|
|
$next = $this->where('id', '>', $row['id'])->order('id', 'asc')->find();
|
|
|
|
|
|
|
|
if($show_field){
|
|
|
|
$row->visible($show_field);
|
|
|
|
}
|
|
|
|
if($except_field){
|
|
|
|
$row->hidden($except_field);
|
|
|
|
}
|
2025-05-21 18:06:43 +08:00
|
|
|
$row->attachment;
|
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
$row = $row->toArray();
|
|
|
|
$row['prev'] = $prev;
|
|
|
|
$row['next'] = $next;
|
2025-05-21 18:06:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
$this->setViews($id,$user_id,$oper_type,$user_id,$trans);
|
|
|
|
|
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2025-05-21 18:06:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**设置浏览量
|
|
|
|
* @param $id
|
|
|
|
* @param int $user_id
|
|
|
|
* @param bool $check
|
|
|
|
* @param bool $trans
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function setViews($id,$user_id,$oper_type='user',$oper_id=0,$trans=false){
|
|
|
|
$classes_lib = self::where("id",$id)->find();
|
|
|
|
if(!$classes_lib)throw new \Exception("找不到活动!");
|
|
|
|
//判断逻辑
|
|
|
|
if($trans){
|
|
|
|
self::beginTrans();
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
//事务逻辑
|
|
|
|
$classes_lib->views = $classes_lib->views + 1;
|
|
|
|
//查询是否已收藏
|
|
|
|
$classes_lib->save();
|
|
|
|
|
|
|
|
//调用事件
|
|
|
|
if($trans){
|
|
|
|
self::commitTrans();
|
|
|
|
}
|
|
|
|
}catch (\Exception $e){
|
|
|
|
if($trans){
|
|
|
|
self::rollbackTrans();
|
|
|
|
}
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
return $classes_lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-20 16:33:23 +08:00
|
|
|
}
|