auth.php
10.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* 认证模块
* 处理微信登录、Session 管理
*/
class AuthService
{
private $pdo;
private $config;
private $redis;
public function __construct($pdo, $config, $redis = null)
{
$this->pdo = $pdo;
$this->config = $config;
$this->redis = $redis;
}
/**
* 微信小程序登录
* @param string $code 微信 login code
* @param string $phone 手机号
* @param string $phoneCode 手机号 code (用于获取真实手机号)
* @return array
*/
public function wechatLogin($code, $phone = null, $phoneCode = null)
{
// 1. 用 code 换取 openid
$wxSession = $this->code2Session($code);
if (!$wxSession || empty($wxSession['openid'])) {
return ['ok' => false, 'error' => '微信登录失败'];
}
$openid = $wxSession['openid'];
$unionid = $wxSession['unionid'] ?? null;
// 2. 如果有 phoneCode,获取真实手机号
if ($phoneCode) {
$this->logDebug("Fetching phone with code: ... " . substr($phoneCode, -4));
$realPhone = $this->getPhoneNumber($phoneCode);
$this->logDebug("Resolved phone number: " . ($realPhone ?: 'NULL'));
if ($realPhone) {
$phone = $realPhone;
}
}
$this->logDebug("Final Phone before check: " . ($phone ?: 'EMPTY'));
if (empty($phone)) {
$this->logDebug("Error: Phone is empty");
return ['ok' => false, 'error' => '手机号不能为空'];
}
// 3. 查找或创建用户
try {
$user = $this->findOrCreateUser($phone, $openid, $unionid);
} catch (Exception $e) {
$this->logDebug("Exception in findOrCreateUser: " . $e->getMessage());
return ['ok' => false, 'error' => 'Database Error: ' . $e->getMessage()];
}
if (!$user) {
$this->logDebug("Error: Failed to find/create user (Result is null)");
return ['ok' => false, 'error' => '用户创建失败'];
}
$this->logDebug("User ID: " . $user['id']);
// 4. 创建 Session
try {
$token = $this->createSession($user['id']);
} catch (Exception $e) {
$this->logDebug("Exception in createSession: " . $e->getMessage());
return ['ok' => false, 'error' => 'Session Error: ' . $e->getMessage()];
}
if (!$token) {
$this->logDebug("Error: Failed to create session");
return ['ok' => false, 'error' => 'Session 创建失败'];
}
// 5. 获取绑定的设备
$devices = $this->getUserDevices($user['id']);
return [
'ok' => true,
'token' => $token,
'user' => [
'id' => $user['id'],
'phone' => $user['phone'],
'nickname' => $user['nickname'],
'avatar_url' => $user['avatar_url'],
],
'devices' => $devices
];
}
/**
* 验证 Token
* @param string $token
* @return array|null 用户信息或 null
*/
public function verifyToken($token)
{
if (empty($token)) {
return null;
}
// 先从 Redis 缓存查
if ($this->redis) {
$cached = $this->redis->get("session:$token");
if ($cached) {
return json_decode($cached, true);
}
}
// 从数据库查
$stmt = $this->pdo->prepare("
SELECT s.*, u.phone, u.nickname, u.avatar_url
FROM sessions s
JOIN users u ON s.user_id = u.id
WHERE s.token = ? AND s.expires_at > NOW()
");
$stmt->execute([$token]);
$session = $stmt->fetch();
if (!$session) {
return null;
}
$user = [
'id' => $session['user_id'],
'phone' => $session['phone'],
'nickname' => $session['nickname'],
'avatar_url' => $session['avatar_url'],
];
// 缓存到 Redis
if ($this->redis) {
$ttl = strtotime($session['expires_at']) - time();
if ($ttl > 0) {
$this->redis->setex("session:$token", $ttl, json_encode($user));
}
}
return $user;
}
/**
* 注销登录
* @param string $token
* @return bool
*/
public function logout($token)
{
// 删除数据库记录
$stmt = $this->pdo->prepare("DELETE FROM sessions WHERE token = ?");
$stmt->execute([$token]);
// 删除 Redis 缓存
if ($this->redis) {
$this->redis->del("session:$token");
}
return true;
}
/**
* 微信 code2Session
*/
private function code2Session($code)
{
$appId = $this->config['wechat']['app_id'];
$appSecret = $this->config['wechat']['app_secret'];
if (empty($appId) || empty($appSecret)) {
// 开发模式:返回模拟数据
return [
'openid' => 'dev_openid_' . substr(md5($code), 0, 16),
'session_key' => 'dev_session_key',
];
}
$url = "https://api.weixin.qq.com/sns/jscode2session?" . http_build_query([
'appid' => $appId,
'secret' => $appSecret,
'js_code' => $code,
'grant_type' => 'authorization_code'
]);
$response = file_get_contents($url);
if (!$response) {
return null;
}
$data = json_decode($response, true);
if (isset($data['errcode']) && $data['errcode'] != 0) {
error_log("WeChat code2Session error: " . json_encode($data));
return null;
}
return $data;
}
/**
* 获取微信手机号
*/
private function logDebug($msg)
{
file_put_contents(__DIR__ . '/debug.log', "[" . date('Y-m-d H:i:s') . "] " . $msg . "\n", FILE_APPEND);
}
private function getPhoneNumber($phoneCode)
{
$appId = $this->config['wechat']['app_id'];
$appSecret = $this->config['wechat']['app_secret'];
$this->logDebug("Checking WeChat Config - AppID: {$appId}, SecretLen: " . strlen($appSecret));
if (empty($appId) || empty($appSecret)) {
$this->logDebug("AppID or Secret is empty!");
return null; // 开发模式不支持
}
// 1. 获取 access_token
$tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" . http_build_query([
'grant_type' => 'client_credential',
'appid' => $appId,
'secret' => $appSecret,
]);
$tokenRaw = file_get_contents($tokenUrl);
$this->logDebug("Access Token Raw Response: {$tokenRaw}");
$tokenRes = json_decode($tokenRaw, true);
if (empty($tokenRes['access_token'])) {
$this->logDebug("Failed to get access token.");
return null;
}
// 2. 获取手机号
$phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $tokenRes['access_token'];
$phoneRaw = file_get_contents($phoneUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode(['code' => $phoneCode])
]
]));
$this->logDebug("Phone Number Raw Response: {$phoneRaw}");
$phoneRes = json_decode($phoneRaw, true);
if (isset($phoneRes['phone_info']['purePhoneNumber'])) {
return $phoneRes['phone_info']['purePhoneNumber'];
}
$this->logDebug("Failed to parse phone number from response.");
return null;
}
/**
* 查找或创建用户
*/
private function findOrCreateUser($phone, $openid, $unionid = null)
{
// 先按手机号查找
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE phone = ?");
$stmt->execute([$phone]);
$user = $stmt->fetch();
if ($user) {
// 更新 openid
if ($openid && $user['wx_openid'] !== $openid) {
$stmt = $this->pdo->prepare("UPDATE users SET wx_openid = ? WHERE id = ?");
$stmt->execute([$openid, $user['id']]);
}
return $user;
}
// 创建新用户
$stmt = $this->pdo->prepare("
INSERT INTO users (phone, wx_openid, nickname)
VALUES (?, ?, ?)
");
$nickname = '用户' . substr($phone, -4);
$stmt->execute([$phone, $openid, $nickname]);
return [
'id' => $this->pdo->lastInsertId(),
'phone' => $phone,
'wx_openid' => $openid,
'nickname' => $nickname,
'avatar_url' => null,
];
}
/**
* 创建 Session
*/
private function createSession($userId)
{
$token = bin2hex(random_bytes(32));
$ttl = $this->config['session']['ttl'];
$expiresAt = date('Y-m-d H:i:s', time() + $ttl);
// 单点登录:删除该用户的其他 session
if ($this->config['session']['single_login']) {
$stmt = $this->pdo->prepare("SELECT token FROM sessions WHERE user_id = ?");
$stmt->execute([$userId]);
$oldTokens = $stmt->fetchAll(\PDO::FETCH_COLUMN);
// 删除 Redis 缓存
if ($this->redis && $oldTokens) {
foreach ($oldTokens as $oldToken) {
$this->redis->del("session:$oldToken");
}
}
// 删除数据库记录
$stmt = $this->pdo->prepare("DELETE FROM sessions WHERE user_id = ?");
$stmt->execute([$userId]);
}
// 插入新 session
$stmt = $this->pdo->prepare("
INSERT INTO sessions (user_id, token, expires_at)
VALUES (?, ?, ?)
");
$stmt->execute([$userId, $token, $expiresAt]);
return $token;
}
/**
* 获取用户绑定的设备
*/
private function getUserDevices($userId)
{
$stmt = $this->pdo->prepare("
SELECT d.id, d.name, d.status, d.last_seen, b.is_primary, b.bound_at
FROM user_device_bindings b
JOIN devices d ON b.device_id = d.id
WHERE b.user_id = ?
ORDER BY b.is_primary DESC, b.bound_at ASC
");
$stmt->execute([$userId]);
return $stmt->fetchAll();
}
}