TopicFavoriteForm.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\TopicFavorite;
  9. use yii\base\Model;
  10. class TopicFavoriteForm extends Model
  11. {
  12. public $store_id;
  13. public $user_id;
  14. public $topic_id;
  15. public $action;
  16. public function rules()
  17. {
  18. return [
  19. [['topic_id', 'action'], 'required'],
  20. [['action'], 'in', 'range' => [0, 1]],
  21. ];
  22. }
  23. public function save()
  24. {
  25. if (!$this->validate()) {
  26. return $this->errorResponse;
  27. }
  28. if ($this->action == 0) {
  29. TopicFavorite::updateAll([
  30. 'is_delete' => 1
  31. ], [
  32. 'user_id' => $this->user_id,
  33. 'topic_id' => $this->topic_id,
  34. 'is_delete' => 0,
  35. 'store_id' => $this->store_id,
  36. ]);
  37. return [
  38. 'code' => 0,
  39. 'msg' => '已取消收藏',
  40. ];
  41. }
  42. $favorite = TopicFavorite::findOne([
  43. 'user_id' => $this->user_id,
  44. 'topic_id' => $this->topic_id,
  45. 'is_delete' => 0,
  46. 'store_id' => $this->store_id,
  47. ]);
  48. if ($favorite) {
  49. return [
  50. 'code' => 0,
  51. 'msg' => '收藏成功',
  52. ];
  53. }
  54. $favorite = new TopicFavorite();
  55. $favorite->attributes = $this->attributes;
  56. $favorite->addtime = time();
  57. if ($favorite->save()) {
  58. return [
  59. 'code' => 0,
  60. 'msg' => '收藏成功',
  61. ];
  62. }
  63. return $this->getErrorResponse($favorite);
  64. }
  65. }