CartController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\models\BusinessCart;
  9. use app\modules\alliance\models\CartForm;
  10. use app\modules\alliance\models\CartListForm;
  11. use yii\base\BaseObject;
  12. use yii\helpers\Json;
  13. class CartController extends BaseController
  14. {
  15. /**
  16. * 购物车列表
  17. */
  18. public function actionList()
  19. {
  20. $form = new CartListForm();
  21. $form->attributes = \Yii::$app->request->get();
  22. $form->store_id = get_store_id();
  23. $form->saas_id = get_saas_user_id();
  24. return $this->asJson($form->search());
  25. }
  26. /**
  27. * 添加购物车
  28. */
  29. public function actionAddCart()
  30. {
  31. if (\Yii::$app->request->isPost) {
  32. $post = post_params();
  33. $form = new CartForm();
  34. $form->store_id = get_store_id();
  35. $form->saas_id = get_saas_user_id();
  36. if(isset($post['attr_list']) && !empty($post['attr_list'])){
  37. $attr_list = Json::decode($post['attr_list']);
  38. $form->goods_id = $post['goods_id'];
  39. $error = 0;
  40. foreach ($attr_list as $value){
  41. $form->attr = Json::encode($value['attr_list']);
  42. $form->num = $value['number'];
  43. $status = $form->save();
  44. if($status['code'] != 0){
  45. $error++;
  46. }
  47. }
  48. if($error > 0){
  49. return $this->asJson([
  50. 'code' => 0,
  51. 'msg' => '添加成功,' . $error . "个失败",
  52. ]);
  53. }else{
  54. return $this->asJson([
  55. 'code' => 0,
  56. 'msg' => '添加成功'
  57. ]);
  58. }
  59. }else{
  60. $form->attributes = post_params();
  61. return $this->asJson($form->save());
  62. }
  63. }
  64. }
  65. public function actionUpdateCart()
  66. {
  67. if (\Yii::$app->request->isPost) {
  68. $post = post_params();
  69. if (!isset($post['attr']) || empty($post['attr'])) {
  70. return $this->asJson([
  71. 'code' => 1,
  72. 'msg' => '规格数据不能为空'
  73. ]);
  74. }
  75. if (!isset($post['cart_id'])) {
  76. return $this->asJson([
  77. 'code' => 1,
  78. 'msg' => '缺少必要参数'
  79. ]);
  80. }
  81. if (!isset($post['num']) || $post['num'] <= 0) {
  82. return $this->asJson([
  83. 'code' => 1,
  84. 'msg' => '缺少必要参数'
  85. ]);
  86. }
  87. $id = $post['cart_id'];
  88. $cart = BusinessCart::findOne($id);
  89. if (!$cart) {
  90. return $this->asJson([
  91. 'code' => 1,
  92. 'msg' => '数据不存在'
  93. ]);
  94. }
  95. $params_attr = json_decode($post['attr'], true);
  96. $attr = [];
  97. foreach ($params_attr as $item) {
  98. if (!empty($item['attr_id'])) {
  99. $attr[] = $item['attr_id'];
  100. }
  101. }
  102. sort($attr);
  103. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  104. $cart->attr = $attr;
  105. $cart->num = (int)$post['num'];
  106. if ($cart->save()) {
  107. return $this->asJson([
  108. 'code' => 0,
  109. 'msg' => '规格修改成功'
  110. ]);
  111. }
  112. return $this->asJson([
  113. 'code' => 1,
  114. 'msg' => '规格修改失败'
  115. ]);
  116. }
  117. }
  118. /**
  119. * 删除
  120. */
  121. public function actionDelete()
  122. {
  123. $form = new CartForm();
  124. $form->attributes = get_params();
  125. //$form->store_id = get_store_id();
  126. $form->saas_id = get_saas_user_id();
  127. return $this->asJson($form->del());
  128. }
  129. /**
  130. * 清空
  131. */
  132. public function actionClear()
  133. {
  134. $form = new CartForm();
  135. $form->attributes = get_params();
  136. $form->store_id = get_store_id();
  137. $form->saas_id = get_saas_user_id();
  138. return $this->asJson($form->clear());
  139. }
  140. /**
  141. * 购物车详情
  142. */
  143. public function actionCartEdit()
  144. {
  145. if (\Yii::$app->request->isPost) {
  146. $form = new CartListForm();
  147. $form->store_id = get_store_id();
  148. $form->saas_id = get_saas_user_id();
  149. $form->list = post_params('list');
  150. $form->mch_list = post_params('mch_list');
  151. return $this->asJson($form->save());
  152. }
  153. }
  154. /**
  155. * 购物车详情
  156. */
  157. public function actionUpdateNum()
  158. {
  159. $cart_id = get_params('cart_id', 0);
  160. $store_id = get_params('store_id', 0);
  161. $num = get_params('num', 1);
  162. $cart = BusinessCart::findOne(['id' => $cart_id, 'store_id' => $store_id, 'is_delete' => 0]);
  163. if (empty($cart)) {
  164. return $this->asJson([
  165. 'code' => 1,
  166. 'msg' => '参数错误'
  167. ]);
  168. }
  169. $cart->num = $num;
  170. if ($cart->save()) {
  171. return $this->asJson([
  172. 'code' => 0,
  173. 'msg' => '保存成功'
  174. ]);
  175. } else {
  176. return $this->asJson([
  177. 'code' => 1,
  178. 'msg' => '保存失败'
  179. ]);
  180. }
  181. }
  182. /**
  183. * 商品列表减少购物车
  184. */
  185. public function actionCartReduce()
  186. {
  187. $goods_id = get_params('goods_id');
  188. $attr_info = Json::decode(get_params('arrt_info'));
  189. if($attr_info){
  190. $attr_str = '[';
  191. foreach ($attr_info as $k=>$v){
  192. $attr_str .= $k==0 ? $v['attr_id'] : ','.$v['attr_id'];
  193. }
  194. $attr_str .= ']';
  195. }
  196. $saas_id = get_saas_user_id();
  197. if(empty($goods_id)||empty($saas_id)){
  198. return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  199. }
  200. $cart_info = $attr_str ? BusinessCart::findOne(['goods_id'=>$goods_id,'saas_id'=>$saas_id,'is_delete'=>0,'attr'=>$attr_str]) : BusinessCart::findOne(['goods_id'=>$goods_id,'saas_id'=>$saas_id,'is_delete'=>0]);
  201. if(empty($cart_info->id)){
  202. return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  203. }
  204. if($cart_info->num ==1 || \Yii::$app->request->get('is_delete')){
  205. $cart_info->is_delete = 1;
  206. }
  207. $cart_info->num = $cart_info->num-1;
  208. if($cart_info->save()){
  209. $this->asJson(['code'=>0,'msg'=>'成功']);
  210. }else{
  211. $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  212. }
  213. }
  214. /**
  215. * 批量添加购物车
  216. */
  217. public function actionBatchAddCart()
  218. {
  219. if (\Yii::$app->request->isPost) {
  220. $post = post_params();
  221. if (empty($post['goods_list'])) {
  222. return $this->asJson([
  223. 'code' => 1,
  224. 'msg' => '数据为空'
  225. ]);
  226. }
  227. $goods_list = Json::decode($post['goods_list']);
  228. $form = new CartForm();
  229. $form->store_id = get_store_id();
  230. $form->saas_id = get_saas_user_id();
  231. $error = 0;
  232. foreach ($goods_list as $goods) {
  233. $form->goods_id = $goods['goods_id'];
  234. $form->attr = Json::encode($goods['attr_list']);
  235. $form->num = $goods['num'];
  236. $status = $form->save();
  237. if ($status['code'] != 0) {
  238. $error++;
  239. }
  240. }
  241. if ($error > 0) {
  242. return $this->asJson([
  243. 'code' => 0,
  244. 'msg' => '添加成功,' . $error . "个失败",
  245. ]);
  246. } else {
  247. return $this->asJson([
  248. 'code' => 0,
  249. 'msg' => '添加成功'
  250. ]);
  251. }
  252. }
  253. }
  254. }