117 lines
3.2 KiB
PHP
117 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\SystemConfig;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class ThirdPartyConfigController extends Controller
|
|
{
|
|
/**
|
|
* 获取第三方配置分组
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$group = $request->get('group', 'third_party');
|
|
|
|
$configs = SystemConfig::where('group', $group)
|
|
->where('status', 'active')
|
|
->orderBy('sort', 'asc')
|
|
->get()
|
|
->map(fn($c) => [
|
|
'id' => $c->id,
|
|
'key' => $c->key,
|
|
'value' => $this->maskSensitiveValue($c->key, $c->value),
|
|
'type' => $c->type,
|
|
'label' => $c->label,
|
|
'description' => $c->description,
|
|
]);
|
|
|
|
return response()->json([
|
|
'code' => 200,
|
|
'data' => $configs,
|
|
'message' => 'success',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 批量更新配置
|
|
*/
|
|
public function update(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'configs' => 'required|array',
|
|
'configs.*.key' => 'required|string',
|
|
'configs.*.value' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['code' => 422, 'message' => '验证失败', 'errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$group = $request->get('group', 'third_party');
|
|
|
|
foreach ($request->configs as $config) {
|
|
// 跳过空值和掩码值
|
|
$value = $config['value'];
|
|
if ($value === '' || $value === '******') {
|
|
continue;
|
|
}
|
|
|
|
SystemConfig::updateOrCreate(
|
|
['group' => $group, 'key' => $config['key']],
|
|
[
|
|
'value' => $value,
|
|
'type' => $config['type'] ?? 'text',
|
|
'label' => $config['label'] ?? $config['key'],
|
|
'status' => 'active',
|
|
]
|
|
);
|
|
|
|
// 清除缓存
|
|
\Illuminate\Support\Facades\Cache::forget("system_config_{$group}_{$config['key']}");
|
|
\Illuminate\Support\Facades\Cache::forget("system_config_group_{$group}");
|
|
}
|
|
|
|
return response()->json([
|
|
'code' => 200,
|
|
'message' => '配置保存成功',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取单个配置(原文)
|
|
*/
|
|
public function get(Request $request, string $key)
|
|
{
|
|
$group = $request->get('group', 'third_party');
|
|
$value = SystemConfig::getValue($group, $key);
|
|
|
|
return response()->json([
|
|
'code' => 200,
|
|
'data' => ['value' => $value],
|
|
'message' => 'success',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 掩码敏感值
|
|
*/
|
|
private function maskSensitiveValue(string $key, ?string $value): ?string
|
|
{
|
|
if (!$value) {
|
|
return null;
|
|
}
|
|
|
|
$sensitiveKeys = ['secret', 'password', 'appsecret', 'accesssecret', 'private_key'];
|
|
foreach ($sensitiveKeys as $sk) {
|
|
if (stripos($key, $sk) !== false) {
|
|
return '******';
|
|
}
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|