diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderPreController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderPreController.php index 1bed5210..bde91a5f 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/OrderPreController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/OrderPreController.php @@ -20,53 +20,52 @@ class OrderPreController extends Controller { $user = $request->user(); if (!$user->userInfo->is_company) { - throw new BizException('非内部员工' . $user->id); + throw new BizException('非内部员工'); } $request->validate([ - 'store_id' => 'required', 'products' => 'required|array', 'products.*.sku_id' => 'required', 'products.*.quantity' => 'required', 'products.*.send' => 'required', ], [ - 'store_id.required' => '店铺ID必须', 'products.required' => '未选择商品' ]); // 验证商品是否属于该店铺 - $store = Store::findOrFail($request->input('store_id')); - - $products = $request->input('products'); - foreach($products as $item) { - $quantity = $item['quantity']; - $send = $item['send']; - $sku_id = $item['sku_id']; - - // 验证商品是否属于该店铺 - $store_product = $store->productSkus()->wherePivot('product_sku_id', $sku_id)->first(); - if (!$store_product) { - throw new BizException('店铺未出售商品: ' . $sku_id); - } - - // 验证商品是否可销售 - if (!$store_product->pivot->status) { - throw new BizException('店铺已下架商品: ' . $sku_id); - } - - // 验证商品库存(店铺库存) - if ($store_product->pivot->amount < $send) { - throw new BizException('店铺商品: ' . $store_product->name . ' 库存不足'); - } - - // 验证商品的购买数量 大于 发货数量 - if ($send > $quantity) { - throw new BizException('商品购买数量大于发货数量, ' . $sku_id); + if ($request->filled('store_id')) { + $store = Store::findOrFail($request->input('store_id')); + $products = $request->input('products'); + foreach($products as $item) { + $quantity = $item['quantity']; + $send = $item['send']; + $sku_id = $item['sku_id']; + + // 验证商品是否属于该店铺 + $store_product = $store->productSkus()->wherePivot('product_sku_id', $sku_id)->first(); + if (!$store_product) { + throw new BizException('店铺未出售商品: ' . $sku_id); + } + + // 验证商品是否可销售 + if (!$store_product->pivot->status) { + throw new BizException('店铺已下架商品: ' . $sku_id); + } + + // 验证商品库存(店铺库存) + if ($store_product->pivot->amount < $send) { + throw new BizException('店铺商品: ' . $store_product->name . ' 库存不足'); + } + + // 验证商品的购买数量 大于 发货数量 + if ($send > $quantity) { + throw new BizException('商品购买数量大于发货数量, ' . $sku_id); + } } } $order_pre = OrderPre::create([ 'user_id' => $user->id, - 'store_id' => $store->id, + 'store_id' => $request->input('store_id', 0), 'products' => $request->input('products'), 'remarks' => $request->input('remarks'), 'others' => [ @@ -75,8 +74,13 @@ class OrderPreController extends Controller ] ]); + $scene = http_build_query([ + 'o' => $order_pre->id, + 'i' => $user->userInfo->code, + ]); + if (config('app.env') == 'local') { - $url = 'https://ui-avatars.com/api/?name=pdkj'; + $url = 'https://ui-avatars.com/api/?name=pdkj&scene=' . urlencode($scene); $order_pre->update(['qrcode' => $url]); return response()->json([ 'order_pre' => $order_pre->id, @@ -84,34 +88,31 @@ class OrderPreController extends Controller ]); } + // 生成小程序码 $app = $this->getWechatApp(); - - $scene = http_build_query([ - 'o' => $order_pre->id, - 'i' => $user->userInfo->code, - ]); - $response = $app->app_code->getUnlimit($scene, [ 'page' => 'pages/welcome/index', 'check_path' => false, - // develop: 开发版, trial: 体验版 + // release: 正式版, develop: 开发版, trial: 体验版 'env_version' => app()->isProduction() ? 'release' : $request->input('env_version', 'trial'), 'width' => $request->input('width', 200), ]); - + + // 保存小程序码 if ($response instanceof StreamResponse) { - // 保存小程序码 $disk = Storage::disk('public'); $filepath = 'order-pre'; $filename = $order_pre->id . '.png'; $response->saveAs($disk->path($filepath), $filename); - + $url = $disk->url($filepath . '/' . $filename); - + $order_pre->update(['qrcode' => $url]); return response()->json([ 'order_pre' => $order_pre->id, 'image' => $url, + // 返回 base64 图片 + // 'data:image/png;base64,'.base64_encode($response->getBody()), ]); }