request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['goods_id', 'num'], 'required'], [['attr_id'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]); if (empty($goods)) return $this->error("商品不存在"); // 检查用户申请是否通过 if (PurchaseManager::check($this->user_id) == false) return $this->error("请申请资格"); // 没有规格id则查找默认规格【寻找价格最低的规格】 $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]; !empty($params['attr_id']) && $where['id'] = $params['attr_id']; $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one(); if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对"); if (empty($goods->is_attr)) { if (empty($goods->stock)) return $this->error('库存不足'); } else { //多规格 if (empty($goods_attr)) return $this->error("所选的规格不存在"); if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足"); } $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; PurchaseCart::addCart($params); return $this->success('加入购物车成功', []); } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * 删除购物车 * @return mixed|void */ public function actionDel() { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['list'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; PurchaseCart::delCart($params); return $this->success('操作成功', []); } /** * 购物车列表 * @return mixed|void */ public function actionIndex() { $list = PurchaseCart::getCartList($this->user_id); $data = []; if (!empty($list)) { $attr_id_arr = []; $num_arr = []; foreach ($list as $v) { $row = json_decode($v, true); if ($row) { $attr_id_arr[] = $row['attr_id']; $num_arr[$row['attr_id']] = $row['num']; } } $params['where'] = [['id' => $attr_id_arr]]; $params['with'] = ['goods' => function ($query) { $query->select('id,goods_name,is_on_sale,cover_pic,is_attr')->with(['extra']); }]; $params['select'] = 'id,sign_id,goods_id,pic_url,price,name'; $list = GoodsAttr::lists($params); $rows = []; if (!empty($list)) { foreach ($list as $v) { $rows[] = [ 'goods_id' => $v['goods_id'], 'num' => $num_arr[$v['id']], 'attr_id' => $v['id'], 'price' => $v['price'], 'cover_pic' => $v['goods']['cover_pic']??'', 'attr' => $v['name'], 'goods_name' => $v['goods']['goods_name'], 'is_on_sale' => $v['goods']['is_on_sale'], ]; } } $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name'); $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo'); $mch_arr = [ ['id' => 0, 'mch_pic' => $mall_logo, 'mch_name' => $mall_name, 'list' => $rows] ]; } return $this->success('操作成功', $mch_arr); } /** * @notes:修改购物车 * @return mixed|null * @author: lun * @Time: 2022/10/24 14:56 */ public function actionUpdateCart() { try { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['goods_id', 'num'], 'required'], [['attr_id'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]); if (empty($goods)) return $this->error("商品不存在"); // 没有规格id则查找默认规格【寻找价格最低的规格】 $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]; !empty($params['attr_id']) && $where['id'] = $params['attr_id']; $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one(); if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对"); if (empty($goods->is_attr)) { if (empty($goods->stock)) return $this->error('库存不足'); } else { //多规格 if (empty($goods_attr)) return $this->error("所选的规格不存在"); if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足"); } $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; PurchaseCart::updateCart($params); return $this->success('更新购物车成功', []); } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } }