refactor(linuxdo): 重构 Linuxdo 登录功能
- 优化了数据库连接和查询逻辑 - 添加了账号绑定和登录的前端界面 - 实现了通过 API 进行账号注册和登录的后端逻辑 - 改进了代码格式和命名规范
This commit is contained in:
130
linuxdo.php
130
linuxdo.php
@@ -6,54 +6,55 @@ 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'];
|
||||||
|
|
||||||
$key = base64_encode($_LINUXDO_CONNECT['client_id'].':'.$_LINUXDO_CONNECT['client_secret']);
|
$key = base64_encode($_LINUXDO_CONNECT['client_id'] . ':' . $_LINUXDO_CONNECT['client_secret']);
|
||||||
|
|
||||||
$header = [
|
$header = [
|
||||||
'Authorization: Basic '.$key
|
'Authorization: Basic ' . $key
|
||||||
];
|
];
|
||||||
|
|
||||||
$post = http_build_query([
|
$post = http_build_query([
|
||||||
'grant_type' => 'authorization_code',
|
'grant_type' => 'authorization_code',
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'redirect_uri' => ''
|
'redirect_uri' => ''
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$getTokenRes = get_curl('https://connect.linux.do/oauth2/token', $post, 0, 0, $header);
|
$getTokenRes = get_curl('https://connect.linux.do/oauth2/token', $post, 0, 0, $header);
|
||||||
|
|
||||||
$getTokenArr = json_decode($getTokenRes, true);
|
$getTokenArr = json_decode($getTokenRes, true);
|
||||||
|
|
||||||
if (isset($getTokenArr['access_token'])) {
|
if (isset($getTokenArr['access_token'])) {
|
||||||
$access_token = $getTokenArr['access_token'];
|
$access_token = $getTokenArr['access_token'];
|
||||||
|
|
||||||
$header = [
|
$header = [
|
||||||
'Authorization: Bearer '.$access_token
|
'Authorization: Bearer ' . $access_token
|
||||||
];
|
];
|
||||||
|
|
||||||
$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 中
|
||||||
$userInfo['user_id'] = $getUserArr['id'];
|
$userInfo['user_id'] = $getUserArr['id'];
|
||||||
@@ -69,16 +70,15 @@ 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();
|
||||||
$row = $result->fetch_array();
|
$row = $result->fetch_array();
|
||||||
if(!empty($row)){
|
if (!empty($row)) {
|
||||||
$getPlayer = $mySQLi->prepare('select username,password from player where id=?');
|
$getPlayer = $mySQLi->prepare('select username,password from player where id=?');
|
||||||
$getPlayer->bind_param('s', $row['player_id']);
|
$getPlayer->bind_param('s', $row['player_id']);
|
||||||
$getPlayer->execute();
|
$getPlayer->execute();
|
||||||
@@ -94,7 +94,7 @@ switch ($act) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cURL 函数
|
// cURL 函数
|
||||||
function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobaody=0, $addheader=0)
|
function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0, $addheader = 0)
|
||||||
{
|
{
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
@@ -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,
|
||||||
margin:0;
|
body {
|
||||||
}
|
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) {
|
||||||
<input type="password" id="password" placeholder="请输入密码">
|
return fetch("/linuxdo?act=bind&account=" + account + "&connect_id=" + linuxdo_account).then(res => {
|
||||||
<button type="submit">绑定</button>
|
const { password } = res.data
|
||||||
</form>
|
location.href = "/play?account=" + document.getElementById("linuxdo").value + "&token=" + password;
|
||||||
<?php }?>
|
})
|
||||||
|
}
|
||||||
|
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" @click="linkAccount">绑定并登录</button>
|
||||||
|
<?php } ?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
|
||||||
|
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user