CartForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\BusinessCart;
  9. use app\models\Goods;
  10. use app\models\Store;
  11. use yii\base\Model;
  12. class CartForm extends Model
  13. {
  14. public $saas_id;
  15. public $store_id;
  16. public $goods_id;
  17. public $attr;
  18. public $num;
  19. public $cart_id_list;
  20. public function rules()
  21. {
  22. return [
  23. //[['goods_id', 'attr', 'num','cart_id_list'], 'required'],
  24. [['goods_id', 'num','saas_id'], 'integer'],
  25. [['num'], 'integer', 'min' => 1],
  26. [['cart_id_list','attr'],'string']
  27. ];
  28. }
  29. public function save()
  30. {
  31. try {
  32. if (!$this->validate()) {
  33. return [
  34. 'code' => 1,
  35. 'msg' => $this->getErrorSummary(false)[0]
  36. ];
  37. }
  38. $goods = Goods::findOne([
  39. 'id' => $this->goods_id,
  40. //'store_id' => $this->store_id,
  41. 'is_delete' => 0,
  42. 'status' => 1,
  43. ]);
  44. if (!$goods) {
  45. return [
  46. 'code' => 1,
  47. 'msg' => '商品不存在或已下架',
  48. ];
  49. }
  50. $store = \app\models\Store::findOne(['id' => $goods->store_id, 'is_delete' => 0, 'business_model' => 2]);
  51. if (!$store) {
  52. return [
  53. 'code' => 1,
  54. 'msg' => '店铺不存在或非平台运营',
  55. ];
  56. }
  57. $this->attr = json_decode($this->attr, true);
  58. $attr = [];
  59. foreach ($this->attr as $item) {
  60. if (!empty($item['attr_id'])) {
  61. $attr[] = intval($item['attr_id']);
  62. }
  63. }
  64. sort($attr);
  65. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  66. $cart = BusinessCart::findOne([
  67. 'store_id' => $goods->store_id,
  68. 'goods_id' => $this->goods_id,
  69. 'saas_id' => $this->saas_id,
  70. //'md_id' => get_md_id(),
  71. 'is_delete' => 0,
  72. 'attr' => $attr,
  73. ]);
  74. if (!$cart) {
  75. $cart = new BusinessCart();
  76. $cart->store_id = $goods->store_id;
  77. $cart->goods_id = $this->goods_id;
  78. $cart->saas_id = $this->saas_id;
  79. $cart->num = 0;
  80. $cart->created_at = time();
  81. $cart->is_delete = 0;
  82. $cart->attr = $attr;
  83. // if (get_md_id()) {
  84. // $cart->md_id = get_md_id();
  85. // }
  86. }
  87. $cart->num += $this->num;
  88. if ($cart->save()) {
  89. return [
  90. 'code' => 0,
  91. 'msg' => '添加购物车成功',
  92. ];
  93. } else {
  94. return ['code' => 1, 'msg' => $cart->errors[0]];
  95. }
  96. } catch (\Exception $e) {
  97. return [
  98. 'code' => 0,
  99. 'msg' => $e->getMessage() . 'line=' . $e->getLine()
  100. ];
  101. }
  102. }
  103. public function del()
  104. {
  105. if (!$this->validate()) {
  106. return $this->errorResponse;
  107. }
  108. try {
  109. $this->cart_id_list = json_decode($this->cart_id_list, true);
  110. foreach ($this->cart_id_list as $cart_id) {
  111. $cart = BusinessCart::findOne([
  112. //'store_id' => $this->store_id,
  113. 'is_delete' => 0,
  114. 'saas_id' => $this->saas_id,
  115. 'id' => $cart_id,
  116. ]);
  117. if (!$cart) {
  118. continue;
  119. }
  120. $cart->is_delete = 1;
  121. $cart->save();
  122. }
  123. return [
  124. 'code' => 0,
  125. 'msg' => '删除完成',
  126. ];
  127. } catch (\Exception $e) {
  128. return [
  129. 'code' => 1,
  130. 'msg' => $e->getMessage() . $e->getLine(),
  131. ];
  132. }
  133. }
  134. public function clear()
  135. {
  136. try {
  137. $store_id = $this->store_id;
  138. $saas_id = $this->saas_id;
  139. $cart = BusinessCart::findOne([
  140. 'is_delete' => 0,
  141. 'saas_id' => $saas_id,
  142. 'store_id' => $store_id,
  143. ]);
  144. if (!$cart) {
  145. throw new \Exception('购物车列表为空');
  146. }
  147. BusinessCart::updateAll(['is_delete' => 1], [
  148. 'is_delete' => 0,
  149. 'saas_id' => $saas_id,
  150. 'store_id' => $store_id,
  151. ]);
  152. return [
  153. 'code' => 0,
  154. 'msg' => '清空完成',
  155. ];
  156. } catch (\Exception $e) {
  157. return [
  158. 'code' => 1,
  159. 'msg' => $e->getMessage() . $e->getLine(),
  160. ];
  161. }
  162. }
  163. }