user(); if (!$user->userInfo->is_company) { throw new BizException('非内部员工' . $user->id); } $request->validate([ 'store_id' => 'required', 'products' => 'required|array', 'products.*.sku_id' => 'required', 'products.*.quantity' => 'required', 'products.*.send' => 'required', ], [ 'store_id.required' => '店铺ID必须', 'products.required' => '未选择商品' ]); $products = $request->input('products'); // 验证商品的购买数量 大于 发货数量 foreach($products as $item) { $quantity = $item['quantity']; $send = $item['send']; if ($send > $quantity) { throw new BizException('商品购买数量大于发货数量, ' . $item['sku_id']); } } $order_pre = OrderPre::create([ 'user_id' => $user->id, 'store_id' => $request->input('store_id'), 'products' => $request->input('products'), 'remarks' => $request->input('remarks'), 'others' => [ 'coupon_id' => $request->input('coupon_id'), 'note' => $request->input('note') ] ]); $app = $this->getWechatApp(); $scene = http_build_query([ 'order_pre' => $order_pre->id, ]); $response = $app->app_code->getUnlimit($scene, [ 'page' => 'pages/welcome/index', 'check_path' => false, 'env_version' => app()->isProduction() ? 'release' : 'develop', 'width' => $request->input('width', 200), ]); if ($response instanceof StreamResponse) { return response()->json([ 'order_pre' => $order_pre->id, 'image' => 'data:image/png;base64,'.base64_encode($response->getBody()), ]); } throw new BizException('生成失败, 请稍后再试'); } public function storeOrder(Request $request) { $id = $request->input('id'); $order_pre = OrderPre::findOrFail($id); $user = $request->user(); $products = []; foreach($order_pre->products as $item) { array_push($products, [ 'sku' => ProductSku::findOrFail($item['sku_id']), 'quantity' => $item['quantity'] ]); } $coupon_id = data_get($order_pre, 'others.coupon_id'); $note = data_get($order_pre, 'others.note'); try { DB::beginTransaction(); $service = new OrderService(); $order = $service->createOrder($user, $products, null, $coupon_id, $note); $order->update([ 'store_id' => $order_pre->store_id, 'inviter_id' => $order_pre->user_id, 'source_type' => OrderPre::class, 'source_id' => $order_pre->id, ]); DB::commit(); return response()->json([ 'order_id' => $order->id ]); } catch (Throwable $e) { DB::rollBack(); report($e); throw new BizException($e->getMessage()); } } protected function getWechatApp($getway = 'default') { return \EasyWeChat\Factory::miniProgram(config('wechat.mini_program.' . $getway)); } }