294 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			294 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\adminapi\model\home;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\common\model\BaseModel;
							 | 
						||
| 
								 | 
							
								use think\Model;
							 | 
						||
| 
								 | 
							
								use traits\model\SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class NewsCate extends BaseModel
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    use SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 表名
							 | 
						||
| 
								 | 
							
								    protected $name = 'news_cate';
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    // 自动写入时间戳字段
							 | 
						||
| 
								 | 
							
								    protected $autoWriteTimestamp = 'integer';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 定义时间戳字段名
							 | 
						||
| 
								 | 
							
								    protected $createTime = 'createtime';
							 | 
						||
| 
								 | 
							
								    protected $updateTime = 'updatetime';
							 | 
						||
| 
								 | 
							
								    protected $deleteTime = 'deletetime';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 追加属性
							 | 
						||
| 
								 | 
							
								    protected $append = [
							 | 
						||
| 
								 | 
							
								        'status_text',
							 | 
						||
| 
								 | 
							
								        'hot_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 getStatusList()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return ['1' => __('Status 1'), '2' => __('Status 2')];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getHotList()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getStatusTextAttr($value, $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $value = $value ?: ($data['status'] ?? '');
							 | 
						||
| 
								 | 
							
								        $list = $this->getStatusList();
							 | 
						||
| 
								 | 
							
								        return $list[$value] ?? '';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getHotTextAttr($value, $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $value = $value ?: ($data['hot'] ?? '');
							 | 
						||
| 
								 | 
							
								        $list = $this->getHotList();
							 | 
						||
| 
								 | 
							
								        return $list[$value] ?? '';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getImageAttr($value, $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!empty($value)) return cdnurl($value, true);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**得到基础条件
							 | 
						||
| 
								 | 
							
								     * @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)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if(in_array($fields, ['status',"hot"]))continue;
							 | 
						||
| 
								 | 
							
								//            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]);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['status']) && $whereData['status']) $model = $model->where("{$alisa}status", 'in', $whereData['status']);
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}id|{$alisa}name", 'Like', "%{$whereData['keywords']}%");
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['time'])&&$whereData['time']){
							 | 
						||
| 
								 | 
							
								            $model = $model->time($whereData['time']);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['hot']) && $whereData['hot']!=="" && $whereData['hot']!==null) $model = $model->where("{$alisa}hot", 'in', $whereData['hot']);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $model;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static function allList($page, $limit,$params=[]){
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $sort = "weigh desc,id desc";
							 | 
						||
| 
								 | 
							
								//        if(!empty($params['status'])){
							 | 
						||
| 
								 | 
							
								//            $params['status'] = '1';
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								        return (new self)->getBaseList($params, $page, $limit,$sort);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** 通用详情(后台api版本)
							 | 
						||
| 
								 | 
							
								     * @param $params
							 | 
						||
| 
								 | 
							
								     * @param $trans
							 | 
						||
| 
								 | 
							
								     * @return $this
							 | 
						||
| 
								 | 
							
								     * @throws \Exception
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function detail($id,$show_field=[],$except_field=[]){
							 | 
						||
| 
								 | 
							
								        $row = $this->get($id);
							 | 
						||
| 
								 | 
							
								        if (!$row) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('No Results were found'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if($show_field){
							 | 
						||
| 
								 | 
							
								            $row->visible($show_field);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if($except_field){
							 | 
						||
| 
								 | 
							
								            $row->hidden($except_field);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $row;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								     /** 通用删除(后台api版本)
							 | 
						||
| 
								 | 
							
								     * @param $params
							 | 
						||
| 
								 | 
							
								     * @param $trans
							 | 
						||
| 
								 | 
							
								     * @return $this
							 | 
						||
| 
								 | 
							
								     * @throws \Exception
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function del($ids = null,$trans=false){
							 | 
						||
| 
								 | 
							
								        if (empty($ids)) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('Parameter %s can not be empty', 'ids'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								//判断逻辑
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $pk = $this->getPk();
							 | 
						||
| 
								 | 
							
								        $adminIds = $this->getDataLimitAdminIds();
							 | 
						||
| 
								 | 
							
								        if (is_array($adminIds)) {
							 | 
						||
| 
								 | 
							
								            $this->where($this->dataLimitField, 'in', $adminIds);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $list = $this->where($pk, 'in', $ids)->select();
							 | 
						||
| 
								 | 
							
								        $count = 0;
							 | 
						||
| 
								 | 
							
								        if($trans){
							 | 
						||
| 
								 | 
							
								            self::beginTrans();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $res = true;
							 | 
						||
| 
								 | 
							
								        try{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            foreach ($list as $item) {
							 | 
						||
| 
								 | 
							
								                $count += $item->delete();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::commitTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }catch (\Exception $e){
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::rollbackTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $count;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /** 通用新增(后台api版本)
							 | 
						||
| 
								 | 
							
								     * @param $params
							 | 
						||
| 
								 | 
							
								     * @param $trans
							 | 
						||
| 
								 | 
							
								     * @return $this
							 | 
						||
| 
								 | 
							
								     * @throws \Exception
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function add($params,$trans=false){
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (empty($params)) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('Parameter %s can not be empty', ''));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
							 | 
						||
| 
								 | 
							
								            $params[$this->dataLimitField] = $this->auth->id;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								//判断逻辑
							 | 
						||
| 
								 | 
							
								        if($trans){
							 | 
						||
| 
								 | 
							
								            self::beginTrans();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $res = true;
							 | 
						||
| 
								 | 
							
								        try{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            //是否采用模型验证
							 | 
						||
| 
								 | 
							
								            if ($this->modelValidate) {
							 | 
						||
| 
								 | 
							
								                $name = str_replace("\\model\\", "\\validate\\", get_class($this));
							 | 
						||
| 
								 | 
							
								                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
							 | 
						||
| 
								 | 
							
								                $this->validateFailException()->validate($validate);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            $result = $this->allowField(true)->save($params);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::commitTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }catch (\Exception $e){
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::rollbackTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** 通用编辑(后台api版本)
							 | 
						||
| 
								 | 
							
								     * @param $params
							 | 
						||
| 
								 | 
							
								     * @param $trans
							 | 
						||
| 
								 | 
							
								     * @return $this
							 | 
						||
| 
								 | 
							
								     * @throws \Exception
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function edit($id,$params,$trans=false){
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $row = $this->get($id);
							 | 
						||
| 
								 | 
							
								        if (!$row) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('No Results were found'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $adminIds = $this->getDataLimitAdminIds();
							 | 
						||
| 
								 | 
							
								        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('You have no permission'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (empty($params)) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception(__('Parameter %s can not be empty', ''));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								//判断逻辑
							 | 
						||
| 
								 | 
							
								        if($trans){
							 | 
						||
| 
								 | 
							
								            self::beginTrans();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $res = true;
							 | 
						||
| 
								 | 
							
								        try{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            //是否采用模型验证
							 | 
						||
| 
								 | 
							
								            if ($this->modelValidate) {
							 | 
						||
| 
								 | 
							
								                $name = str_replace("\\model\\", "\\validate\\", get_class($this));
							 | 
						||
| 
								 | 
							
								                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
							 | 
						||
| 
								 | 
							
								                $row->validateFailException()->validate($validate);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            $result = $row->allowField(true)->save($params);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::commitTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }catch (\Exception $e){
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::rollbackTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $row;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |