seven 2 日 前
コミット
5b4efc1cca

+ 42 - 0
app/Console/Commands/PaymentTask.php

@@ -0,0 +1,42 @@
+<?php
+
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Jobs\PaymentJob;
+use Illuminate\Support\Facades\Log;
+
+class PaymentStartFiveSecondTask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'payment_task';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->info('启动支付订单状态更新循环任务...');
+        Log::error('启动支付订单状态更新循环任务...'.now());
+        // 首次启动任务
+        PaymentJob::dispatch()->delay(now()->addSeconds(10));
+        
+        $this->info('任务已启动,将在支付订单状态更新后首次执行');
+        
+        return Command::SUCCESS;
+    }
+}

+ 68 - 0
app/Jobs/PaymentJob.php

@@ -0,0 +1,68 @@
+<?php
+
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Log;
+
+class PaymentJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * 任务尝试次数
+     */
+    public $tries = 3;
+
+    /**
+     * 任务超时时间(秒)
+     */
+    public $timeout = 30;
+
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        try {
+            Log::error('🚀 开始执行更新订单状态任务: ' . now());
+           
+
+            Log::error('📅 下一个更新订单状态任务已安排');
+
+        } catch (\Exception $e) {
+            Log::error('❌ 更新订单状态任务执行异常: ' . $e->getMessage());
+
+            // 异常后15秒重试
+            PaymentJob::dispatch()->delay(now()->addSeconds(15));
+        }
+    }
+
+    // 可选:失败处理
+    public function failed(\Throwable $exception)
+    {
+        Log::error('更新订单状态任务失败: ' . $exception->getMessage());
+
+        // 可选:重新分发或通知
+        dispatch(new self())->delay(now()->addSeconds(15));
+    }
+}

+ 1 - 0
app/Services/PaymentOrderService.php

@@ -292,6 +292,7 @@ class PaymentOrderService extends BaseService
 
                 // 付款
                 if ($info->type == self::TYPE_PAY) {
+                    $info->state = $params['state'];
                     if ($params['state'] == 1) {
                         $info->status = self::STATUS_SUCCESS;
                         $wallet = WalletService::findOne(['member_id' => $info->member_id]);

+ 34 - 0
database/migrations/2025_12_09_132633_add_state_to_payment_orders_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payment_orders', function (Blueprint $table) {
+            $table->tinyInteger('state')
+                  ->default(0)
+                  ->comment('0:待支付, 1:支付成功, 2:支付失败, 3:未出码, 4:异常');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payment_orders', function (Blueprint $table) {
+            //
+        });
+    }
+};