220 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			220 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\common\model\school\help;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\common\model\BaseModel;
							 | 
						||
| 
								 | 
							
								use think\Model;
							 | 
						||
| 
								 | 
							
								use traits\model\SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class Article extends BaseModel
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    use SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 表名
							 | 
						||
| 
								 | 
							
								    protected $name = 'school_help_article';
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    // 自动写入时间戳字段
							 | 
						||
| 
								 | 
							
								    protected $autoWriteTimestamp = 'integer';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 定义时间戳字段名
							 | 
						||
| 
								 | 
							
								    protected $createTime = false;
							 | 
						||
| 
								 | 
							
								    protected $updateTime = 'updatetime';
							 | 
						||
| 
								 | 
							
								    protected $deleteTime = 'deletetime';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 追加属性
							 | 
						||
| 
								 | 
							
								    protected $append = [
							 | 
						||
| 
								 | 
							
								        'status_text',
							 | 
						||
| 
								 | 
							
								        'hot_text',
							 | 
						||
| 
								 | 
							
								        'start_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 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 ? $value : (isset($data['status']) ? $data['status'] : '');
							 | 
						||
| 
								 | 
							
								        $list = $this->getStatusList();
							 | 
						||
| 
								 | 
							
								        return isset($list[$value]) ? $list[$value] : '';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getHotTextAttr($value, $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
							 | 
						||
| 
								 | 
							
								        $list = $this->getHotList();
							 | 
						||
| 
								 | 
							
								        return isset($list[$value]) ? $list[$value] : '';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getStartTimeTextAttr($value, $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
							 | 
						||
| 
								 | 
							
								        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    protected function setStartTimeAttr($value)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**得到基础条件
							 | 
						||
| 
								 | 
							
								     * @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',"help_cate_ids"]))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['not_status']) && $whereData['not_status']) $model = $model->where("{$alisa}status", 'not in', $whereData['not_status']);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}title|{$alisa}content", 'like', "%". $whereData['keywords']."%");
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['time'])&&$whereData['time']){
							 | 
						||
| 
								 | 
							
								            $model = $model->time(["{$alisa}start_time",$whereData['time']]);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (isset($whereData['help_cate_ids']) && $whereData['help_cate_ids']){
							 | 
						||
| 
								 | 
							
								            $help_cate_ids =  implode("|",explode(',',$whereData['help_cate_ids']));
							 | 
						||
| 
								 | 
							
								            $model = $model->whereRaw(" {$alisa}help_cate_ids REGEXP '({$help_cate_ids})'");
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $model;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static function articleList($page, $limit,$params=[]){
							 | 
						||
| 
								 | 
							
								        $with_field = [
							 | 
						||
| 
								 | 
							
								            'base'=>['*'],
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								        $alisa = (new self)->getWithAlisaName();
							 | 
						||
| 
								 | 
							
								        $sort = "{$alisa}.hot desc,{$alisa}.weigh desc,{$alisa}.id desc";
							 | 
						||
| 
								 | 
							
								        $serch_where = ["status"=>'1'];
							 | 
						||
| 
								 | 
							
								        $serch_where = array_merge($serch_where,$params);
							 | 
						||
| 
								 | 
							
								        return (new self)->getBaseList($serch_where, $page, $limit,$sort,$with_field);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** 文章详情
							 | 
						||
| 
								 | 
							
								     * @param $id
							 | 
						||
| 
								 | 
							
								     * @throws \think\exception\DbException
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function detail($id,$user_id=0,$oper_type='user',$trans=false){
							 | 
						||
| 
								 | 
							
								        $self = self::get($id);
							 | 
						||
| 
								 | 
							
								        if(!$self) throw new \Exception("未找到相关数据");
							 | 
						||
| 
								 | 
							
								        if($self["status"] != 1) throw new \Exception("未找到相关数据");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if($user_id){
							 | 
						||
| 
								 | 
							
								//            if($self["user_id"] != $user_id) throw new \Exception("非法访问");
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//        $self->getRelation('user')->visible(['nickname','realname','mobile','avatar']);
							 | 
						||
| 
								 | 
							
								//        $self->getRelation('shop')->visible(['name','logo']);
							 | 
						||
| 
								 | 
							
								//        $self->getRelation('lib')->visible(['title','headimage']);
							 | 
						||
| 
								 | 
							
								//        $self->getRelation('classesorder')->visible(['order_no']);
							 | 
						||
| 
								 | 
							
								//        $self->getRelation('teacher')->visible(['name',"head_image"]);
							 | 
						||
| 
								 | 
							
								        //增加浏览量
							 | 
						||
| 
								 | 
							
								        $this->setViews($id,$user_id,$oper_type,$user_id,$trans);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $self;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**设置浏览量
							 | 
						||
| 
								 | 
							
								     * @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();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            //调用事件
							 | 
						||
| 
								 | 
							
								            $data = ['help_article' => $classes_lib,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id];
							 | 
						||
| 
								 | 
							
								            \think\Hook::listen('help_article_view_after', $data);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::commitTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }catch (\Exception $e){
							 | 
						||
| 
								 | 
							
								            if($trans){
							 | 
						||
| 
								 | 
							
								                self::rollbackTrans();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            throw new \Exception($e->getMessage());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $classes_lib;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |