From a9184c643c6d288768a315ea36ca13c4162a734e Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Sat, 19 Apr 2025 12:58:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E7=BB=99=20request=20uplo?= =?UTF-8?q?adFile=20=E6=B7=BB=E5=8A=A0=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/http.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/utils/http.ts diff --git a/src/utils/http.ts b/src/utils/http.ts new file mode 100644 index 0000000..189e499 --- /dev/null +++ b/src/utils/http.ts @@ -0,0 +1,39 @@ +/** + * 给 request uploadFile 添加拦截器 + * + * 1. 基础地址 + * 2. 超时时间 + * 3. 请求头 + * 4. token + */ + +import { useMemberStore } from '@/stores/modules/member' + +const baseUrl = 'https://pcapi-xiaotuxian-front-devtest.itheima.net' + +// 拦截器配置 + +const interceptorOptions = { + // 拦截前触发 + invoke(options: UniApp.RequestOptions) { + // 1. baseUrl + if (!options.url.startsWith('http')) { + options.url = baseUrl + options.url + } + // 2. 超时时间, 默认60s + options.timeout = 10 * 1000 + // 3. 请求头 + options.header = { + ...options.header, + 'source-client': 'miniapp', + } + // 4. token + const token = useMemberStore().profile?.token + if (token) { + options.header.Authorization = token + } + }, +} + +uni.addInterceptor('request', interceptorOptions) +uni.addInterceptor('uploadFile', interceptorOptions)