Files
advertising-system/update_token.php
Snowz 9653a70dad feat(umami): 新增token自动更新功能并优化缓存策略
- 添加Umami token自动获取和更新机制
- 实现token有效性检测和自动管理
- 新增update_token.php用于手动更新token
- 延长缓存时间至7天减少API调用
- 优化系统稳定性和维护性
2025-08-06 15:42:24 +08:00

116 lines
3.4 KiB
PHP
Raw Permalink 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
/**
* Umami Token 更新工具
* 用于手动更新或测试token获取功能
*/
// 配置信息(请根据实际情况修改)
$apiBaseUrl = 'https://um.com';
$username = 'your-username'; // 请替换为实际的用户名
$password = 'your-password'; // 请替换为实际的密码
$tokenFile = 'umami_token.json';
/**
* 获取新的Umami token
* @param string $apiBaseUrl API基础URL
* @param string $username 用户名
* @param string $password 密码
* @return array 包含token和状态信息的数组
*/
function getNewUmamiToken($apiBaseUrl, $username, $password) {
$loginUrl = "$apiBaseUrl/api/auth/login";
$loginData = json_encode([
'username' => $username,
'password' => $password
]);
$options = [
'http' => [
'method' => 'POST',
'header' => [
"Content-Type: application/json",
"Content-Length: " . strlen($loginData)
],
'content' => $loginData
]
];
$context = stream_context_create($options);
$response = @file_get_contents($loginUrl, false, $context);
if ($response === FALSE) {
return [
'success' => false,
'message' => 'Failed to connect to Umami API',
'token' => null
];
}
$responseData = json_decode($response, true);
if (isset($responseData['token'])) {
return [
'success' => true,
'message' => 'Token obtained successfully',
'token' => $responseData['token'],
'user_info' => $responseData['user'] ?? null
];
} else {
return [
'success' => false,
'message' => 'Invalid credentials or API response',
'token' => null,
'response' => $responseData
];
}
}
// 执行token获取
echo "正在获取新的Umami token...\n";
echo "API URL: $apiBaseUrl\n";
echo "Username: $username\n";
echo "\n";
$result = getNewUmamiToken($apiBaseUrl, $username, $password);
if ($result['success']) {
echo "✅ Token获取成功\n";
echo "Token: " . $result['token'] . "\n";
if (isset($result['user_info'])) {
echo "用户信息:\n";
echo " - ID: " . ($result['user_info']['id'] ?? 'N/A') . "\n";
echo " - Username: " . ($result['user_info']['username'] ?? 'N/A') . "\n";
echo " - Role: " . ($result['user_info']['role'] ?? 'N/A') . "\n";
}
// 保存token到缓存文件
$tokenData = [
'token' => $result['token'],
'timestamp' => time(),
'user_info' => $result['user_info'] ?? null
];
if (file_put_contents($tokenFile, json_encode($tokenData, JSON_PRETTY_PRINT))) {
echo "\n✅ Token已保存到缓存文件: $tokenFile\n";
} else {
echo "\n❌ 保存token到缓存文件失败\n";
}
} else {
echo "❌ Token获取失败\n";
echo "错误信息: " . $result['message'] . "\n";
if (isset($result['response'])) {
echo "API响应: " . json_encode($result['response'], JSON_PRETTY_PRINT) . "\n";
}
}
echo "\n";
echo "使用说明:\n";
echo "1. 请确保在文件顶部正确配置了API URL、用户名和密码\n";
echo "2. 确保Umami服务正常运行且网络连接正常\n";
echo "3. 如果获取失败,请检查用户名密码是否正确\n";
echo "4. Token将自动保存到 $tokenFile 文件中\n";
echo "5. info.php 会自动使用缓存的token\n";
?>