whereIn('id', $cartIds) ->where('uid', $request->user['uid']) ->select() ->toArray(); if (empty($cartList)) { return app('json')->fail('购物车商品不存在'); } // 获取收货地址 $address = null; if ($post['address_id'] > 0) { $address = Db::name('user_address')->where('id', $post['address_id'])->find(); } if (!$address) { $address = (new UserAddress())->getDefaultAddress($request->user['uid']); } if (!$address) { return app('json')->fail('请先添加收货地址'); } // 计算订单金额 $totalMoney = 0; $payMoney = 0; $cartInfo = []; foreach ($cartList as $cart) { $product = Db::name('store_product')->where('id', $cart['product_id'])->find(); if (!$product) { continue; } $price = $product['price']; $skuPrice = null; // 如果有SKU,获取SKU价格 if (!empty($cart['product_attr_unique'])) { $sku = (new StoreProductAttrValue())->getSkuBySuk($cart['product_id'], $cart['product_attr_unique']); if ($sku) { $price = $sku['price']; } } $productMoney = $price * $cart['cart_num']; $totalMoney += $productMoney; $cartInfo[] = [ 'product_id' => $cart['product_id'], 'cart_num' => $cart['cart_num'], 'product_attr_unique' => $cart['product_attr_unique'], 'title' => $product['title'], 'image' => $product['image'], 'price' => $price, 'total_price' => $productMoney, ]; } // 运费(暂时设置为0,后续可以根据运费模板计算) $postage = 0; $payMoney = $totalMoney + $postage; // 生成订单号 $orderId = makeOrderId($request->user['uid'], 'SO'); // 开启事务 Db::startTrans(); try { // 创建订单 $orderData = [ 'order_id' => $orderId, 'uid' => $request->user['uid'], 'real_name' => $address['real_name'], 'phone' => $address['phone'], 'province' => $address['province'], 'city' => $address['city'], 'district' => $address['district'], 'detail' => $address['detail'], 'total_price' => $totalMoney, 'total_num' => count($cartInfo), 'total_postage' => $postage, 'pay_price' => $payMoney, 'pay_postage' => $postage, 'paid' => 0, 'status' => 0, 'time' => time(), ]; $orderIdDb = Db::name('store_order')->insertGetId($orderData); // 创建订单商品 foreach ($cartInfo as &$item) { $item['oid'] = $orderIdDb; $item['time'] = time(); } Db::name('store_order_cart_info')->insertAll($cartInfo); // 创建支付流水 $payTrade = new PayTrade(); $payNo = $payTrade->credentials( 'score', $request->user['uid'], 'shop_order', $payMoney, '商城订单支付', $orderId, ['order_id' => $orderId], 0 ); // 更新支付流水的o_id if ($payNo) { Db::name('pay_trade')->where('pay_no', $payNo)->update(['o_id' => $orderIdDb]); } // 清除购物车 Db::name('store_cart')->whereIn('id', $cartIds)->delete(); Db::commit(); return app('json')->success([ 'order_id' => $orderId, 'pay_money' => $payMoney, 'pay_no' => $payNo ], '订单创建成功'); } catch (DbException $e) { Db::rollback(); return app('json')->fail('订单创建失败'); } } /** * 订单列表 * @param Request $request */ public function orderList(Request $request) { $post = UtilService::getMore([ ['page', 1], ['pageSize', 20], ['status', ''], ], $request); $where = [ ['uid', '=', $request->user['uid']] ]; if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1", "2", "3", "-1"])) { $where[] = ['status', '=', (int)$post['status']]; } $result = (new StoreOrder())->getList($where, $post['page'], $post['pageSize']); return app('json')->success($result); } /** * 订单详情 * @param Request $request */ public function orderDetail(Request $request) { $post = UtilService::getMore([ ['order_id', '', 'empty', '参数错误'], ], $request); $order = Db::name('store_order') ->where('order_id', $post['order_id']) ->where('uid', $request->user['uid']) ->find(); if (!$order) { return app('json')->fail('订单不存在'); } // 获取订单商品 $cartInfo = Db::name('store_order_cart_info') ->where('oid', $order['id']) ->select() ->toArray(); $order['cart_info'] = $cartInfo; return app('json')->success($order); } /** * 取消订单 * @param Request $request */ public function cancelOrder(Request $request) { $post = UtilService::getMore([ ['order_id', '', 'empty', '参数错误'], ], $request); $order = Db::name('store_order') ->where('order_id', $post['order_id']) ->where('uid', $request->user['uid']) ->where('status', 0) ->find(); if (!$order) { return app('json')->fail('订单不存在或已取消'); } Db::name('store_order')->where('id', $order['id'])->update(['status' => -1]); return app('json')->success('订单已取消'); } /** * 确认收货 * @param Request $request */ public function confirmOrder(Request $request) { $post = UtilService::getMore([ ['order_id', '', 'empty', '参数错误'], ], $request); $order = Db::name('store_order') ->where('order_id', $post['order_id']) ->where('uid', $request->user['uid']) ->where('status', 2) ->find(); if (!$order) { return app('json')->fail('订单不存在或状态错误'); } Db::name('store_order')->where('id', $order['id'])->update([ 'status' => 3, 'confirm_time' => time() ]); return app('json')->success('确认收货成功'); } }