You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import functions from "@/utils/functions.js";
|
|
|
|
import {
|
|
|
|
getToken,
|
|
|
|
getRefreshToken,
|
|
|
|
setToken
|
|
|
|
} from "@/utils/auth.js";
|
|
|
|
|
|
|
|
const service = (params) => {
|
|
|
|
|
|
|
|
return new Promise((relove, reject) => {
|
|
|
|
|
|
|
|
let result = doRequest(params);
|
|
|
|
|
|
|
|
result.then(res => {
|
|
|
|
if (res) {
|
|
|
|
relove(res);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//请求接口
|
|
|
|
async function doRequest(params) {
|
|
|
|
|
|
|
|
functions.showWaiting();
|
|
|
|
|
|
|
|
let header = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"token": getToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.header) {
|
|
|
|
header = {
|
|
|
|
...header,
|
|
|
|
...params.header
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const [err, success] = await uni.request({
|
|
|
|
url: params.url,
|
|
|
|
data: params.data,
|
|
|
|
method: params.method,
|
|
|
|
dataType: "json",
|
|
|
|
header: header
|
|
|
|
});
|
|
|
|
|
|
|
|
functions.closeWaiting();
|
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
|
|
|
|
if (success.statusCode == 200) {
|
|
|
|
|
|
|
|
let data = success.data;
|
|
|
|
|
|
|
|
if (data.code == 200) {
|
|
|
|
return data.data;
|
|
|
|
} else if (data.code == 203) {
|
|
|
|
return refreshTokenRequest(params);
|
|
|
|
} else if (data.code == 205) {
|
|
|
|
functions.logout();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
functions.error(data.msg);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//请求失败
|
|
|
|
functions.error("服务器请求失败");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//请求失败
|
|
|
|
functions.error("服务器请求失败");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//刷新token
|
|
|
|
async function refreshTokenRequest(params) {
|
|
|
|
|
|
|
|
if (!getRefreshToken()) {
|
|
|
|
functions.logout();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let apiUrl = `${functions.getDomain()}/api/cy-check-sure`;
|
|
|
|
|
|
|
|
const result = doRequest({
|
|
|
|
url: apiUrl,
|
|
|
|
method: "post",
|
|
|
|
data: {},
|
|
|
|
header: {
|
|
|
|
"Refresh-Token": getRefreshToken()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const newToken = await result;
|
|
|
|
|
|
|
|
setToken(newToken.token);
|
|
|
|
|
|
|
|
return doRequest(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default service;
|