2024-04-27 12:44:15 +00:00
|
|
|
import { goto } from "./untool";
|
2024-04-28 09:50:39 +00:00
|
|
|
import { showToast, showLoading, hideLoading } from "./untool"
|
2024-05-10 10:30:41 +00:00
|
|
|
import { setSessionXToken, getSessionXToken } from "@/session"
|
|
|
|
|
|
|
|
|
|
const getCommonReqData = ()=> {
|
|
|
|
|
const xToken = getSessionXToken()
|
|
|
|
|
return {
|
|
|
|
|
header: {
|
|
|
|
|
"X-Token": xToken,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 16:30:33 +00:00
|
|
|
|
|
|
|
|
// 请求接口
|
|
|
|
|
function dealRequest(def, url, param) {
|
2024-04-28 09:50:39 +00:00
|
|
|
if(param && param._loading) {
|
|
|
|
|
showLoading()
|
|
|
|
|
}
|
2024-02-21 11:52:20 +00:00
|
|
|
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,
|
2024-05-10 11:14:35 +00:00
|
|
|
header,
|
2024-02-21 11:52:20 +00:00
|
|
|
param
|
|
|
|
|
});
|
|
|
|
|
}, function(response) {
|
|
|
|
|
console.log('request url==========1', url)
|
|
|
|
|
console.log('request param==========1', param)
|
|
|
|
|
console.log('response ===========1', response);
|
|
|
|
|
showToast(`网络错误`)
|
|
|
|
|
reject({
|
|
|
|
|
response
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-12-18 16:30:33 +00:00
|
|
|
|
2024-02-21 11:52:20 +00:00
|
|
|
// 统一提示 errmsg
|
2024-04-28 10:38:36 +00:00
|
|
|
promise.then(function(res) {
|
|
|
|
|
if(param && param._loading) {
|
|
|
|
|
hideLoading()
|
|
|
|
|
}
|
|
|
|
|
}, function(res) {
|
2024-04-28 09:50:39 +00:00
|
|
|
hideLoading()
|
2024-02-21 11:52:20 +00:00
|
|
|
})
|
|
|
|
|
return promise;
|
2023-12-18 16:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理响应
|
2024-02-21 11:52:20 +00:00
|
|
|
function handleResponse({
|
|
|
|
|
data,
|
2024-05-10 11:14:35 +00:00
|
|
|
header,
|
2024-02-21 11:52:20 +00:00
|
|
|
resolve,
|
|
|
|
|
reject,
|
|
|
|
|
url,
|
|
|
|
|
param
|
|
|
|
|
}) {
|
2024-05-10 11:14:35 +00:00
|
|
|
// 图片类型数据特殊处理
|
|
|
|
|
if(header['content-type']?.indexOf?.("image/") > -1) {
|
|
|
|
|
uni.downloadFile({
|
|
|
|
|
url: data,
|
|
|
|
|
success: (res) => {
|
|
|
|
|
debugger
|
|
|
|
|
},
|
|
|
|
|
fail: (err)=>{
|
|
|
|
|
debugger
|
|
|
|
|
console.log("downloadFile:err", err)
|
|
|
|
|
reject(err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
debugger
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:52:20 +00:00
|
|
|
data = data || {}
|
|
|
|
|
const { msg, code } = data
|
|
|
|
|
if ((code == 200)) {
|
|
|
|
|
resolve(data)
|
|
|
|
|
} else {
|
2024-03-04 16:19:56 +00:00
|
|
|
if ( code == 401) { // 统一判断需要认证, 并作页面跳转
|
2024-05-03 17:17:45 +00:00
|
|
|
setSessionXToken()
|
2024-02-21 11:52:20 +00:00
|
|
|
showToast(msg)
|
2024-05-03 17:17:45 +00:00
|
|
|
goto("login/login", 2)
|
2024-02-21 11:52:20 +00:00
|
|
|
return reject({
|
|
|
|
|
code: {}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setTimeout(() => {
|
2024-05-06 07:35:01 +00:00
|
|
|
showToast(msg || `网络错误`)
|
2024-02-21 11:52:20 +00:00
|
|
|
}, 0)
|
|
|
|
|
reject({
|
|
|
|
|
code: {}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-18 16:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export {
|
2024-05-10 10:30:41 +00:00
|
|
|
getCommonReqData,
|
2024-02-21 11:52:20 +00:00
|
|
|
dealRequest,
|
|
|
|
|
handleResponse
|
|
|
|
|
}
|