优化 linuxdo.php 文件中的 fetch 请求封装及错误处理
This commit is contained in:
15
linuxdo.php
15
linuxdo.php
@@ -112,8 +112,11 @@ if (isset($getTokenArr['access_token'])) {
|
||||
<link rel="stylesheet" href="static/css/login.css?v=1.1.6" />
|
||||
|
||||
<script>
|
||||
function request(url, params){
|
||||
return fetch(url, params).then(res => res.json())
|
||||
}
|
||||
function handleBind(account, linuxdo_account) {
|
||||
return fetch("/linuxdo?act=bind&account=" + account + "&connect_id=" + linuxdo_account).then(res => {
|
||||
return request("/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;
|
||||
})
|
||||
@@ -122,11 +125,11 @@ if (isset($getTokenArr['access_token'])) {
|
||||
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 => {
|
||||
request("/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');
|
||||
document.body.innerHTML = "授权失败"
|
||||
return
|
||||
}
|
||||
})
|
||||
@@ -151,11 +154,11 @@ if (isset($getTokenArr['access_token'])) {
|
||||
<?php if (!empty($getUserArr['username'])) { ?>
|
||||
<script>
|
||||
const connectId = document.getElementById('linuxdo').value
|
||||
fetch("/api?act=link&connect_id" + connectId).then(res => {
|
||||
request("/api?act=link&connect_id=" + connectId,{method:"POST"}).then(res => {
|
||||
if (res.code == '0') {
|
||||
location.href = "/play?account=" + res.data.account + "&token=" + res.data.password;
|
||||
} else {
|
||||
showTips(res.msg, 6, 'error');
|
||||
document.body.innerHTML = "授权失败"
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -179,4 +182,4 @@ if (isset($getTokenArr['access_token'])) {
|
||||
<?php } ?>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user