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