| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\models\Cart;
- use app\models\Goods;
- use app\models\Option;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\AddCartForm;
- use app\modules\client\models\v1\CartDeleteForm;
- use app\modules\client\models\v1\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->user_id = get_user_id();
- $form->cart_id_list = json_decode(\Yii::$app->request->post('cart_id_list'),true);
- return $this->asJson($form->search(all_params()));
- }
- /**
- * 添加购物车
- */
- public function actionAddCart()
- {
- if (\Yii::$app->request->isPost) {
- $post = post_params();
- $form = new AddCartForm();
- $form->store_id = get_store_id();
- $form->user_id = get_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;
- $goods_info = Goods::findOne($post['goods_id']);
- //
- // //加价保护start
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- if (!empty($platform_profit_strategy) && $goods_info->cloud_goods_id > 0) {
- if (intval($platform_profit_strategy['is_risk_control'])) {
- //开启风控
- $id = \Yii::$app->cache->get('risk_control_model_id_' . $post['goods_id']);
- if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
- \Yii::$app->queue->remove($id);
- }
- $id =\queue_push(new \app\jobs\RiskControlModelJob(['goods_id' => $post['goods_id']]));
- \Yii::$app->cache->set('risk_control_model_id_' . $post['goods_id'], $id);
- }
- }
- 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(all_params()));
- }
- }
- }
- 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 = Cart::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 CartDeleteForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- return $this->asJson($form->save(all_params()));
- }
- /**
- * 购物车详情
- */
- public function actionCartEdit()
- {
- if (\Yii::$app->request->isPost) {
- $form = new CartListForm();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- $form->list = post_params('list');
- $form->mch_list = post_params('mch_list');
- return $this->asJson($form->save());
- }
- }
- /**
- * 购物车详情
- */
- public function actionUpdateNum()
- {
- $cart = Cart::findOne(['id' => get_params('cart_id', 0), 'store_id' => get_store_id(), 'is_delete' => 0]);
- if (empty($cart)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- // 兼容首页减到0 的时候直接删除
- $num = get_params('num');
- if($num == 0){
- $cart->is_delete = 1;
- }else{
- $cart->num = get_params('num', 1);
- }
- if ($cart->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败'
- ]);
- }
- }
- /**
- * 商品列表减少购物车
- */
- public function actionCartReduce()
- {
- $type = input_params('type', 0);
- $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 .= ']';
- }
- $user_id = get_user_id();
- if(empty($goods_id)||empty($user_id)){
- return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
- }
- $cart_info = $attr_str ? Cart::findOne(['type'=>$type,'goods_id'=>$goods_id,'user_id'=>$user_id,'is_delete'=>0,'attr'=>$attr_str]) : Cart::findOne(['type'=>$type,'goods_id'=>$goods_id,'user_id'=>$user_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 AddCartForm();
- $form->store_id = get_store_id();
- $form->user_id = get_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' => '添加成功'
- ]);
- }
- }
- }
- /**
- * 扫码加购物车
- */
- public function actionScanAddCart() {
- try {
- $type = input_params('type', 0);
- $scan_code = post_params('scan_code');
- $store_id = get_store_id();
- $user_id = get_user_id();
- $goods = Goods::find()->where([
- 'is_delete' => 0,
- 'product_type' => 0,
- 'store_id' => $store_id
- ])->andWhere(['LIKE', "JSON_UNQUOTE(JSON_EXTRACT(attr, '$[*].no'))", $scan_code])->select('id, attr')
- ->asArray()->one();
- if (!$goods) {
- throw new \Exception('商品不存在');
- }
- $current_attr = [];
- $attr = json_decode($goods['attr'], true);
- foreach ($attr as $attr_item) {
- if ($attr_item['no'] === $scan_code) {
- $current_attr = $attr_item;
- }
- }
- $attr_id = array_column($current_attr['attr_list'], 'attr_id');
- sort($attr_id);
- $attr = json_encode($attr_id, JSON_UNESCAPED_UNICODE);
- $cart = Cart::findOne([
- 'type'=>$type,
- 'store_id' => $store_id,
- 'goods_id' => $goods['id'],
- 'user_id' => $user_id,
- 'md_id' => get_md_id(),
- 'is_delete' => 0,
- 'attr' => $attr,
- ]);
- if (!$cart) {
- $cart = new Cart();
- $cart->store_id = $store_id;
- $cart->goods_id = $goods['id'];
- $cart->mch_id = 0;
- $cart->user_id = $user_id;
- $cart->num = 0;
- $cart->created_at = time();
- $cart->is_delete = 0;
- $cart->attr = $attr;
- if (get_md_id()) {
- $cart->md_id = get_md_id();
- }
- }
- $cart->num += 1;
- if (!$cart->save()) {
- throw new \Exception($cart->errors[0]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加购物车成功',
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- }
|