初始化仓库
This commit is contained in:
97
admin/models/forms/drug/NavForm.php
Normal file
97
admin/models/forms/drug/NavForm.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace admin\models\forms\drug;
|
||||
|
||||
use common\core\BaseModel;
|
||||
use common\models\Nav;
|
||||
use yii\db\Exception;
|
||||
|
||||
class NavForm extends BaseModel
|
||||
{
|
||||
public $id;
|
||||
public $store_id;
|
||||
public $sort;
|
||||
public $link_type;
|
||||
|
||||
public $pic;
|
||||
public $external_link;
|
||||
public $type;
|
||||
public $is_home;
|
||||
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['pic','required'],
|
||||
['external_link','string'],
|
||||
[['id','is_home','store_id','type','sort','link_type'],'integer'],
|
||||
['external_link', 'url', 'defaultScheme' => '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 ['删除成功'];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user