FavoriteAddForm.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-05-12 10:51:33
  10. * @LastEditTime: 2021-05-19 14:51:01
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\FavoriteAddForm.php
  14. */
  15. namespace app\modules\client\models\v1;
  16. use app\models\Favorite;
  17. use yii\base\Model;
  18. class FavoriteAddForm extends Model
  19. {
  20. public $store_id;
  21. public $user_id;
  22. public $id;
  23. public $type;
  24. public $worker_id;
  25. public function rules()
  26. {
  27. return [
  28. [['type', 'worker_id', 'id'], 'default', 'value' => 0],
  29. ['type', 'in', 'range' => [Favorite::FAVORITE_GOODS, Favorite::FAVORITE_SHOP, Favorite::FAVORITE_WORKER]],
  30. // [['id'], 'required',],
  31. ];
  32. }
  33. public function save()
  34. {
  35. if (!$this->validate()) {
  36. return [
  37. 'code' => 1,
  38. 'msg' => $this->getErrorSummary(false)[0]
  39. ];
  40. }
  41. if (intval($this->type) === Favorite::FAVORITE_WORKER) {
  42. $existFavorite = Favorite::findOne([
  43. 'store_id' => $this->store_id,
  44. 'user_id' => $this->user_id,
  45. 'type' => $this->type,
  46. 'worker_id' => $this->worker_id
  47. ]);
  48. } else {
  49. $existFavorite = Favorite::findOne([
  50. 'store_id' => $this->store_id,
  51. 'user_id' => $this->user_id,
  52. 'goods_id' => $this->id,
  53. 'type' => $this->type,
  54. 'worker_id' => $this->worker_id
  55. ]);
  56. }
  57. if ($existFavorite) {
  58. $existFavorite->is_delete = $existFavorite->is_delete ? 0 : 1;
  59. $existFavorite->save();
  60. return [
  61. 'code' => 0,
  62. 'msg' => 'success',
  63. ];
  64. }
  65. $favorite = new Favorite();
  66. $favorite->store_id = $this->store_id;
  67. $favorite->user_id = $this->user_id;
  68. $favorite->goods_id = $this->id;
  69. $favorite->type = $this->type;
  70. $favorite->created_at = time();
  71. $favorite->worker_id = $this->worker_id;
  72. if ($favorite->save()) {
  73. return [
  74. 'code' => 0,
  75. 'msg' => 'success',
  76. ];
  77. } else {
  78. return [
  79. 'code' => 1,
  80. 'msg' => 'fail',
  81. ];
  82. }
  83. }
  84. }