refactor(linuxdo): 重构 Linuxdo 登录功能

- 优化了数据库连接和查询逻辑
- 添加了账号绑定和登录的前端界面
- 实现了通过 API 进行账号注册和登录的后端逻辑
- 改进了代码格式和命名规范
This commit is contained in:
2024-12-22 11:06:11 +08:00
parent 88952cfb7a
commit 3be7b55f0e

View File

@@ -6,23 +6,24 @@ include 'function.php';
$act = input('act'); $act = input('act');
$mySQLi = new mysqli($_CONFIG_DB['db_host'], $_CONFIG_DB['db_user'], $_CONFIG_DB['db_password'], $_CONFIG_DB['db_name'], $_CONFIG_DB['db_port']); $mySQLi = new mysqli($_CONFIG_DB['db_host'], $_CONFIG_DB['db_user'], $_CONFIG_DB['db_password'], $_CONFIG_DB['db_name'], $_CONFIG_DB['db_port']);
if($mySQLi->connect_errno) exit($mySQLi->connect_error); if ($mySQLi->connect_errno)
exit($mySQLi->connect_error);
$mySQLi->set_charset($_CONFIG_DB['db_charset']); $mySQLi->set_charset($_CONFIG_DB['db_charset']);
switch ($act) { switch ($act) {
case 'reg': case 'bind':
$player = [ $stmt1 = $mySQLi->prepare('insert into `player_connect_threeparty` (player_id, type, connect_id) values(?, `linuxdo`, ?)');
'username' => $userInfo['user_username'], $stmt1->bind_param('ssisiissis', input('account'), input('connect_id'));
'password' => $userInfo['user_api_key'], $stmt1->execute();
'email' => $userInfo['user_email'], $stmt1->close();
'reg_ip' => '127.0.0.1', $stmt2 = $mySQLi->prepare('select password from player where username=?');
'reg_time' => time(), $stmt2->bind_param('s', input('account'));
'reg_source' => 'linuxdo', $stmt2->execute();
] $result = $stmt2->get_result();
$stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type='linuxdo' and connect_id=?'); $data = $result->fetch_array();
$stmt->bind_param('s', $userInfo['user_username']); $result->free_result();
$stmt->execute(); $stmt2->close();
break; exit(json_encode($data));
default: default:
$code = $_GET['code']; $code = $_GET['code'];
@@ -52,7 +53,7 @@ switch ($act) {
$getUserRes = get_curl('https://connect.linux.do/api/user', 0, 0, 0, $header); $getUserRes = get_curl('https://connect.linux.do/api/user', 0, 0, 0, $header);
$getUserArr = json_decode($getUserRes, true); $getUserArr = json_decode($getUserRes, true);
$userInfo = [] $userInfo = [];
if (isset($getUserArr['id'])) { if (isset($getUserArr['id'])) {
// 保存每个用户数据项到 session 中 // 保存每个用户数据项到 session 中
@@ -69,11 +70,10 @@ switch ($act) {
$userInfo['user_silenced'] = $getUserArr['silenced']; $userInfo['user_silenced'] = $getUserArr['silenced'];
$userInfo['user_external_ids'] = $getUserArr['external_ids'] ?? 'null'; $userInfo['user_external_ids'] = $getUserArr['external_ids'] ?? 'null';
$userInfo['user_api_key'] = $getUserArr['api_key']; $userInfo['user_api_key'] = $getUserArr['api_key'];
} }
// 判断是否已经关联 // 判断是否已经关联
$stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type='linuxdo' and connect_id=?'); $stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type=`linuxdo` and connect_id=?');
$stmt->bind_param('s', $userInfo['user_username']); $stmt->bind_param('s', $userInfo['user_username']);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
@@ -140,25 +140,59 @@ function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobao
?> ?>
<html> <html>
<head> <head>
<style> <style>
html,body{ html,
body {
margin: 0; margin: 0;
} }
</style> </style>
</head> </head>
<body> <body>
<?php if (empty($row)) { ?> <?php if (empty($row)) { ?>
当前尚未有你的游戏账号,请选择<br> 当前尚未有你的游戏账号,请选择<br>
<a href="/linudo?act=bind&connect_id=<?=$userInfo['user_username']?>">Linuxdo账号绑定</a></br> <a onclick="registerDirect">Linuxdo账号绑定</a></br>
如果已有账号,请输入账号和密码 <input type="hidden" id="linuxdo" value="<?= $userInfo['user_username'] ?>">
<form action="/linudo?act=reg&connect_id=<?=$userInfo['user_username']?>"> <script>
<input type="text" id="account" placeholder="请输入账号" onKeyUp="value = value.replace(/[\W]/g, '')" autocomplete="off" disableautocomplete> function handleBind(account, linuxdo_account) {
return fetch("/linuxdo?act=bind&account=" + account + "&connect_id=" + linuxdo_account).then(res => {
const { password } = res.data
location.href = "/play?account=" + document.getElementById("linuxdo").value + "&token=" + password;
})
}
function processLogin(params = {}, connect_id) {
const formData = new FormData();
formData.append("serverId", "1");
Object.entries(params).forEach(([key, value]) => formData.append(key, value));
fetch("/api?act=reg", { method: "POST", body: formData }).then(res => {
if (res.code == '0') {
handleBind(res.data.account, connect_id)
} else {
showTips(res.msg, 6, 'error');
return
})
})
}
function registerDirect() {
const linuxdo_account = document.getElementById("linuxdo").value;
processLogin({ type: "1", account: linuxdo_account, password: "1", password2: "1" }, linuxdo_account)
}
function linkAccount() {
const account = document.getElementById("account").value;
const password = document.getElementById("password").value;
const linuxdo_account = document.getElementById("linuxdo").value;
processLogin({ type: "0", account, password, }, linuxdo_account)
}
</script>
如果已有账号,请输入账号和密码<br>
<input type="text" id="account" placeholder="请输入账号" onKeyUp="value = value.replace(/[\W]/g, '')" autocomplete="off"
disableautocomplete>
<input type="password" id="password" placeholder="请输入密码"> <input type="password" id="password" placeholder="请输入密码">
<button type="submit">绑定</button> <button type="submit" @click="linkAccount">绑定并登录</button>
</form>
<?php } ?> <?php } ?>
</body> </body>
</html> </html>