增加新项目
This commit is contained in:
78
项目合集/最新写真app/fontend/js/http/index.js
Normal file
78
项目合集/最新写真app/fontend/js/http/index.js
Normal file
@@ -0,0 +1,78 @@
|
||||
// 用js的function 来模拟class
|
||||
const http = () => {
|
||||
NProgress.configure({
|
||||
easing: 'ease', speed: 200,
|
||||
trickleSpeed: 150
|
||||
});
|
||||
|
||||
const init = () => {
|
||||
return new XMLHttpRequest();
|
||||
}
|
||||
|
||||
const get = (params) => {
|
||||
let http = init()
|
||||
NProgress.start();
|
||||
return new Promise((success, error) => {
|
||||
http.open("get", handleGetParams(params))
|
||||
http.send()
|
||||
readystatechange(http, success, error)
|
||||
})
|
||||
}
|
||||
|
||||
const post = (params) => {
|
||||
let http = init()
|
||||
NProgress.start();
|
||||
return new Promise((success, error) => {
|
||||
http.open("post", config.checkStage() + params.url)
|
||||
http.setRequestHeader("Content-Type", "application/json")
|
||||
http.send(JSON.stringify(params.data))
|
||||
readystatechange(http, success, error)
|
||||
})
|
||||
}
|
||||
|
||||
const readystatechange = (ajax, success, error) => {
|
||||
ajax.onreadystatechange = () => {
|
||||
// http status
|
||||
// ajax readyState
|
||||
// 接口提供的 标识当前你访问的接口操作的 具体信息
|
||||
if (ajax.readyState == 4) {
|
||||
if (ajax.status == 200) {
|
||||
NProgress.done();
|
||||
let result = JSON.parse(ajax.responseText)
|
||||
|
||||
switch (result.status) {
|
||||
case 200:
|
||||
success(result.data)
|
||||
break;
|
||||
|
||||
case 500:
|
||||
alert(result.message)
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
error(ajax)
|
||||
console.error(ajax)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理get请求参数并 拼接地址
|
||||
const handleGetParams = (params) => {
|
||||
let str = ""
|
||||
for (const key in params.data) {
|
||||
str += `&${key}=${params.data[key]}`
|
||||
}
|
||||
str = str.replace("&", "?")
|
||||
return config.checkStage() + params.url + str
|
||||
}
|
||||
|
||||
|
||||
return { get, post }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user