88 lines
1.5 KiB
JavaScript
88 lines
1.5 KiB
JavaScript
import { goto } from "./untool";
|
|
import { showToast, showLoading, hideLoading } from "./untool"
|
|
|
|
// 请求接口
|
|
function dealRequest(def, url, param) {
|
|
if(param && param._loading) {
|
|
showLoading()
|
|
}
|
|
const promise = new Promise(function(resolve, reject) {
|
|
def.then(function(response) {
|
|
let {
|
|
data,
|
|
cookies,
|
|
statusCode,
|
|
header,
|
|
errMsg
|
|
} = response
|
|
if (statusCode != 200) {
|
|
showToast(`网络错误`)
|
|
reject({
|
|
response
|
|
})
|
|
}
|
|
handleResponse({
|
|
response,
|
|
data,
|
|
resolve,
|
|
reject,
|
|
url,
|
|
param
|
|
});
|
|
}, function(response) {
|
|
console.log('request url==========1', url)
|
|
console.log('request param==========1', param)
|
|
console.log('response ===========1', response);
|
|
showToast(`网络错误`)
|
|
reject({
|
|
response
|
|
})
|
|
})
|
|
})
|
|
|
|
// 统一提示 errmsg
|
|
promise.then(function(res) {
|
|
if(param && param._loading) {
|
|
hideLoading()
|
|
}
|
|
}, function(res) {
|
|
hideLoading()
|
|
})
|
|
return promise;
|
|
}
|
|
|
|
// 处理响应
|
|
function handleResponse({
|
|
data,
|
|
resolve,
|
|
reject,
|
|
url,
|
|
param
|
|
}) {
|
|
data = data || {}
|
|
const { msg, code } = data
|
|
if ((code == 200)) {
|
|
resolve(data)
|
|
} else {
|
|
if ( code == 401) { // 统一判断需要认证, 并作页面跳转
|
|
showToast(msg)
|
|
setTimeout(() => {
|
|
goto("login/login", 2)
|
|
}, 3500)
|
|
return reject({
|
|
code: {}
|
|
});
|
|
}
|
|
setTimeout(() => {
|
|
showToast(`网络错误`)
|
|
}, 0)
|
|
reject({
|
|
code: {}
|
|
});
|
|
}
|
|
}
|
|
|
|
export {
|
|
dealRequest,
|
|
handleResponse
|
|
} |