|
|
@@ -17,10 +17,10 @@ export type RequestOptions = {
|
|
|
};
|
|
|
|
|
|
// 响应数据类型定义
|
|
|
-export type Response = {
|
|
|
- code?: number;
|
|
|
- message?: string;
|
|
|
- data?: any;
|
|
|
+export type Response<R = any> = {
|
|
|
+ code: number;
|
|
|
+ message: string;
|
|
|
+ data?: R;
|
|
|
};
|
|
|
|
|
|
// 请求队列(用于等待token刷新后继续请求)
|
|
|
@@ -42,7 +42,7 @@ const isIgnoreToken = (url: string) => {
|
|
|
* @param options 请求参数
|
|
|
* @returns Promise<T>
|
|
|
*/
|
|
|
-export function request(options: RequestOptions): Promise<any | null> {
|
|
|
+export function request<R = any>(options: RequestOptions): Promise<Response<R>> {
|
|
|
let { url, method = "GET", data = {}, header = {}, timeout = 60000 } = options;
|
|
|
|
|
|
const { user } = useStore();
|
|
|
@@ -60,14 +60,14 @@ export function request(options: RequestOptions): Promise<any | null> {
|
|
|
// 获取当前token
|
|
|
let Authorization: string | null = user.token;
|
|
|
|
|
|
- // 如果是忽略token的接口,则不携带token
|
|
|
- if (isIgnoreToken(url)) {
|
|
|
- Authorization = null;
|
|
|
- }
|
|
|
+ console.log(Authorization);
|
|
|
+
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
// 发起请求的实际函数
|
|
|
const next = () => {
|
|
|
+ console.log(222);
|
|
|
+
|
|
|
uni.request({
|
|
|
url,
|
|
|
method,
|
|
|
@@ -80,6 +80,8 @@ export function request(options: RequestOptions): Promise<any | null> {
|
|
|
timeout,
|
|
|
|
|
|
success(res) {
|
|
|
+ console.log(res);
|
|
|
+
|
|
|
// 401 无权限
|
|
|
if (res.statusCode == 401) {
|
|
|
user.logout();
|
|
|
@@ -101,7 +103,7 @@ export function request(options: RequestOptions): Promise<any | null> {
|
|
|
}
|
|
|
|
|
|
// 200 正常响应
|
|
|
- else if (res.statusCode == 200) {
|
|
|
+ else if (res.statusCode == 0 || res.statusCode == 200) {
|
|
|
if (res.data == null) {
|
|
|
resolve(null);
|
|
|
} else if (!isObject(res.data as any)) {
|
|
|
@@ -132,51 +134,89 @@ export function request(options: RequestOptions): Promise<any | null> {
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
-
|
|
|
+ console.log(2222);
|
|
|
+ next();
|
|
|
// 非刷新token接口才进行token有效性校验
|
|
|
if (!options.url.includes("/refreshToken")) {
|
|
|
if (!isNull(Authorization)) {
|
|
|
// 判断token是否过期
|
|
|
- if (storage.isExpired("token")) {
|
|
|
- // 判断refreshToken是否过期
|
|
|
- if (storage.isExpired("refreshToken")) {
|
|
|
- // 刷新token也过期,直接退出登录
|
|
|
- user.logout();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 如果当前没有在刷新token,则发起刷新
|
|
|
- if (!isRefreshing) {
|
|
|
- isRefreshing = true;
|
|
|
- user.refreshToken()
|
|
|
- .then((token) => {
|
|
|
- // 刷新成功后,执行队列中的请求
|
|
|
- requests.forEach((cb) => cb(token));
|
|
|
- requests = [];
|
|
|
- isRefreshing = false;
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- reject(err);
|
|
|
- user.logout();
|
|
|
- });
|
|
|
- }
|
|
|
+ // if (storage.isExpired("token")) {
|
|
|
+ // // 判断refreshToken是否过期
|
|
|
+ // if (storage.isExpired("refreshToken")) {
|
|
|
+ // // 刷新token也过期,直接退出登录
|
|
|
+ // user.logout();
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 如果当前没有在刷新token,则发起刷新
|
|
|
+ // if (!isRefreshing) {
|
|
|
+ // isRefreshing = true;
|
|
|
+ // user.refreshToken()
|
|
|
+ // .then((token) => {
|
|
|
+ // // 刷新成功后,执行队列中的请求
|
|
|
+ // requests.forEach((cb) => cb(token));
|
|
|
+ // requests = [];
|
|
|
+ // isRefreshing = false;
|
|
|
+ // })
|
|
|
+ // .catch((err) => {
|
|
|
+ // reject(err);
|
|
|
+ // user.logout();
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 将当前请求加入队列,等待token刷新后再执行
|
|
|
+ // new Promise((resolve) => {
|
|
|
+ // requests.push((token: string) => {
|
|
|
+ // // 重新设置token
|
|
|
+ // Authorization = token;
|
|
|
+ // next();
|
|
|
+ // resolve(true);
|
|
|
+ // });
|
|
|
+ // });
|
|
|
+ // // 此处return,等待token刷新
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
|
|
|
- // 将当前请求加入队列,等待token刷新后再执行
|
|
|
- new Promise((resolve) => {
|
|
|
- requests.push((token: string) => {
|
|
|
- // 重新设置token
|
|
|
- Authorization = token;
|
|
|
- next();
|
|
|
- resolve(true);
|
|
|
- });
|
|
|
- });
|
|
|
- // 此处return,等待token刷新
|
|
|
- return;
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// token有效,直接发起请求
|
|
|
- next();
|
|
|
+ // next();
|
|
|
});
|
|
|
}
|
|
|
+export function useGet<R = any, T = any>(url: string, params?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
+ const options: RequestOptions = {
|
|
|
+ url,
|
|
|
+ params,
|
|
|
+ method: 'GET',
|
|
|
+ ...config,
|
|
|
+ }
|
|
|
+ return request<R>(options)
|
|
|
+}
|
|
|
+export function usePost<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
+ const options: RequestOptions = {
|
|
|
+ url,
|
|
|
+ data,
|
|
|
+ method: 'POST',
|
|
|
+ ...config,
|
|
|
+ }
|
|
|
+ return request<R>(options)
|
|
|
+}
|
|
|
+export function usePut<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
+ const options: RequestOptions = {
|
|
|
+ url,
|
|
|
+ data,
|
|
|
+ method: 'PUT',
|
|
|
+ ...config,
|
|
|
+ }
|
|
|
+ return request<R>(options)
|
|
|
+}
|
|
|
+export function useDelete<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
+ const options: RequestOptions = {
|
|
|
+ url,
|
|
|
+ data,
|
|
|
+ method: 'DELETE',
|
|
|
+ ...config,
|
|
|
+ }
|
|
|
+ return request<R>(options)
|
|
|
+}
|