2024-07-08 06:25:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* 通用uni-app网络请求
|
|
|
|
|
* 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
|
|
|
|
|
*/
|
|
|
|
|
export default {
|
|
|
|
|
config: {
|
|
|
|
|
|
2024-07-10 01:25:59 +00:00
|
|
|
|
baseUrl: 'https://erpapi.fontree.cn',
|
2024-07-08 06:25:32 +00:00
|
|
|
|
header: {
|
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
|
|
|
// 'Content-Type':'application/x-www-form-urlencoded'
|
|
|
|
|
},
|
|
|
|
|
data: {},
|
|
|
|
|
method: "GET",
|
|
|
|
|
dataType: "json" /* 如设为json,会对返回的数据做一次 JSON.parse */,
|
|
|
|
|
responseType: "text",
|
|
|
|
|
success() {},
|
|
|
|
|
fail() {},
|
|
|
|
|
complete() {},
|
|
|
|
|
},
|
|
|
|
|
interceptor: {
|
|
|
|
|
request: null,
|
|
|
|
|
response: null,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
request(options) {
|
|
|
|
|
if (!options) {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
options.baseUrl = options.baseUrl || this.config.baseUrl;
|
|
|
|
|
options.dataType = options.dataType || this.config.dataType;
|
|
|
|
|
options.url = options.baseUrl + options.url;
|
|
|
|
|
options.data = options.data || {};
|
|
|
|
|
options.method = options.method || this.config.method;
|
|
|
|
|
//TODO 加密数据
|
|
|
|
|
options.header = options.header || this.config.header;
|
|
|
|
|
//TODO 数据签名
|
|
|
|
|
let _token = {
|
|
|
|
|
Authorization: uni.getStorageSync("repari-token") || "undefined",
|
|
|
|
|
};
|
|
|
|
|
options.header = Object.assign({}, options.header, _token);
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let _config = null;
|
|
|
|
|
|
|
|
|
|
options.complete = async (response) => {
|
|
|
|
|
let statusCode = response.statusCode;
|
|
|
|
|
response.config = _config;
|
|
|
|
|
if (this.interceptor.response) {
|
|
|
|
|
let newResponse = this.interceptor.response(response);
|
|
|
|
|
if (newResponse) {
|
|
|
|
|
response = newResponse;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 01:43:02 +00:00
|
|
|
|
if (response.data.code === 401) {
|
2024-07-08 06:25:32 +00:00
|
|
|
|
// 若token过期则尝试获取RefreshToken重新获取token
|
|
|
|
|
const newToken = await this.getRefreshToken();
|
|
|
|
|
if (newToken) {
|
|
|
|
|
// 更新token
|
|
|
|
|
options.header.Authorization = newToken;
|
|
|
|
|
// 重新发起请求
|
|
|
|
|
uni.request(options);
|
|
|
|
|
} else {
|
|
|
|
|
// 若获取RefreshToken失败,则跳转到登录页
|
|
|
|
|
uni.removeStorageSync("repari-token");
|
|
|
|
|
uni.removeStorageSync("repari-user-info");
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
url: "/pages/login/index",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 统一的响应日志记录
|
|
|
|
|
_reslog(response);
|
|
|
|
|
if (statusCode === 200) {
|
|
|
|
|
//成功
|
|
|
|
|
resolve(response.data);
|
|
|
|
|
} else {
|
|
|
|
|
reject(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_config = Object.assign({}, this.config, options);
|
|
|
|
|
_config.requestId = new Date().getTime();
|
|
|
|
|
|
|
|
|
|
if (this.interceptor.request) {
|
|
|
|
|
this.interceptor.request(_config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uni.request(_config);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
isRefreshing: false,
|
|
|
|
|
refreshSubscribers: [],
|
|
|
|
|
async getRefreshToken() {
|
|
|
|
|
let res
|
|
|
|
|
if (!this.isRefreshing) {
|
|
|
|
|
this.isRefreshing = true;
|
|
|
|
|
const refreshToken = uni.getStorageSync("repari-refresh-token");
|
|
|
|
|
if (refreshToken) {
|
|
|
|
|
try {
|
|
|
|
|
const data = { refreshToken };
|
|
|
|
|
res = await this.post("/user/refresh/token", data);
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
uni.setStorageSync("repari-token", res.data.Token);
|
|
|
|
|
uni.setStorageSync("repari-user-info", res.data.AccountInfo);
|
|
|
|
|
uni.setStorageSync("repari-refresh-token", res.data.RefreshToken);
|
|
|
|
|
// 返回新的token
|
|
|
|
|
return res.data.Token;
|
|
|
|
|
} else {
|
|
|
|
|
// 获取RefreshToken失败
|
|
|
|
|
uni.removeStorageSync("repari-token");
|
|
|
|
|
uni.removeStorageSync("repari-user-info");
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
url: "/pages/login/index",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
} finally {
|
|
|
|
|
this.isRefreshing = false;
|
|
|
|
|
this.refreshSubscribers.forEach((cb) => cb(res.data.Token));
|
|
|
|
|
// 重置队列
|
|
|
|
|
this.refreshSubscribers = [];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 若没有RefreshToken,则跳转到登录页
|
|
|
|
|
webUni.postMessage({
|
|
|
|
|
data: {
|
|
|
|
|
params: 401,
|
|
|
|
|
action: 'login'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/* uni.redirectTo({
|
|
|
|
|
url: "/pages/login/index",
|
|
|
|
|
}); */
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 若正在获取RefreshToken,则返回一个Promise,等待获取到新的token后再继续之前的请求
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.refreshSubscribers.push((token) => {
|
|
|
|
|
resolve(token);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get(url, data, options) {
|
|
|
|
|
if (!options) {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
options.url = url;
|
|
|
|
|
options.data = data;
|
|
|
|
|
options.method = "GET";
|
|
|
|
|
return this.request(options);
|
|
|
|
|
},
|
|
|
|
|
post(url, data, options, header) {
|
|
|
|
|
if (!options) {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
options.url = url;
|
|
|
|
|
options.data = data;
|
|
|
|
|
options.header = header;
|
|
|
|
|
options.method = "POST";
|
|
|
|
|
return this.request(options);
|
|
|
|
|
},
|
|
|
|
|
put(url, data, options) {
|
|
|
|
|
if (!options) {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
options.url = url;
|
|
|
|
|
options.data = data;
|
|
|
|
|
options.method = "PUT";
|
|
|
|
|
return this.request(options);
|
|
|
|
|
},
|
|
|
|
|
delete(url, data, options) {
|
|
|
|
|
if (!options) {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
options.url = url;
|
|
|
|
|
options.data = data;
|
|
|
|
|
options.method = "DELETE";
|
|
|
|
|
return this.request(options);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 响应接口日志记录
|
|
|
|
|
*/
|
|
|
|
|
function _reslog(res) {
|
|
|
|
|
let _statusCode = res.data.status;
|
|
|
|
|
//TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
|
|
|
|
|
switch (_statusCode) {
|
|
|
|
|
case 200:
|
|
|
|
|
break;
|
|
|
|
|
case 401:
|
|
|
|
|
break;
|
|
|
|
|
case 404:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|