|
@@ -17,10 +17,10 @@ export type RequestOptions = {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 响应数据类型定义
|
|
// 响应数据类型定义
|
|
|
-export type Response<R = any> = {
|
|
|
|
|
- code: number;
|
|
|
|
|
- message: string;
|
|
|
|
|
- data?: R;
|
|
|
|
|
|
|
+export type Response = {
|
|
|
|
|
+ code?: number;
|
|
|
|
|
+ message?: string;
|
|
|
|
|
+ data?: any;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 请求队列(用于等待token刷新后继续请求)
|
|
// 请求队列(用于等待token刷新后继续请求)
|
|
@@ -42,32 +42,32 @@ const isIgnoreToken = (url: string) => {
|
|
|
* @param options 请求参数
|
|
* @param options 请求参数
|
|
|
* @returns Promise<T>
|
|
* @returns Promise<T>
|
|
|
*/
|
|
*/
|
|
|
-export function request<R = any>(options: RequestOptions): Promise<Response<R>> {
|
|
|
|
|
|
|
+export function request(options: RequestOptions): Promise<any | null> {
|
|
|
let { url, method = "GET", data = {}, header = {}, timeout = 60000 } = options;
|
|
let { url, method = "GET", data = {}, header = {}, timeout = 60000 } = options;
|
|
|
|
|
|
|
|
const { user } = useStore();
|
|
const { user } = useStore();
|
|
|
|
|
|
|
|
- // 开发环境下打印请求信息
|
|
|
|
|
- if (isDev) {
|
|
|
|
|
- console.log(`[${method}] ${url}`);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// 拼接基础url
|
|
// 拼接基础url
|
|
|
if (!url.startsWith("http")) {
|
|
if (!url.startsWith("http")) {
|
|
|
url = config.baseUrl + url;
|
|
url = config.baseUrl + url;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+// 开发环境下打印请求信息
|
|
|
|
|
+ if (isDev) {
|
|
|
|
|
+ console.log(`[${method}] ${url}`);
|
|
|
|
|
+ }
|
|
|
// 获取当前token
|
|
// 获取当前token
|
|
|
let Authorization: string | null = user.token;
|
|
let Authorization: string | null = user.token;
|
|
|
|
|
|
|
|
- console.log(Authorization);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 如果是忽略token的接口,则不携带token
|
|
|
|
|
+ if (isIgnoreToken(url)) {
|
|
|
|
|
+ Authorization = null;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
// 发起请求的实际函数
|
|
// 发起请求的实际函数
|
|
|
const next = () => {
|
|
const next = () => {
|
|
|
- console.log(222);
|
|
|
|
|
-
|
|
|
|
|
uni.request({
|
|
uni.request({
|
|
|
url,
|
|
url,
|
|
|
method,
|
|
method,
|
|
@@ -80,8 +80,6 @@ export function request<R = any>(options: RequestOptions): Promise<Response<R>>
|
|
|
timeout,
|
|
timeout,
|
|
|
|
|
|
|
|
success(res) {
|
|
success(res) {
|
|
|
- console.log(res);
|
|
|
|
|
-
|
|
|
|
|
// 401 无权限
|
|
// 401 无权限
|
|
|
if (res.statusCode == 401) {
|
|
if (res.statusCode == 401) {
|
|
|
user.logout();
|
|
user.logout();
|
|
@@ -103,7 +101,7 @@ export function request<R = any>(options: RequestOptions): Promise<Response<R>>
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 200 正常响应
|
|
// 200 正常响应
|
|
|
- else if (res.statusCode == 0 || res.statusCode == 200) {
|
|
|
|
|
|
|
+ else if (res.statusCode == 200) {
|
|
|
if (res.data == null) {
|
|
if (res.data == null) {
|
|
|
resolve(null);
|
|
resolve(null);
|
|
|
} else if (!isObject(res.data as any)) {
|
|
} else if (!isObject(res.data as any)) {
|
|
@@ -134,89 +132,43 @@ export function request<R = any>(options: RequestOptions): Promise<Response<R>>
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
- console.log(2222);
|
|
|
|
|
|
|
+
|
|
|
next();
|
|
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();
|
|
|
|
|
- // });
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- // // 将当前请求加入队列,等待token刷新后再执行
|
|
|
|
|
- // new Promise((resolve) => {
|
|
|
|
|
- // requests.push((token: string) => {
|
|
|
|
|
- // // 重新设置token
|
|
|
|
|
- // Authorization = token;
|
|
|
|
|
- // next();
|
|
|
|
|
- // resolve(true);
|
|
|
|
|
- // });
|
|
|
|
|
- // });
|
|
|
|
|
- // // 此处return,等待token刷新
|
|
|
|
|
- // return;
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // token有效,直接发起请求
|
|
|
|
|
- // next();
|
|
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
-export function useGet<R = any, T = any>(url: string, params?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
|
|
|
|
+export function useGet(url: string, params?: any, config?: RequestOptions): Promise<Response> {
|
|
|
const options: RequestOptions = {
|
|
const options: RequestOptions = {
|
|
|
url,
|
|
url,
|
|
|
params,
|
|
params,
|
|
|
method: 'GET',
|
|
method: 'GET',
|
|
|
...config,
|
|
...config,
|
|
|
}
|
|
}
|
|
|
- return request<R>(options)
|
|
|
|
|
|
|
+ return request(options)
|
|
|
}
|
|
}
|
|
|
-export function usePost<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
|
|
|
|
+export function usePost(url: string, data?: any, config?: RequestOptions): Promise<Response> {
|
|
|
const options: RequestOptions = {
|
|
const options: RequestOptions = {
|
|
|
url,
|
|
url,
|
|
|
data,
|
|
data,
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
...config,
|
|
...config,
|
|
|
}
|
|
}
|
|
|
- return request<R>(options)
|
|
|
|
|
|
|
+ return request(options)
|
|
|
}
|
|
}
|
|
|
-export function usePut<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
|
|
|
|
+export function usePut(url: string, data?: any, config?: RequestOptions): Promise<Response> {
|
|
|
const options: RequestOptions = {
|
|
const options: RequestOptions = {
|
|
|
url,
|
|
url,
|
|
|
data,
|
|
data,
|
|
|
method: 'PUT',
|
|
method: 'PUT',
|
|
|
...config,
|
|
...config,
|
|
|
}
|
|
}
|
|
|
- return request<R>(options)
|
|
|
|
|
|
|
+ return request(options)
|
|
|
}
|
|
}
|
|
|
-export function useDelete<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
|
|
|
|
|
|
|
+export function useDelete(url: string, data?: any, config?: RequestOptions): Promise<Response> {
|
|
|
const options: RequestOptions = {
|
|
const options: RequestOptions = {
|
|
|
url,
|
|
url,
|
|
|
data,
|
|
data,
|
|
|
method: 'DELETE',
|
|
method: 'DELETE',
|
|
|
...config,
|
|
...config,
|
|
|
}
|
|
}
|
|
|
- return request<R>(options)
|
|
|
|
|
|
|
+ return request(options)
|
|
|
}
|
|
}
|