Files
xk-api-yii/admin/models/forms/drug/NavForm.php

97 lines
2.4 KiB
PHP

<?php
namespace admin\models\forms\drug;
use common\core\BaseModel;
use common\models\oldDb\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 ['删除成功'];
}
}