cxgj/models/common/cms/CommonPostActionForm.php
2023-11-27 09:45:13 +08:00

333 lines
11 KiB
PHP

<?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\RichText;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonPostActionForm extends Model
{
public $post_id;
public $alias;
public $cx_mch_id;
public $password;
public $scene;
public function rules()
{
return [
[['password', 'alias'], 'trim'],
[['password', 'alias'], 'string'],
[['cx_mch_id', 'scene'], 'integer'],
[['post_id', ], 'safe'],
[['post_id', 'cx_mch_id', 'scene'], 'required'],
];
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->post_id)){
foreach ($this->post_id as $post_id){
$t = \Yii::$app->db->beginTransaction();
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_id,
'is_delete' => 0,
'status' => Post::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 = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_id,
'is_delete' => 0,
'status' => Post::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->post_id)){
foreach ($this->post_id as $post_id){
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_id,
'is_delete' => 0,
'status' => Post::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Post::STATUS_PUBLISHED;
if(empty($model->published_at)){
$model->published_at = time();
}
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_id,
'is_delete' => 0,
'status' => Post::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('文章不存在或已经删除');
}
$model->status = Post::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->post_id)){
foreach ($this->post_id as $post_id){
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_id,
'is_delete' => 0,
'status' => [Post::STATUS_DRAFT, Post::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Post::STATUS_TRASH;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_id,
'is_delete' => 0,
'status' => [Post::STATUS_DRAFT, Post::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('文章不存在或已经删除');
}
$model->status = Post::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->post_id)){
foreach ($this->post_id as $post_id){
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_id,
'is_delete' => 0,
'status' => Post::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Post::STATUS_DRAFT;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_id,
'is_delete' => 0,
'status' => Post::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('文章不存在或已经删除');
}
$model->status = Post::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->post_id)){
foreach ($this->post_id as $post_id){
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_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 = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_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->post_id)){
foreach ($this->post_id as $post_id){
$model = Post::findOne([
'scene' => $this->scene,
'id' => $post_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 = Post::findOne([
'scene' => $this->scene,
'id' => $this->post_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 CommonPostListForm();
$form->scenario = 'detail';
$form->scene = $this->scene;
$form->post_id = $this->post_id;
$form->cx_mch_id = $this->cx_mch_id;
$form->status = Post::STATUS_PUBLISHED;
$res = $form->search();
if($res['code'] != 0)
return $res;
if(count($res['data']) == 0)
return $this->apiReturnError('文章详情不存在');
Post::setViewedNum($this->post_id, $this->cx_mch_id);
$data = $res['data'][0];
if($data['allow_read'] == 1){
return $this->apiReturnSuccess('ok',$data);
}
//校验密码
$post = Post::findOne(['id' => $this->post_id]);
if(empty($this->password) || !\Yii::$app->security->validatePassword($this->password, $post->password)){
return $this->apiReturnError('访问密码错误');
}
return $this->apiReturnSuccess('ok',$data);
}
}