| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers;
- use app\models\BusinessCart;
- use app\modules\alliance\models\CartForm;
- use app\modules\alliance\models\CartListForm;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class CartController extends BaseController
- {
- /**
- * 购物车列表
- */
- public function actionList()
- {
- $form = new CartListForm();
- $form->attributes = \Yii::$app->request->get();
- $form->store_id = get_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_store_id();
- $form->saas_id = get_saas_user_id();
- if(isset($post['attr_list']) && !empty($post['attr_list'])){
- $attr_list = Json::decode($post['attr_list']);
- $form->goods_id = $post['goods_id'];
- $error = 0;
- foreach ($attr_list as $value){
- $form->attr = Json::encode($value['attr_list']);
- $form->num = $value['number'];
- $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' => '添加成功'
- ]);
- }
- }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 = BusinessCart::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 = get_params();
- //$form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->del());
- }
- /**
- * 清空
- */
- public function actionClear()
- {
- $form = new CartForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- return $this->asJson($form->clear());
- }
- /**
- * 购物车详情
- */
- public function actionCartEdit()
- {
- if (\Yii::$app->request->isPost) {
- $form = new CartListForm();
- $form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- $form->list = post_params('list');
- $form->mch_list = post_params('mch_list');
- return $this->asJson($form->save());
- }
- }
- /**
- * 购物车详情
- */
- public function actionUpdateNum()
- {
- $cart_id = get_params('cart_id', 0);
- $store_id = get_params('store_id', 0);
- $num = get_params('num', 1);
- $cart = BusinessCart::findOne(['id' => $cart_id, 'store_id' => $store_id, 'is_delete' => 0]);
- if (empty($cart)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- $cart->num = $num;
- if ($cart->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败'
- ]);
- }
- }
- /**
- * 商品列表减少购物车
- */
- public function actionCartReduce()
- {
- $goods_id = get_params('goods_id');
- $attr_info = Json::decode(get_params('arrt_info'));
- if($attr_info){
- $attr_str = '[';
- foreach ($attr_info as $k=>$v){
- $attr_str .= $k==0 ? $v['attr_id'] : ','.$v['attr_id'];
- }
- $attr_str .= ']';
- }
- $saas_id = get_saas_user_id();
- if(empty($goods_id)||empty($saas_id)){
- return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
- }
- $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]);
- if(empty($cart_info->id)){
- return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
- }
- if($cart_info->num ==1 || \Yii::$app->request->get('is_delete')){
- $cart_info->is_delete = 1;
- }
- $cart_info->num = $cart_info->num-1;
- if($cart_info->save()){
- $this->asJson(['code'=>0,'msg'=>'成功']);
- }else{
- $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
- }
- }
- /**
- * 批量添加购物车
- */
- public function actionBatchAddCart()
- {
- if (\Yii::$app->request->isPost) {
- $post = post_params();
- if (empty($post['goods_list'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '数据为空'
- ]);
- }
- $goods_list = Json::decode($post['goods_list']);
- $form = new CartForm();
- $form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- $error = 0;
- foreach ($goods_list as $goods) {
- $form->goods_id = $goods['goods_id'];
- $form->attr = Json::encode($goods['attr_list']);
- $form->num = $goods['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' => '添加成功'
- ]);
- }
- }
- }
- }
|