order_pre
parent
44dda50250
commit
3707ecab87
|
|
@ -5,14 +5,21 @@ namespace App\Admin\Actions\Show;
|
|||
use Dcat\Admin\Show\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
use App\Admin\Forms\ProfitSuccessForm;
|
||||
use App\Models\OrderProfit;
|
||||
|
||||
class ProfitSuccess extends AbstractTool
|
||||
{
|
||||
protected $title = '支付';
|
||||
|
||||
public function allowed()
|
||||
{
|
||||
$info = OrderProfit::find($this->getKey());
|
||||
return $info->status != 2;
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
$form = ProfitSuccessForm::make()->payload(['id'=>$this->getKey()]);;
|
||||
$form = ProfitSuccessForm::make()->payload(['id' => $this->getKey()]);;
|
||||
return Modal::make()
|
||||
->lg()
|
||||
->title($this->title)
|
||||
|
|
|
|||
|
|
@ -80,13 +80,13 @@ class OrderService
|
|||
//操作订单状态-需要调整为统一支付方法
|
||||
$orderService = new EndpointOrderService();
|
||||
$orderService->pay($order, PayWay::Offline);
|
||||
//注册支付成功事件
|
||||
OrderPaid::dispatch($order);
|
||||
//记录操作日志
|
||||
// 记录操作日志
|
||||
OrderLog::create([
|
||||
'order_id'=> $order->id,
|
||||
'content'=> '修改订单状态为【已支付】',
|
||||
]);
|
||||
//注册支付成功事件
|
||||
OrderPaid::dispatch($order);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Controllers\Order;
|
||||
|
||||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\{OrderPre, ProductSku};
|
||||
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' => '未选择商品'
|
||||
]);
|
||||
|
||||
$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' : '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 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));
|
||||
}
|
||||
}
|
||||
|
|
@ -217,4 +217,9 @@ Route::group([
|
|||
});
|
||||
|
||||
Route::apiResource('store', StoreController::class)->only(['index', 'show']);
|
||||
|
||||
Route::group(['middleware' => ['auth:api', \App\Endpoint\Api\Http\Middleware\CheckUserStatus::class]], function () {
|
||||
Route::post('order-pre', [App\Endpoint\Api\Http\Controllers\Order\OrderPreController::class, 'store']);
|
||||
Route::post('order-pre/order', [App\Endpoint\Api\Http\Controllers\Order\OrderPreController::class, 'storeOrder']);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Events\OrderPaid;
|
||||
use App\Models\{Order, OrderPre};
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class OrderPackage
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param \App\Events\OrderPaid $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(OrderPaid $event)
|
||||
{
|
||||
$order = Order::find($event->order->id);
|
||||
|
||||
// 订单来源为 帮客户下单 order_pres
|
||||
$source = $order->source;
|
||||
if ($source instanceof OrderPre) {
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$order_products = $order->products;
|
||||
// 根据 order_pres 发货数量, 自动发货
|
||||
$service_package = new \App\Admin\Services\OrderPackageService();
|
||||
// order_product_id: 订单商品ID, quantity: 发货数量
|
||||
$package_params = [];
|
||||
foreach($source->products as $item) {
|
||||
$sku_id = $item['sku_id'];
|
||||
$send = $item['send'];
|
||||
$order_product = $order_products->firstWhere('sku_id', $sku_id);
|
||||
if ($order_product) {
|
||||
array_push($package_params, [
|
||||
'order_product_id' => $order_product->id,
|
||||
'quantity' => $send
|
||||
]);
|
||||
}
|
||||
}
|
||||
// 发货
|
||||
if (count($package_params) > 0) {
|
||||
$service_package->createPackage($order, [
|
||||
'shipping_company' => '自提',
|
||||
'shipping_number' => '0',
|
||||
'packages' => $package_params
|
||||
]);
|
||||
}
|
||||
DB::commit();
|
||||
} catch (Throwable $th) {
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
logger('出错了');
|
||||
logger($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -89,6 +89,10 @@ class Order extends Model
|
|||
'sales_value',
|
||||
'bargain_amount',
|
||||
'profit',
|
||||
'source_type',
|
||||
'source_id',
|
||||
'store_id',
|
||||
'inviter_id',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -212,6 +216,14 @@ class Order extends Model
|
|||
return $this->hasMany(OrderProfit::class, 'order_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单来源
|
||||
*/
|
||||
public function source()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 此订单是否待付款
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
|
||||
class OrderPre extends Model
|
||||
{
|
||||
use HasFactory, HasDateTimeFormatter;
|
||||
|
||||
protected $fillable = ['id', 'store_id', 'user_id', 'remarks', 'products', 'others'];
|
||||
|
||||
protected $casts = [
|
||||
'products' => 'array',
|
||||
'others' => 'array'
|
||||
];
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ class EventServiceProvider extends ServiceProvider
|
|||
\App\Events\OrderPaid::class => [
|
||||
\App\Listeners\OrderPaidNotify::class,
|
||||
\App\Listeners\SendCoupons::class,
|
||||
\App\Listeners\OrderPackage::class,
|
||||
],
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ class OrderService
|
|||
* @param BargainOrder|null $bargainOrder
|
||||
* @return \App\Models\Order
|
||||
*/
|
||||
protected function createOrder(
|
||||
public function createOrder(
|
||||
User $user,
|
||||
array $products,
|
||||
?int $shippingAddressId,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace App\Services;
|
|||
|
||||
use App\Exceptions\BizException;
|
||||
use App\Exceptions\InvalidPaySerialNumberException;
|
||||
use App\Models\Order;
|
||||
use App\Models\{Order, OrderPre};
|
||||
use App\Models\PayLog;
|
||||
|
||||
class PayService
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ class AddInvitorIdToOrders extends Migration
|
|||
$table->decimal('profit')->default(0)->comment('产生的提成');
|
||||
$table->unsignedBigInteger('store_id')->nullable()->comment('关联门店');
|
||||
$table->unsignedBigInteger('inviter_id')->nullable()->comment('关联员工');
|
||||
$table->string('source_type')->nullable()->comment('来源');
|
||||
$table->unsignedBigInteger('source_id')->nullable()->comment('来源');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateOrderPresTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('order_pres', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('store_id')->comment('关联门店, stores.id');
|
||||
$table->unsignedBigInteger('user_id')->comment('关联员工, users.id');
|
||||
$table->string('remarks')->nullable()->comment('备注');
|
||||
$table->text('products')->comment('商品信息{sku_id, quantity, send}');
|
||||
$table->text('others')->comment('其他信息{coupon_id, note}');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('order_pres');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue