-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrename.php
More file actions
170 lines (144 loc) · 5.4 KB
/
rename.php
File metadata and controls
170 lines (144 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
session_start();
$username = "匿名用户";
$token = "";
$authCode = "";
$responseBody = ""; // 新增:用于存储响应主体
if (isset($_SESSION['responseBody'])) {
$dt = is_string($_SESSION['responseBody']) ? json_decode($_SESSION['responseBody'], true) : $_SESSION['responseBody'];
} else {
$redirectUrl = '/getv.php?r=test.php';
header('Location: ' . $redirectUrl);
exit;
}
class AuthService {
private $baseUrl = 'http://nlm-api-cn.turtlesim.com/';
private $username;
public $token;
public $authCode;
public $responseBody; // 新增:存储API响应主体
public function __construct($username) {
$this->username = $username;
}
public function login() {
$identifier = bin2hex(random_bytes(20));
$identifier = str_pad($identifier, 40, '0', STR_PAD_LEFT);
$requestData = [
'Target' => $this->username,
'Version' => 2411,
'Language' => "Chinese",
];
$headers = [
"Content-Type: application/json",
"Accept: application/json",
"Accept-Language: zh-CN",
"x-API-Token: " . $_SESSION["token"],
"x-API-AuthCode: " . $_SESSION["authCode"],
];
$response = $this->postRequest("Users/Rename", $requestData, $headers);
$statusCode = $response['status'];
$this->responseBody = $response['body']; // 存储原始响应主体
$responseData = json_decode($this->responseBody, true);
// 检查JSON解码是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("API响应格式错误: " . json_last_error_msg());
}
// 优先检查业务状态码
if (isset($responseData['Status'])) {
if ($responseData['Status'] === 200) {
$this->token = $responseData['Token'] ?? '';
$this->authCode = $responseData['AuthCode'] ?? '';
return $responseData;
} else {
$errorMsg = $responseData['Message'] ?? '登录失败';
throw new Exception($errorMsg);
}
}
// 处理 HTTP 错误
elseif ($statusCode >= 400) {
$errorMap = [
403 => '访问被拒绝(403)',
404 => '资源不存在(404)',
500 => '服务器错误(500)'
];
$errorMsg = $errorMap[$statusCode] ?? "HTTP 错误: $statusCode";
throw new Exception($errorMsg);
}
// 完全无效的响应
else {
throw new Exception("无效的API响应");
}
}
private function postRequest($endpoint, $data, $headers = []) {
$url = $this->baseUrl . $endpoint;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("请求失败: $error");
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $headerSize);
curl_close($ch);
return [
'status' => $statusCode,
'body' => $body
];
}
}
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
if (!empty($username)) {
$authService = new AuthService($username);
$loginData = $authService->login();
// 登录成功,保存到session
$_SESSION['username'] = $username;
// 重定向或显示成功消息
header('Location: /');
exit;
} else {
$error = "用户名和密码不能为空";
}
}
} catch (Exception $e) {
}
?>
<link rel="stylesheet" href="/styles/auth.css"/>
<div class="auth-container">
<div class="auth-card">
<div class="auth-header">
<h1>修改你的昵称</h1>
</div>
<?php if(isset($_SESSION['success'])): ?>
<div class="alert success">
<?= htmlspecialchars($_SESSION['success']) ?>
<?php unset($_SESSION['success']); ?>
</div>
<?php endif; ?>
<form method="post" class="auth-form">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" id="username" name="username"
value="<?= htmlspecialchars($_POST['username'] ?? '') ?>"
required autofocus
placeholder="请输入用户名">
<span class="input-icon">👤</span>
</div>
<button type="submit" class="btn primary">立即提交</button>
</form>
</div>
</div>