first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
(function (){
class Ajax {
constructor() {
this.ajax = new XMLHttpRequest();
}
async get(option) {
var params = ""
for(var key in option.data) {
params+= `&${key}=${option.data[key]}`
}
params = params.replace("&", "?")
option.url = this.checkBaseUrl() + option.url + params
this.ajax.open("GET", option.url)
this.ajax.send()
return await this.readystatechange();
}
post(option) {
this.ajax.open("POST", this.checkBaseUrl() + option.url)
this.ajax.setRequestHeader("Content-Type","application/json")
this.ajax.send(JSON.stringify(option.data))
return this.readystatechange()
}
readystatechange() {
return new Promise( (success,error) => {
this.ajax.onreadystatechange = () => {
if (this.ajax.readyState == 4) {
if (this.ajax.status == 200) {
// 如果后端给的数据是string,才进行 JSON.parse
var result = typeof this.ajax.responseText == "string"
? JSON.parse(this.ajax.responseText)
: this.ajax.responseText;
switch (result.status) {
case 200:
success(result.data)
break;
case 500:
error(result.msg)
break;
default:
}
} else {
error("请求出错")
}
}
}
})
}
checkBaseUrl() {
switch (Config.DevelopmentPhase) {
case "dev":
return Config.DevUrl;
break;
case "test":
return Config.TestUrl;
break;
case "prod":
return Config.ProdUrl;
break
}
}
}
window.$get = new Ajax().get.bind(new Ajax());
window.$post = new Ajax().post.bind(new Ajax());
})()