首页页面完善
This commit is contained in:
1
src/utils/assets.ts
Normal file
1
src/utils/assets.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const assetsUrl ='/static/images/'
|
@@ -1,5 +1,5 @@
|
||||
// 引入配置
|
||||
import type { HttpRequestConfig } from 'uview-plus/libs/luch-request/index';
|
||||
import type { HttpRequestConfig } from 'uview-plus/libs/luch-request';
|
||||
import { requestInterceptors, responseInterceptors } from './interceptors';
|
||||
import type { IResponse } from './type';
|
||||
|
||||
@@ -14,11 +14,15 @@ export function setupRequest() {
|
||||
responseInterceptors();
|
||||
}
|
||||
|
||||
export function request<T = any>(config: HttpRequestConfig): Promise<T> {
|
||||
export function request<T = any>(config: HttpRequestConfig): Promise<T | any> {
|
||||
return new Promise((resolve) => {
|
||||
uni.$u.http.request(config).then((res: IResponse) => {
|
||||
const { result } = res;
|
||||
resolve(result as T);
|
||||
if(res instanceof ArrayBuffer) {
|
||||
resolve(res);
|
||||
} else {
|
||||
const { result } = res;
|
||||
resolve(result as T);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import type {
|
||||
HttpError,
|
||||
HttpRequestConfig,
|
||||
HttpResponse,
|
||||
} from 'uview-plus/libs/luch-request/index';
|
||||
HttpResponse
|
||||
} from 'uview-plus/libs/luch-request';
|
||||
import { showMessage } from './status';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import useUserStore from '@/store/modules/user';
|
||||
@@ -24,16 +24,17 @@ function requestInterceptors() {
|
||||
config.data = config.data || {};
|
||||
// token设置
|
||||
const token = getToken();
|
||||
if (token && config.header) {
|
||||
if(token && config.header) {
|
||||
config.header.token = token;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(
|
||||
config: any, // 可使用async await 做异步操作
|
||||
) => Promise.reject(config),
|
||||
config: any // 可使用async await 做异步操作
|
||||
) => Promise.reject(config)
|
||||
);
|
||||
}
|
||||
|
||||
function responseInterceptors() {
|
||||
/**
|
||||
* 响应拦截
|
||||
@@ -48,15 +49,19 @@ function responseInterceptors() {
|
||||
// 自定义参数
|
||||
const custom = config?.custom;
|
||||
|
||||
if(config.responseType === 'arraybuffer') {
|
||||
return data;
|
||||
}
|
||||
|
||||
// 请求成功则返回结果
|
||||
if (data.code === 200) {
|
||||
if(data.code === 200) {
|
||||
return data || {};
|
||||
}
|
||||
|
||||
// 登录状态失效,重新登录
|
||||
if (data.code === 401) {
|
||||
if(data.code === 401) {
|
||||
// 是否在获取token中,防止重复获取
|
||||
if (!isRefreshing) {
|
||||
if(!isRefreshing) {
|
||||
// 修改登录状态为true
|
||||
isRefreshing = true;
|
||||
await useUserStore().authLogin();
|
||||
@@ -78,26 +83,27 @@ function responseInterceptors() {
|
||||
}
|
||||
|
||||
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
||||
if (custom?.toast !== false) {
|
||||
if(custom?.toast !== false) {
|
||||
uni.$u.toast(data.message);
|
||||
}
|
||||
|
||||
// 如果需要catch返回,则进行reject
|
||||
if (custom?.catch) {
|
||||
if(custom?.catch) {
|
||||
return Promise.reject(data);
|
||||
} else {
|
||||
// 否则返回一个pending中的promise
|
||||
return new Promise(() => {});
|
||||
return new Promise(() => {
|
||||
});
|
||||
}
|
||||
},
|
||||
(response: HttpError) => {
|
||||
if (response.statusCode) {
|
||||
if(response.statusCode) {
|
||||
// 请求已发出,但是不在2xx的范围
|
||||
showMessage(response.statusCode);
|
||||
return Promise.reject(response.data);
|
||||
}
|
||||
showMessage('网络连接异常,请稍后再试!');
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user