seven il y a 1 mois
Parent
commit
29dcb9ac77

+ 16 - 0
app/Models/Timezone.php

@@ -0,0 +1,16 @@
+<?php
+
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Timezone extends BaseModel
+{
+    protected $table = 'timezones';
+    protected $fillable = ['name', 'code', 'status'];
+
+    const STATUS_YES = 1;//正常
+    const STATUS_NO = 0;//禁用
+
+}

+ 30 - 0
app/Providers/ApiFootballServiceProvider.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Providers;
+
+use App\Services\ApiFootball\Sdk;
+use Illuminate\Support\ServiceProvider;
+
+class ApiFootballServiceProvider extends ServiceProvider
+{
+    /**
+     * Register services.
+     */
+    public function register(): void
+    {
+        // $this->app->singleton(Sdk::class, function ($app) {
+        //     return new Sdk();
+        // });
+        
+        // 或者使用别名
+        $this->app->alias(Sdk::class, 'api-football');
+    }
+
+    /**
+     * Bootstrap services.
+     */
+    public function boot(): void
+    {
+        //
+    }
+}

+ 59 - 0
app/Services/ApiFootball/Client.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Services\ApiFootball;
+
+use Illuminate\Support\Facades\Http;
+
+
+class Client
+{
+
+    // 
+    public static function get($endpoint, $params = [])
+    {
+        $response = Http::withHeaders([
+            'x-apisports-key' => config('services.api_football.key'),
+        ])
+        ->withoutVerifying()  // 临时跳过 SSL 验证
+        ->get(config('services.api_football.host') .'/'. $endpoint, $params);
+
+        if ($response->successful()) {
+            return $response->json();
+        }
+
+        // Handle errors as needed
+        throw new \Exception("API request failed: " . $response->body());
+    }
+
+    public static function post($endpoint, $data = [])
+    {
+        $response = Http::withHeaders([
+            'x-apisports-key' => config('services.api_football.key'),
+        ])->post(config('services.api_football.host') .'/'. $endpoint, $data);
+
+        if ($response->successful()) {
+            return $response->json();
+        }
+
+        // Handle errors as needed
+        throw new \Exception("API request failed: " . $response->body());
+    }
+
+    // 时区
+    public static function timezone()
+    {
+        return self::get('timezone');
+    }
+
+    // 国家/地区
+    public static function countries($params = [])
+    {
+        return self::get('countries', $params);
+    }
+
+    // 赛程
+    public static function fixtures($params = [])
+    {
+        return self::get('fixtures', $params);
+    }
+}

+ 23 - 0
app/Services/ApiFootball/Sdk.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Services\ApiFootball;
+
+class Sdk
+{
+    public function timezone()
+    {
+        $result = Client::timezone();
+        return $result;
+    }
+
+    public function countries($params = [])
+    {
+        $result = Client::countries($params);
+        return json_decode($result, true);
+    }
+
+    public function fixtures($params = [])
+    {
+        return Client::fixtures($params);
+    }
+}

+ 40 - 0
app/Services/SyncFootballDataService.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Services;
+
+use App\Services\BaseService;
+use App\Models\Timezone;
+
+class SyncFootballDataService extends BaseService
+{
+    public static function syncTimezone()
+    {
+        $result = app('api-football')->timezone();
+        $response = $result['response'] ?? [];
+        
+        if (empty($response)) {
+            return $result;
+        }
+
+        // 先将所有时区设为无效
+        Timezone::query()->update(['status' => Timezone::STATUS_NO]);
+        
+        foreach ($response as $code) {
+            // 使用 firstOrCreate 简化代码
+            $timezone = Timezone::firstOrCreate(
+                ['code' => $code],
+                [
+                    'code' => $code,
+                    'status' => Timezone::STATUS_YES
+                ]
+            );
+            
+            // 如果已存在,更新状态
+            if (!$timezone->wasRecentlyCreated) {
+                $timezone->update(['status' => Timezone::STATUS_YES]);
+            }
+        }
+
+        return $result;
+    }
+}

+ 1 - 0
bootstrap/providers.php

@@ -1,5 +1,6 @@
 <?php
 
 return [
+    App\Providers\ApiFootballServiceProvider::class,
     App\Providers\AppServiceProvider::class,
 ];

+ 31 - 0
database/migrations/2026_03_03_164521_create_timezones_table.php

@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('timezones', function (Blueprint $table) {
+            $table->id();
+            $table->string('name', 100)->nullable()->comment('时区名称');
+            $table->string('code', 50)->nullable()->comment('时区代码');
+            $table->integer('status')->nullable()->default(1)->comment('状态: 1-正常, 0-禁用');
+            $table->timestamps();
+            $table->comment('时区表');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('timezones');
+    }
+};

+ 7 - 1
routes/admin.php

@@ -1,6 +1,12 @@
 <?php
 
 
-
 // use App\Http\Controllers\Admin\AdminController;
 
+use Illuminate\Support\Facades\Route;
+
+use App\Services\SyncFootballDataService;
+
+Route::get('/test', function () {
+    return SyncFootballDataService::syncTimezone();
+});