cxfoot/models/common/cms/CommonPostEditForm.php
2023-10-27 14:25:12 +08:00

165 lines
5.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2021年7月29日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\cms;
use app\models\cms\Post;
use app\models\cms\PostMeta;
use app\models\cms\RichText;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
use app\models\Cat;
class CommonPostEditForm extends Model
{
public $scene;
public $cx_mch_id;
public $title;
public $content;
public $status;
public $sort;
public $is_top;
public $url_alias;
public $allow_read;
public $password;
public $cover_url;
public $author;
public $summary;
public $model;
public $cat_ids;
public function rules()
{
return [
[['title', 'content', 'url_alias', 'password', 'cover_url', 'author', 'summary'], 'trim'],
[['title', 'content', 'url_alias', 'password', 'cover_url', 'author', 'summary'], 'string'],
[['cx_mch_id', 'status', 'sort', 'is_top', 'allow_read', 'scene'], 'integer'],
[['is_top', 'allow_read'], 'in', 'range' => [0, 1]],
[['status'], 'in', 'range' => [0, 1, 2]],
[['model', 'cat_ids'], 'safe'],
[['sort'], 'default', 'value' => 100],
[['title', 'content', 'cx_mch_id', 'status', 'is_top', 'model', 'scene'], 'required'],
];
}
public function attributeLabels()
{
return [
'scene' => '场景',
'cover_url' => '封面',
'author' => '作者',
'summary' => '摘要',
'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;
$this->model->scene = $this->scene;
}
$res = RichText::saveRichText($this->model->rich_text_id, $this->title, $this->content, $this->cover_url, $this->author, $this->summary, $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 = Post::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 == Post::STATUS_PUBLISHED && empty($this->model->published_at)){
$this->model->published_at = time();
}
if(!$this->model->save()){
$t->rollBack();
return $this->getModelError($this->model);
}
//保存分类
$res = $this->saveCat($this->model->id, $this->cat_ids, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
return $res;
}
$t->commit();
return $this->apiReturnSuccess("保存成功");
}
private function saveCat($post_id, $cat_ids, $cx_mch_id = 0)
{
//删除所有分类
PostMeta::updateAll(['is_delete' => 1],[
'cx_mch_id' => $cx_mch_id,
'type' => PostMeta::TYPE_CAT,
'is_delete' => 0,
'post_id' => $post_id,
]);
foreach ($cat_ids as $cat_id){
//分类是否存在
$temp = Cat::findOne([
'cx_mch_id' => $cx_mch_id,
'type' => Cat::TYPE_POST_DEFAULT,
'is_delete' => 0,
'id' => $cat_id,
]);
if($temp == null){
return $this->apiReturnError("ID为{$cat_id}的分类不存在");
}
$model = PostMeta::findOne([
'cx_mch_id' => $cx_mch_id,
'type' => PostMeta::TYPE_CAT,
'post_id' => $post_id,
'value' => $cat_id
]);
if($model == null){
$model = new PostMeta();
$model->cx_mch_id = $cx_mch_id;
$model->type = PostMeta::TYPE_CAT;
$model->post_id = $post_id;
$model->value = $cat_id;
}
$model->is_delete = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess();
}
}