CartController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers\purchase;
  8. use app\modules\alliance\controllers\BaseController;
  9. use app\models\PurchaseCart;
  10. use app\modules\alliance\models\purchase\CartForm;
  11. use app\modules\alliance\models\purchase\CartListForm;
  12. class CartController extends BaseController
  13. {
  14. /**
  15. * 购物车列表
  16. */
  17. public function actionList()
  18. {
  19. $form = new CartForm();
  20. $form->attributes = get_params();
  21. $form->store_id = get_saas_purchase_store_id();
  22. $form->saas_id = get_saas_user_id();
  23. return $this->asJson($form->search());
  24. }
  25. /**
  26. * 添加购物车
  27. */
  28. public function actionAddCart()
  29. {
  30. if (\Yii::$app->request->isPost) {
  31. $post = post_params();
  32. $form = new CartForm();
  33. $form->store_id = get_saas_purchase_store_id();
  34. $form->saas_id = get_saas_user_id();
  35. if(isset($post['goods_list']) && !empty($post['goods_list'])){
  36. $goods_list = $post['goods_list'];
  37. $error = 0;
  38. foreach ($goods_list as $gv){
  39. $attr_list = $gv['attr_list'];
  40. $form->goods_id = $gv['goods_id'];
  41. foreach ($attr_list as $value){
  42. $form->attr = $value['attr'];
  43. $form->num = $value['num'];
  44. $status = $form->save();
  45. if($status['code'] != 0){
  46. $error++;
  47. }
  48. }
  49. }
  50. if($error > 0){
  51. return $this->asJson([
  52. 'code' => 0,
  53. 'msg' => '添加成功,' . $error . "个失败",
  54. ]);
  55. }else{
  56. return $this->asJson([
  57. 'code' => 0,
  58. 'msg' => '添加成功'
  59. ]);
  60. }
  61. }
  62. if(isset($post['attr_list']) && !empty($post['attr_list'])){
  63. $attr_list = $post['attr_list'];
  64. $form->goods_id = $post['goods_id'];
  65. $error = 0;
  66. foreach ($attr_list as $value){
  67. $form->attr = $value['attr'];
  68. $form->num = $value['num'];
  69. $status = $form->save();
  70. if($status['code'] != 0){
  71. debug_log($status['msg']);
  72. debug_log($error);
  73. $error++;
  74. }
  75. }
  76. if($error > 0){
  77. return $this->asJson([
  78. 'code' => 0,
  79. 'msg' => '添加成功,' . $error . "个失败",
  80. ]);
  81. }else{
  82. return $this->asJson([
  83. 'code' => 0,
  84. 'msg' => '添加成功'
  85. ]);
  86. }
  87. }else{
  88. $form->attributes = post_params();
  89. return $this->asJson($form->save());
  90. }
  91. }
  92. }
  93. public function actionUpdateCart()
  94. {
  95. if (\Yii::$app->request->isPost) {
  96. $post = post_params();
  97. if (!isset($post['attr']) || empty($post['attr'])) {
  98. return $this->asJson([
  99. 'code' => 1,
  100. 'msg' => '规格数据不能为空'
  101. ]);
  102. }
  103. if (!isset($post['cart_id'])) {
  104. return $this->asJson([
  105. 'code' => 1,
  106. 'msg' => '缺少必要参数'
  107. ]);
  108. }
  109. if (!isset($post['num']) || $post['num'] <= 0) {
  110. return $this->asJson([
  111. 'code' => 1,
  112. 'msg' => '缺少必要参数'
  113. ]);
  114. }
  115. $id = $post['cart_id'];
  116. $cart = PurchaseCart::findOne($id);
  117. if (!$cart) {
  118. return $this->asJson([
  119. 'code' => 1,
  120. 'msg' => '数据不存在'
  121. ]);
  122. }
  123. $params_attr = json_decode($post['attr'], true);
  124. $attr = [];
  125. foreach ($params_attr as $item) {
  126. if (!empty($item['attr_id'])) {
  127. $attr[] = $item['attr_id'];
  128. }
  129. }
  130. sort($attr);
  131. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  132. $cart->attr = $attr;
  133. $cart->num = (int)$post['num'];
  134. if ($cart->save()) {
  135. return $this->asJson([
  136. 'code' => 0,
  137. 'msg' => '规格修改成功'
  138. ]);
  139. }
  140. return $this->asJson([
  141. 'code' => 1,
  142. 'msg' => '规格修改失败'
  143. ]);
  144. }
  145. }
  146. /**
  147. * 删除
  148. */
  149. public function actionDelete()
  150. {
  151. $form = new CartForm();
  152. $form->attributes = post_params();
  153. //$form->store_id = get_store_id();
  154. $form->saas_id = get_saas_user_id();
  155. return $this->asJson($form->del());
  156. }
  157. }