request->getBodyParam('goodsId');/*int 商品id*/ $count = Yii::$app->request->getBodyParam('count');/*int 数量*/ $skuId = Yii::$app->request->getBodyParam('skuId');/*int 商品sku id*/ //购物车提交订单需传参数 $cartIds = Yii::$app->request->getBodyParam('cartIds');/*array 购物车商品id*/ if ((empty($cartIds) && empty($goodsId)) || ($goodsId && empty($count))) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } $tra = Yii::$app->db->beginTransaction(); try { $order = new Order(); if (!$order->save()) { throw new ServerErrorHttpException('服务器创建订单失败'); } if ($goodsId && $count) { $this->addGoods($order->id, $goodsId, $skuId, $count);/*添加订单商品*/ } else { $this->addGoodsByCart($cartIds, $order->id);/*通过购物车添加订单商品*/ } $this->saveGoodsInfo($order);/*保存订单商品信息*/ $tra->commit(); Helper::createdResponse($order, $this->viewAction); return $order; } catch (Exception $e) { $tra->rollBack(); throw $e; } } /** * @return bool * @throws Exception * @throws Throwable * @throws yii\base\InvalidConfigException * 根据操作类型更新订单 */ public function update() { $data = Yii::$app->request->getBodyParams(); $type = Yii::$app->request->getBodyParam('type'); $order = $this->findOrder(); $tra = Yii::$app->db->beginTransaction(); try { switch ($type) { case self::TYPE_CONFIRM : $this->confirm($order, $data); break; case self::TYPE_CANCEL: $this->cancel($order); break; case self::TYPE_TAKE: $this->take($order); break; case self::TYPE_CHANGE_SHIPPING_TYPE: $this->switchShippingType($order, $data); break; case self::TYPE_CHANGE_ADDRESS: $this->changeAddress($order, $data); break; case self::TYPE_CHANGE_TAKING_SITE: $this->changeTakingSite($order, $data); break; } if (!$order->save()) { throw new ServerErrorHttpException('服务器更新订单失败'); } $tra->commit(); return true; } catch (Exception $e) { $tra->rollBack(); throw $e; } } /*----------------------------------------- 华丽的分割线 -----------------------------------------*/ /** * @param Order $order * @param array $data * @throws BadRequestHttpException * @throws NotFoundHttpException * 添加地址到订单 */ private function changeAddress($order, $data) { if (!isset($data['address_id'])) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { throw new BadRequestHttpException('配送方式异常'); } $address = Address::findOne(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]); if (!$address) { throw new NotFoundHttpException('收货地址未找到'); } $order->consignee = $address->consignee; $order->phone = $address->phone; $order->city = $address->city; $order->area = $address->area; $order->province = $address->province; $order->address = $address->address; } /** * @param Order $order * @param array $data * @throws BadRequestHttpException * @throws NotFoundHttpException * 修改自提点 */ private function changeTakingSite($order, $data) { if (!isset($data['taking_site_id']) || empty($data['taking_site_id'])) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->shipping_type !== Order::SHIPPING_TYPE_PICKED_UP) { throw new BadRequestHttpException('配送方式异常'); } $site = TakingSite::findOne($data['taking_site_id']); if (!$site) { throw new NotFoundHttpException('自提点未找到'); } $order->taking_site = $data['taking_site_id']; } /** * @param Order $order * @param array $data * @throws BadRequestHttpException * 切换配送方式 */ private function switchShippingType($order, $data) { if (!isset($data['shipping_type'])) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->status !== Order::STATUS_UNCONFIRMED) { throw new BadRequestHttpException('订单状态异常'); } $order->shipping_type = $data['shipping_type']; } /** * @param Order $order * @param array $data * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException * @throws Throwable * @throws yii\db\StaleObjectException * 确定订单 */ private function confirm($order, $data) { if ($order->status !== Order::STATUS_UNCONFIRMED) { throw new BadRequestHttpException('订单状态异常'); } if ($order->shipping_type === Order::SHIPPING_TYPE_PICKED_UP) { if ($data['consignee'] ?? false || $data['phone'] ?? false) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } $order->consignee = $data['consignee']; $order->phone = $data['phone']; } if (empty($order->consignee)) { throw new BadRequestHttpException('需先填写收件人信息'); } if (isset($data['remarks'])) { $order->remarks = $data['remarks']; } Helper::delCarts($this->getCartsIds($order)); //删除购物车对应商品 $order->status = Order::STATUS_NONPAYMENT; $this->updateStock($order, self::TYPE_SUB_STOCK); } /** * @param $order * @return array * 获取订单商品对应购物车商品id */ private function getCartsIds($order) { $allGoods = $this->findOrderGoods($order->id); $ids = []; foreach ($allGoods as $goods) { $cart = Cart::findOne(['goods_id' => $goods->goods_id, 'user_id' => Yii::$app->user->getId()]); if ($cart) { $ids[] = $cart->id; } } return $ids; } /** * @param Order $order * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException * 取消订单 */ private function cancel($order) { if ($order->status !== Order::STATUS_NONPAYMENT) { throw new BadRequestHttpException('订单状态异常'); } $order->status = Order::STATUS_CANCEL; $this->updateStock($order, self::TYPE_ADD_STOCK);/*更新库存*/ } /** * @param Order $order * @throws BadRequestHttpException * 确认收货 */ private function take($order) { if ($order->status !== Order::STATUS_SHIPMENT_ALL) { throw new BadRequestHttpException('订单状态异常'); } $order->status = Order::STATUS_FINISH; } /** * @param $id * @param $type * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException * 更新库存 */ private function updateStock($id, $type) { $allOrderGoods = $this->findOrderGoods($id); foreach ($allOrderGoods as $orderGoods) { /*检查库存*/ Helper::checkStock($orderGoods->goods_id, $orderGoods->goods_count, $orderGoods->sku_id); if ($orderGoods->sku_id) { $goods = GoodsSku::findOne($orderGoods->sku_id); } else { $goods = Goods::findOne($orderGoods->goods_id); } if ($type) { $count = $orderGoods->goods_count; } else { $count = -$orderGoods->goods_count; } if (!$goods->updateCounters(['stock' => $count])) { throw new ServerErrorHttpException('更新库存失败'); } } } /** * @param array $cartIds 购物车商品id * @param int $id 订单id * @throws Exception * @throws NotFoundHttpException * 通过购物车添加商品 */ private function addGoodsByCart($cartIds, $id) { foreach ($cartIds as $cartId) { $cart = Cart::findOne($cartId); if (!$cart) { throw new NotFoundHttpException('购物车未找到'); } $this->addGoods($id, $cart->goods_id, $cart->sku_id, $cart->goods_count); } } /** * @param Order $order * @throws Exception * 保存商品总价格和总数量信息到 */ private function saveGoodsInfo($order) { $orderGoods = $this->findOrderGoods($order->id); foreach ($orderGoods as $goods) { $order->goods_amount += $goods->price; $order->goods_count += $goods->goods_count; } if (!$order->save()) { throw new Exception('服务器创建订单失败'); } } /** * @param $id * @param int $goodsId * @param $skuId * @param $count * @throws Exception * @throws NotFoundHttpException * 添加商品到订单 */ private function addGoods($id, $goodsId, $skuId, $count) { $goods = Goods::findOne($goodsId); $orderGoods = new OrderGoods(); $orderGoods->order_id = $id; $orderGoods->goods_id = $goodsId; $orderGoods->sku_id = $skuId ?: 0; $orderGoods->sku_value = Helper::skuValue($skuId); $orderGoods->weight = $skuId ? $this->skuWeight($skuId) : $goods->weight; $orderGoods->goods_count = $count; $orderGoods->goods_img = $goods->image; $orderGoods->goods_name = $goods->name; $orderGoods->price = Helper::goodsPrice($goodsId, $skuId); Helper::checkStock($goodsId, $count, $skuId); /*检查库存*/ if (!$orderGoods->save()) { throw new Exception('服务器添加订单商品失败'); } } private function skuWeight($skuId) { $sku = GoodsSku::findOne($skuId); if (!$sku) { throw new NotFoundHttpException('sku未找到'); } return $sku->weight; } /** * @return array|Order|null * 根据id获取订单 */ private function findOrder() { $id = Yii::$app->request->getQueryParam('id'); return Order::find() ->where(['id' => $id]) ->andWhere(['user_id' => Yii::$app->user->getId()]) ->one(); } /** * @param $id * @return array|ActiveRecord[]|OrderGoods[] * 根据订单 id 获取所有订单商品 */ private function findOrderGoods($id) { return OrderGoods::find() ->where(['order_id' => $id]) ->all(); } }