| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers\purchase;
- use app\modules\alliance\controllers\BaseController;
- use app\models\PurchaseCart;
- use app\modules\alliance\models\purchase\CartForm;
- use app\modules\alliance\models\purchase\CartListForm;
- class CartController extends BaseController
- {
- /**
- * 购物车列表
- */
- public function actionList()
- {
- $form = new CartForm();
- $form->attributes = get_params();
- $form->store_id = get_saas_purchase_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->search());
- }
- /**
- * 添加购物车
- */
- public function actionAddCart()
- {
- if (\Yii::$app->request->isPost) {
- $post = post_params();
- $form = new CartForm();
- $form->store_id = get_saas_purchase_store_id();
- $form->saas_id = get_saas_user_id();
- if(isset($post['goods_list']) && !empty($post['goods_list'])){
- $goods_list = $post['goods_list'];
- $error = 0;
- foreach ($goods_list as $gv){
- $attr_list = $gv['attr_list'];
- $form->goods_id = $gv['goods_id'];
- foreach ($attr_list as $value){
- $form->attr = $value['attr'];
- $form->num = $value['num'];
- $status = $form->save();
- if($status['code'] != 0){
- $error++;
- }
- }
- }
- if($error > 0){
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功,' . $error . "个失败",
- ]);
- }else{
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功'
- ]);
- }
- }
- if(isset($post['attr_list']) && !empty($post['attr_list'])){
- $attr_list = $post['attr_list'];
- $form->goods_id = $post['goods_id'];
- $error = 0;
- foreach ($attr_list as $value){
- $form->attr = $value['attr'];
- $form->num = $value['num'];
- $status = $form->save();
- if($status['code'] != 0){
- debug_log($status['msg']);
- debug_log($error);
- $error++;
- }
- }
- if($error > 0){
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功,' . $error . "个失败",
- ]);
- }else{
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功'
- ]);
- }
- }else{
- $form->attributes = post_params();
- return $this->asJson($form->save());
- }
- }
- }
- public function actionUpdateCart()
- {
- if (\Yii::$app->request->isPost) {
- $post = post_params();
- if (!isset($post['attr']) || empty($post['attr'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '规格数据不能为空'
- ]);
- }
- if (!isset($post['cart_id'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺少必要参数'
- ]);
- }
- if (!isset($post['num']) || $post['num'] <= 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺少必要参数'
- ]);
- }
- $id = $post['cart_id'];
- $cart = PurchaseCart::findOne($id);
- if (!$cart) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '数据不存在'
- ]);
- }
- $params_attr = json_decode($post['attr'], true);
- $attr = [];
- foreach ($params_attr as $item) {
- if (!empty($item['attr_id'])) {
- $attr[] = $item['attr_id'];
- }
- }
- sort($attr);
- $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
- $cart->attr = $attr;
- $cart->num = (int)$post['num'];
- if ($cart->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '规格修改成功'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '规格修改失败'
- ]);
- }
- }
- /**
- * 删除
- */
- public function actionDelete()
- {
- $form = new CartForm();
- $form->attributes = post_params();
- //$form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->del());
- }
- }
|