| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\TopicFavorite;
- use yii\base\Model;
- class TopicFavoriteForm extends Model
- {
- public $store_id;
- public $user_id;
- public $topic_id;
- public $action;
- public function rules()
- {
- return [
- [['topic_id', 'action'], 'required'],
- [['action'], 'in', 'range' => [0, 1]],
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return $this->errorResponse;
- }
- if ($this->action == 0) {
- TopicFavorite::updateAll([
- 'is_delete' => 1
- ], [
- 'user_id' => $this->user_id,
- 'topic_id' => $this->topic_id,
- 'is_delete' => 0,
- 'store_id' => $this->store_id,
- ]);
- return [
- 'code' => 0,
- 'msg' => '已取消收藏',
- ];
- }
- $favorite = TopicFavorite::findOne([
- 'user_id' => $this->user_id,
- 'topic_id' => $this->topic_id,
- 'is_delete' => 0,
- 'store_id' => $this->store_id,
- ]);
- if ($favorite) {
- return [
- 'code' => 0,
- 'msg' => '收藏成功',
- ];
- }
- $favorite = new TopicFavorite();
- $favorite->attributes = $this->attributes;
- $favorite->addtime = time();
- if ($favorite->save()) {
- return [
- 'code' => 0,
- 'msg' => '收藏成功',
- ];
- }
- return $this->getErrorResponse($favorite);
- }
- }
|