Files
zyt/test_gancao_config.php
T
2026-04-15 16:31:25 +08:00

194 lines
5.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 甘草配置检查脚本
* 用于检查 .env 配置是否正确,特别是检查是否有数组值导致 "Array to string conversion" 错误
*/
// 加载 ThinkPHP
require __DIR__ . '/server/vendor/autoload.php';
$app = new think\App();
$app->initialize();
echo "=== 甘草配置检查 ===\n\n";
// 获取配置
$config = think\facade\Config::get('gancao_scm', []);
echo "1. 检查配置是否存在:\n";
if (empty($config)) {
echo " ❌ 配置为空,请检查 .env 文件\n";
exit(1);
} else {
echo " ✓ 配置已加载\n\n";
}
echo "2. 检查各项配置:\n";
$requiredFields = [
'enabled' => 'boolean',
'gateway_url' => 'string',
'gateway_ak' => 'string',
'gateway_sk' => 'string',
'biz_ak' => 'string',
'biz_sk' => 'string',
'callback_url' => 'string',
'express_type' => 'string',
'cradle_store' => 'string',
'df_id' => 'integer',
];
$hasError = false;
foreach ($requiredFields as $field => $expectedType) {
$value = $config[$field] ?? null;
$actualType = gettype($value);
echo " - {$field}: ";
if ($value === null) {
echo "❌ 未配置\n";
$hasError = true;
continue;
}
// 检查类型
if ($actualType === 'array') {
echo "❌ 错误!值是数组,应该是 {$expectedType}\n";
echo " 实际值: " . json_encode($value, JSON_UNESCAPED_UNICODE) . "\n";
$hasError = true;
continue;
}
// 转换类型检查
$typeMatch = false;
switch ($expectedType) {
case 'string':
$typeMatch = is_string($value) || is_numeric($value);
break;
case 'integer':
$typeMatch = is_int($value) || is_numeric($value);
break;
case 'boolean':
$typeMatch = is_bool($value) || in_array($value, [0, 1, '0', '1', 'true', 'false'], true);
break;
}
if (!$typeMatch) {
echo "⚠️ 类型不匹配(期望: {$expectedType}, 实际: {$actualType}\n";
echo " 值: " . var_export($value, true) . "\n";
} else {
echo "✓ {$actualType}";
if ($expectedType === 'string' && strlen((string)$value) > 50) {
echo " (长度: " . strlen((string)$value) . ", 前50字符: " . substr((string)$value, 0, 50) . "...)";
} else {
echo " = " . var_export($value, true);
}
echo "\n";
}
}
echo "\n3. 检查可选配置:\n";
$optionalFields = [
'default_is_decoct' => 'boolean',
'default_times_per_day' => 'integer',
'default_num_per_pack' => 'integer',
'default_dose_ml' => 'integer',
'herb_id_map' => 'array',
];
foreach ($optionalFields as $field => $expectedType) {
$value = $config[$field] ?? null;
$actualType = gettype($value);
echo " - {$field}: ";
if ($value === null) {
echo "未配置(可选)\n";
continue;
}
if ($expectedType === 'array' && !is_array($value)) {
echo "⚠️ 应该是数组,实际是 {$actualType}\n";
} elseif ($expectedType !== 'array' && is_array($value)) {
echo "❌ 错误!值是数组,应该是 {$expectedType}\n";
$hasError = true;
} else {
echo "✓ {$actualType}\n";
}
}
echo "\n4. 测试 JSON 编码:\n";
// 模拟构建提交负载
$testPayload = [
'token' => 'test_token_123',
'df_id' => (int)($config['df_id'] ?? 101),
'amount' => 7,
'express_type' => isset($config['express_type']) ? (string)$config['express_type'] : 'sf',
'callback_url' => isset($config['callback_url']) ? (string)$config['callback_url'] : '',
'cradle_store' => isset($config['cradle_store']) ? (string)$config['cradle_store'] : 'default',
'express_to' => [
'name' => '测试患者',
'phone' => '13800138000',
'province' => '四川省',
'city' => '成都市',
'addr' => '测试地址123号',
],
'patient' => [
'name' => '测试患者',
'age' => '30',
'sex' => 0,
'phone' => '13800138000',
],
'doctor' => [
'name' => '测试医生',
],
'doct_advice' => [
'taboo' => '无',
'usage_time' => '饭后半小时服用',
'usage_brief' => '遵医嘱',
'others' => '',
'notes_doctor' => '',
],
'm_list' => [
['id' => 1, 'quantity' => 10.0, 'brief' => ''],
],
];
try {
$json = json_encode($testPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($json === false) {
echo " ❌ JSON 编码失败: " . json_last_error_msg() . "\n";
$hasError = true;
} else {
echo " ✓ JSON 编码成功\n";
echo " 负载大小: " . strlen($json) . " 字节\n";
// 检查是否包含 "Array" 字符串(可能是数组转字符串的迹象)
if (strpos($json, '"Array"') !== false || strpos($json, 'Array') !== false) {
echo " ⚠️ 警告:JSON 中包含 'Array' 字符串,可能存在数组转字符串问题\n";
$hasError = true;
}
}
} catch (\Throwable $e) {
echo " ❌ 异常: " . $e->getMessage() . "\n";
$hasError = true;
}
echo "\n";
if ($hasError) {
echo "=== 检查结果: ❌ 发现错误 ===\n";
echo "\n请修复以上错误后重试。\n";
echo "\n常见问题:\n";
echo "1. 配置值被错误地设置为数组\n";
echo "2. .env 文件格式错误(例如:GANCAO_SCM_EXPRESS_TYPE=[\"sf\"] 应该是 GANCAO_SCM_EXPRESS_TYPE=sf\n";
echo "3. 配置文件中有多余的引号或括号\n";
exit(1);
} else {
echo "=== 检查结果: ✓ 配置正常 ===\n";
exit(0);
}