first commit

This commit is contained in:
2024-12-10 12:20:05 +08:00
commit f866087c7e
37 changed files with 2080 additions and 0 deletions

49
js/http/index.js Normal file
View File

@@ -0,0 +1,49 @@
class Ajax {
xhr = new XMLHttpRequest()
get(option) {
$("body").mLoading("show")
return new Promise((succ, err) => {
// jquery 是通过 callback 来对异步ajax代码进行封装的
$.ajax({
type: "GET",
// http://192.168.31.101:9940/v1/api + /home/banner
url: config.checkUrl() + option.url,
data: option.data,
success: res => {
$("body").mLoading("hide")
succ( this.stateChange(res) )
},
error: msg => {
err(msg)
}
});
})
}
post(option) {
return new Promise((success, error) => {
})
}
// 对业务状态码进行判断
stateChange(res) {
switch (res.status) {
case 200:
return res.data
break;
case 404:
alert(res.message)
err(this.xhr)
break;
default:
break;
}
}
}

57
js/http/index.js-old Normal file
View File

@@ -0,0 +1,57 @@
class Ajax {
xhr = new XMLHttpRequest()
get(option) {
return new Promise((success, error) => {
var params = ""
for (const key in option.data) {
params += `&${key}=${option.data[key]}`
}
params = params.replace("&", "?")
// option.url == /home/hot?id=1
this.xhr.open("GET", `${config.checkUrl()}${option.url}${params}`)
this.xhr.send()
this.stateChange(success, error)
})
}
post(option) {
return new Promise((success, error) => {
this.xhr.open("POST", option.url)
this.xhr.setRequestHeader("Content-Type", "application/json")
// post 参数在请求体(body)中发送
this.xhr.send(JSON.stringify(option.data))
this.stateChange(success, error)
})
}
stateChange(suc, err) {
this.xhr.onreadystatechange = () => {
if (this.xhr.readyState == 4) {
if (this.xhr.status == 200) {
let res = JSON.parse(this.xhr.response)
switch (res.status) {
case 200:
suc(res.data)
break;
case 404:
alert(res.message)
err(this.xhr)
break;
default:
break;
}
suc()
} else {
err(this.xhr)
}
}
}
}
}