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');
$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']);
switch ($act) {
case 'reg':
$player = [
'username' => $userInfo['user_username'],
'password' => $userInfo['user_api_key'],
'email' => $userInfo['user_email'],
'reg_ip' => '127.0.0.1',
'reg_time' => time(),
'reg_source' => 'linuxdo',
]
$stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type='linuxdo' and connect_id=?');
$stmt->bind_param('s', $userInfo['user_username']);
$stmt->execute();
break;
case 'bind':
$stmt1 = $mySQLi->prepare('insert into `player_connect_threeparty` (player_id, type, connect_id) values(?, `linuxdo`, ?)');
$stmt1->bind_param('ssisiissis', input('account'), input('connect_id'));
$stmt1->execute();
$stmt1->close();
$stmt2 = $mySQLi->prepare('select password from player where username=?');
$stmt2->bind_param('s', input('account'));
$stmt2->execute();
$result = $stmt2->get_result();
$data = $result->fetch_array();
$result->free_result();
$stmt2->close();
exit(json_encode($data));
default:
$code = $_GET['code'];
@@ -52,7 +53,7 @@ switch ($act) {
$getUserRes = get_curl('https://connect.linux.do/api/user', 0, 0, 0, $header);
$getUserArr = json_decode($getUserRes, true);
$userInfo = []
$userInfo = [];
if (isset($getUserArr['id'])) {
// 保存每个用户数据项到 session 中
@@ -69,11 +70,10 @@ switch ($act) {
$userInfo['user_silenced'] = $getUserArr['silenced'];
$userInfo['user_external_ids'] = $getUserArr['external_ids'] ?? 'null';
$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->execute();
$result = $stmt->get_result();
@@ -140,25 +140,59 @@ function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobao
?>
<html>
<head>
<style>
html,body{
html,
body {
margin: 0;
}
</style>
</head>
<body>
<?php if (empty($row)) { ?>
当前尚未有你的游戏账号,请选择<br>
<a href="/linudo?act=bind&connect_id=<?=$userInfo['user_username']?>">Linuxdo账号绑定</a></br>
如果已有账号,请输入账号和密码
<form action="/linudo?act=reg&connect_id=<?=$userInfo['user_username']?>">
<input type="text" id="account" placeholder="请输入账号" onKeyUp="value = value.replace(/[\W]/g, '')" autocomplete="off" disableautocomplete>
<a onclick="registerDirect">Linuxdo账号绑定</a></br>
<input type="hidden" id="linuxdo" value="<?= $userInfo['user_username'] ?>">
<script>
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="请输入密码">
<button type="submit">绑定</button>
</form>
<button type="submit" @click="linkAccount">绑定并登录</button>
<?php } ?>
</body>
</html>