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

316 lines
10 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 CommonPageActionForm extends Model
{
public $page_id;
public $alias;
public $cx_mch_id;
public $password;
public function rules()
{
return [
[['password', 'alias'], 'trim'],
[['password', 'alias'], 'string'],
[['cx_mch_id'], 'integer'],
[['page_id', ], 'safe'],
[['page_id', 'cx_mch_id'], 'required'],
];
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$t = \Yii::$app->db->beginTransaction();
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'status' => Page::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_delete = 1;
//删除RichText
RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id);
if(!$model->save()){
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
} else {
$t = \Yii::$app->db->beginTransaction();
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'status' => Page::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->is_delete = 1;
//删除RichText
RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id);
if(!$model->save()){
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
return $this->apiReturnSuccess("删除成功");
}
public function publish()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'status' => Page::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Page::STATUS_PUBLISHED;
if(empty($model->published_at)){
$model->published_at = time();
}
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'status' => Page::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->status = Page::STATUS_PUBLISHED;
if(empty($model->published_at)){
$model->published_at = time();
}
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("发布成功");
}
public function trash()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'status' => [Page::STATUS_DRAFT, Page::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Page::STATUS_TRASH;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'status' => [Page::STATUS_DRAFT, Page::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->status = Page::STATUS_TRASH;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("操作成功");
}
public function cancel_trash()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'status' => Page::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Page::STATUS_DRAFT;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'status' => Page::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->status = Page::STATUS_DRAFT;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("操作成功");
}
public function top()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'is_top' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_top = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'is_top' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->is_top = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function cancel_top()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->page_id)){
foreach ($this->page_id as $page_id){
$model = Page::findOne([
'id' => $page_id,
'is_delete' => 0,
'is_top' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_top = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Page::findOne([
'id' => $this->page_id,
'is_delete' => 0,
'is_top' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('页面不存在或已经删除');
}
$model->is_top = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function detail()
{
if(!$this->validate()){
return $this->getModelError();
}
$form = new CommonPageListForm();
$form->scenario = 'detail';
$form->page_id = $this->page_id;
$form->cx_mch_id = $this->cx_mch_id;
$form->status = Page::STATUS_PUBLISHED;
$res = $form->search();
if($res['code'] != 0)
return $res;
if(count($res['data']) == 0)
return $this->apiReturnError('页面详情不存在');
Page::setViewedNum($this->page_id, $this->cx_mch_id);
$data = $res['data'][0];
if($data['allow_read'] == 1){
return $this->apiReturnSuccess('ok',$data);
}
//校验密码
$page = Page::findOne(['id' => $this->page_id]);
if(empty($this->password) || !\Yii::$app->security->validatePassword($this->password, $page->password)){
return $this->apiReturnError('访问密码错误');
}
return $this->apiReturnSuccess('ok',$data);
}
}