106 lines
3.3 KiB
PHP
106 lines
3.3 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021年7月28日
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\models\common\cms;
|
||
|
||
use app\models\cms\Page;
|
||
use app\models\cms\RichText;
|
||
use app\models\Model;
|
||
use yii\data\Pagination;
|
||
use app\components\SiteHelper;
|
||
|
||
|
||
class CommonPageEditForm extends Model
|
||
{
|
||
public $cx_mch_id;
|
||
public $title;
|
||
public $content;
|
||
public $status;
|
||
public $sort;
|
||
public $is_top;
|
||
public $url_alias;
|
||
public $allow_read;
|
||
public $password;
|
||
|
||
|
||
public $model;
|
||
|
||
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['title', 'content', 'url_alias', 'password'], 'trim'],
|
||
[['title', 'content', 'url_alias', 'password'], 'string'],
|
||
[['cx_mch_id', 'status', 'sort', 'is_top', 'allow_read'], 'integer'],
|
||
[['is_top', 'allow_read'], 'in', 'range' => [0, 1]],
|
||
[['status'], 'in', 'range' => [0, 1, 2]],
|
||
[['model',], 'safe'],
|
||
[['sort'], 'default', 'value' => 100],
|
||
[['title', 'content', 'cx_mch_id', 'status', 'is_top', 'model',], 'required'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'cx_mch_id' => '平台商户ID',
|
||
'title' => '标题',
|
||
'content' => '内容',
|
||
'status' => '状态:0=草稿,1=已发布,2=回收站',
|
||
'sort' => '排序:默认100,升序',
|
||
'is_top' => '是否置顶:0=否,1=是',
|
||
];
|
||
}
|
||
|
||
public function save()
|
||
{
|
||
if(!$this->validate()){
|
||
return $this->getModelError();
|
||
}
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
if($this->model->isNewRecord){
|
||
$this->model->cx_mch_id = $this->cx_mch_id;
|
||
$this->model->is_delete = 0;
|
||
$this->model->rich_text_id = 0;
|
||
}
|
||
$res = RichText::saveRichText($this->model->rich_text_id, $this->title, $this->content, null, null, null, $this->cx_mch_id);
|
||
if($res['code'] != 0)
|
||
return $res;
|
||
$this->model->rich_text_id = $res['data']['rich_text_id'];
|
||
$this->model->status = $this->status;
|
||
$this->model->sort = $this->sort;
|
||
$this->model->is_top = $this->is_top;
|
||
$this->model->allow_read = $this->allow_read;
|
||
//别名是否已经存在
|
||
$temp = Page::findOne(['url_alias' => $this->url_alias, 'is_delete' => 0]);
|
||
if($temp != null && !$this->model->isNewRecord && $this->model->id != $temp->id){
|
||
return $this->apiReturnError("别名已被其他页面占用,请更换其他别名");
|
||
}
|
||
$this->model->url_alias = $this->url_alias;
|
||
if($this->allow_read == 0 && !empty($this->password)){
|
||
$this->model->password = \Yii::$app->security->generatePasswordHash($this->password);
|
||
}
|
||
if($this->status == Page::STATUS_PUBLISHED && empty($this->model->published_at)){
|
||
$this->model->published_at = time();
|
||
}
|
||
if(!$this->model->save()){
|
||
$t->rollBack();
|
||
return $this->getModelError($this->model);
|
||
}
|
||
$t->commit();
|
||
return $this->apiReturnSuccess("保存成功");
|
||
}
|
||
}
|
||
|
||
|
||
|