6
0
Fork 0
base
panliang 2023-02-11 11:05:47 +08:00
parent ce18e17186
commit 39f9724585
1 changed files with 44 additions and 43 deletions

View File

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