117 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace app\admin\model\home;
 | 
						|
 | 
						|
use think\Model;
 | 
						|
use traits\model\SoftDelete;
 | 
						|
 | 
						|
class News extends Model
 | 
						|
{
 | 
						|
 | 
						|
    use SoftDelete;
 | 
						|
 | 
						|
    
 | 
						|
 | 
						|
    // 表名
 | 
						|
    protected $name = 'news';
 | 
						|
    
 | 
						|
    // 自动写入时间戳字段
 | 
						|
    protected $autoWriteTimestamp = 'integer';
 | 
						|
 | 
						|
    // 定义时间戳字段名
 | 
						|
    protected $createTime = 'createtime';
 | 
						|
    protected $updateTime = 'updatetime';
 | 
						|
    protected $deleteTime = 'deletetime';
 | 
						|
 | 
						|
    // 追加属性
 | 
						|
    protected $append = [
 | 
						|
        'type_text',
 | 
						|
        'recommend_text',
 | 
						|
        'hot_text',
 | 
						|
        'fine_text',
 | 
						|
        'release_time_text'
 | 
						|
    ];
 | 
						|
    
 | 
						|
 | 
						|
    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 getTypeList()
 | 
						|
    {
 | 
						|
        return ['1' => __('Type 1'), '2' => __('Type 2')];
 | 
						|
    }
 | 
						|
 | 
						|
    public function getRecommendList()
 | 
						|
    {
 | 
						|
        return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
 | 
						|
    }
 | 
						|
 | 
						|
    public function getHotList()
 | 
						|
    {
 | 
						|
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
 | 
						|
    }
 | 
						|
 | 
						|
    public function getFineList()
 | 
						|
    {
 | 
						|
        return ['0' => __('Fine 0'), '1' => __('Fine 1')];
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getTypeTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ?: ($data['type'] ?? '');
 | 
						|
        $list = $this->getTypeList();
 | 
						|
        return $list[$value] ?? '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getRecommendTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ?: ($data['recommend'] ?? '');
 | 
						|
        $list = $this->getRecommendList();
 | 
						|
        return $list[$value] ?? '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getHotTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ?: ($data['hot'] ?? '');
 | 
						|
        $list = $this->getHotList();
 | 
						|
        return $list[$value] ?? '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getFineTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ?: ($data['fine'] ?? '');
 | 
						|
        $list = $this->getFineList();
 | 
						|
        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 cate()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('app\admin\model\news\Cate', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
 | 
						|
    }
 | 
						|
}
 |