Browse Source

Merge branch 'config-setting' into dev

moell 1 year ago
parent
commit
dc3ea383c0

+ 52 - 0
app/Http/Controllers/API/ConfigController.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Http\Controllers\API;
+
+use App\Http\Controllers\Controller;
+use App\Http\Requests\API\Config\AllowSettingConfig;
+use App\Models\Config;
+use Illuminate\Http\Request;
+
+class ConfigController extends Controller
+{
+    public function index(Request $request)
+    {
+        $groups = $request->get("group", []);
+
+        $data = [];
+
+        if ($groups) {
+            $data = Config::query()->whereIn("group", $groups)->get([
+                'group', 'key', 'value'
+            ])->groupBy("group");
+        }
+
+        return $this->success([
+            'data' => $data
+        ]);
+    }
+
+    public function setting(Request $request)
+    {
+        $allowSettingConfig = new AllowSettingConfig();
+
+        foreach ($request->all() as $item) {
+            try {
+                $allowSettingConfig->check($item['group'], $item['key'], $item['value']);
+            } catch (\Exception $exception) {
+                return $this->forbidden($exception->getMessage());
+            }
+        }
+
+        foreach ($request->all() as $item) {
+            Config::query()->updateOrCreate([
+                'group' => $item['group'],
+                'key' => $item['key'],
+            ], [
+                'value' => $item['value']
+            ]);
+        }
+
+        return $this->noContent();
+    }
+}

+ 48 - 0
app/Http/Requests/API/Config/AllowSettingConfig.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Http\Requests\API\Config;
+
+class AllowSettingConfig
+{
+    public function check(string $group, string $key, mixed $value)
+    {
+        if (! method_exists($this, $group)) {
+            throw new \Exception(sprintf("%s configuration group is not supported", $group));
+        }
+
+        $groupRules = $this->$group();
+
+        if (! isset($groupRules[$key])) {
+            throw new \Exception(sprintf("The %s configuration field under the %s group is not supported", $key, $group));
+        }
+
+        $result = validator([$key => $value], [$key => $groupRules[$key]]);
+        if ($result->fails()) {
+            throw new \Exception($result->errors()->first());
+        }
+    }
+
+    /**
+     * email 字段验证
+     *
+     * @return string[]
+     */
+    private function email(): array
+    {
+        return [
+            "email_notification" => "in:on,off",
+            "async_sender" => "in:yes,no",
+            "sender_email" => "nullable|email",
+            "sender" => "nullable|min:1",
+            "domain" => "nullable|url",
+            "smtp_server" => "nullable|min:1",
+            "smtp_account" => "nullable|min:1",
+            "smtp_validation" => "in:yes,no",
+            "smtp_port" => "nullable|numeric",
+            "encryption" => "in:ssl,plain,tls",
+            "smtp_password" => "nullable|min:6",
+            "debug" => "in:off,normal,high",
+            "charset" => "in:utf8,gbk"
+        ];
+    }
+}

+ 13 - 0
app/Models/Config.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Config extends Model
+{
+    use HasFactory;
+
+    protected $guarded = ['id'];
+}

+ 30 - 0
database/migrations/2024_03_04_212018_create_configs_table.php

@@ -0,0 +1,30 @@
+<?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('configs', function (Blueprint $table) {
+            $table->id();
+            $table->string("group", 100);
+            $table->string("key", 100);
+            $table->string("value")->nullable();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('configs');
+    }
+};

+ 3 - 0
routes/api.php

@@ -84,5 +84,8 @@ Route::middleware(['auth:sanctum'])->group(function () {
         Route::delete("folder/{folder}", [API\FolderController::class, "destroy"])->name("folder.destroy");
 
         Route::post("requirement/batch", [API\RequirementController::class, "batchStore"])->name("requirement.batchStore");
+
+        Route::get("config", [API\ConfigController::class, "index"])->name("config.index");
+        Route::post("config-setting", [API\ConfigController::class, "setting"])->name("config.setting");
     });
 });