'http'], ['external_link', 'IsGet'], //定义验证方法 ]; } public function IsGet($attribute){ $temp = get_headers($this->external_link,true); if(!preg_match('/200/',$temp[0])){ $this->addError($attribute,'该链接无效, 无法访问'); }else{ return true; } } public function save() { if (!$this->validate()){ throw new Exception($this->getErrorMsg()); } $nav=new Nav(); $nav->pic=$this->pic; $nav->external_link=$this->external_link; $nav->type=$this->type; $nav->store_id=$this->store_id; $nav->sort=$this->sort; $nav->link_type=$this->link_type; if ($this->is_home==1){ $nav->is_home=$this->is_home; $nav->home_time=date('Y-m-d H:i:s',time()); } $nav->saveOrFail(); } public function update() { $nav=Nav::find()->where([ 'id'=>$this->id, 'is_delete'=>0 ])->one(); if (!$nav) throw new Exception('轮播图不存在'); $nav->pic=$this->pic?? $nav->pic; $nav->external_link=$this->external_link?? $nav->external_link; $nav->type=$this->type?? $nav->type; $nav->store_id=$this->store_id?? $nav->store_id; $nav->sort=$this->sort?? $nav->sort; $nav->link_type=$this->link_type?? $nav->link_type; if ($this->is_home==1) { $nav->home_time=date('Y-m-d H:i:s',time()); } $nav->saveOrFail(); return ['编辑成功']; } public function del() { $nav=Nav::find()->where([ 'id'=>$this->id, 'is_delete'=>0 ])->one(); if (!$nav)throw new Exception('轮播图不存在'); $nav->is_delete=1; $nav->saveOrFail(); return ['删除成功']; } }