147 lines
4.7 KiB
PHP
147 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers\Order;
|
|
|
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
|
use App\Endpoint\Api\Http\Resources\OrderPreResource;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\{Order, OrderPre, ProductSku};
|
|
use App\Models\Store\Store;
|
|
use App\Services\OrderService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exceptions\BizException;
|
|
use Throwable;
|
|
use EasyWeChat\Kernel\Http\StreamResponse;
|
|
|
|
class OrderPreController extends Controller
|
|
{
|
|
public function store(Request $request)
|
|
{
|
|
$user = $request->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' => '未选择商品'
|
|
]);
|
|
|
|
// 验证商品是否属于该店铺
|
|
$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,
|
|
'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,
|
|
'invite_code' => $user->userInfo->code,
|
|
]);
|
|
|
|
$response = $app->app_code->getUnlimit($scene, [
|
|
'page' => 'pages/welcome/index',
|
|
'check_path' => false,
|
|
// develop: 开发版, trial: 体验版
|
|
'env_version' => app()->isProduction() ? 'release' : $request->input('env_version', 'trial'),
|
|
'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 show($id)
|
|
{
|
|
$info = OrderPre::findOrFail($id);
|
|
|
|
return OrderPreResource::make($info);
|
|
}
|
|
|
|
public function storeOrder(Request $request)
|
|
{
|
|
$id = $request->input('id');
|
|
$order_pre = OrderPre::findOrFail($id);
|
|
$user = $request->user();
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$service = new OrderService();
|
|
// 如果还有未支付的订单, 则不生成新订单
|
|
$order = $user->orders()->where([
|
|
'source_type' => OrderPre::class,
|
|
'source_id' => $order_pre->id,
|
|
'status' => Order::STATUS_PENDING,
|
|
])->first();
|
|
if (!$order) {
|
|
$order = $service->createOrderByPre($user, $order_pre);
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|