2023-12-23 11:39:46 +08:00
|
|
|
|
/* eslint-disable no-param-reassign */
|
2024-02-01 15:21:36 +08:00
|
|
|
|
import qs from 'qs'
|
2023-12-23 11:39:46 +08:00
|
|
|
|
import { useUserStore } from '@/store'
|
|
|
|
|
|
2024-02-01 15:21:36 +08:00
|
|
|
|
type CustomRequestOptions = UniApp.RequestOptions & { query?: Record<string, any> }
|
|
|
|
|
|
2023-12-23 11:39:46 +08:00
|
|
|
|
// 请求基地址
|
2023-12-23 12:10:45 +08:00
|
|
|
|
const baseURL = import.meta.env.VITE_SERVER_BASEURL
|
2024-01-22 10:53:28 +08:00
|
|
|
|
// console.log(import.meta.env)
|
2023-12-23 11:39:46 +08:00
|
|
|
|
|
|
|
|
|
// 拦截器配置
|
|
|
|
|
const httpInterceptor = {
|
|
|
|
|
// 拦截前触发
|
2024-02-01 15:21:36 +08:00
|
|
|
|
invoke(options: CustomRequestOptions) {
|
|
|
|
|
// 接口请求支持通过 query 参数配置 queryString
|
|
|
|
|
if (options.query) {
|
|
|
|
|
const queryStr = qs.stringify(options.query)
|
|
|
|
|
if (options.url.includes('?')) {
|
|
|
|
|
options.url += `&${queryStr}`
|
|
|
|
|
} else {
|
|
|
|
|
options.url += `?${queryStr}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 11:39:46 +08:00
|
|
|
|
// 1. 非 http 开头需拼接地址
|
|
|
|
|
if (!options.url.startsWith('http')) {
|
|
|
|
|
options.url = baseURL + options.url
|
|
|
|
|
}
|
|
|
|
|
// 2. 请求超时
|
|
|
|
|
options.timeout = 10000 // 10s
|
|
|
|
|
// 3. 添加小程序端请求头标识
|
|
|
|
|
options.header = {
|
|
|
|
|
platform: 'mp-weixin', // 可选值与 uniapp 定义的平台一致,告诉后台来源
|
|
|
|
|
...options.header,
|
|
|
|
|
}
|
|
|
|
|
// 4. 添加 token 请求头标识
|
2024-01-19 17:28:04 +08:00
|
|
|
|
const userStore = useUserStore()
|
2024-02-04 16:00:27 +08:00
|
|
|
|
const { token } = userStore.userInfo as unknown as IUserInfo
|
2023-12-23 11:39:46 +08:00
|
|
|
|
if (token) {
|
|
|
|
|
options.header.Authorization = `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拦截 request 请求
|
|
|
|
|
uni.addInterceptor('request', httpInterceptor)
|
|
|
|
|
// 拦截 uploadFile 文件上传
|
|
|
|
|
uni.addInterceptor('uploadFile', httpInterceptor)
|
|
|
|
|
|
2024-02-01 15:21:36 +08:00
|
|
|
|
export const http = <T>(options: CustomRequestOptions) => {
|
2023-12-23 11:39:46 +08:00
|
|
|
|
// 1. 返回 Promise 对象
|
2024-02-01 19:51:58 +08:00
|
|
|
|
return new Promise<IResData<T>>((resolve, reject) => {
|
2023-12-23 11:39:46 +08:00
|
|
|
|
uni.request({
|
|
|
|
|
...options,
|
2024-02-01 15:21:36 +08:00
|
|
|
|
dataType: 'json',
|
2024-03-07 19:09:34 +08:00
|
|
|
|
// #ifndef MP-WEIXIN
|
2024-02-01 15:21:36 +08:00
|
|
|
|
responseType: 'json',
|
2024-03-07 19:09:34 +08:00
|
|
|
|
// #endif
|
2023-12-23 11:39:46 +08:00
|
|
|
|
// 响应成功
|
|
|
|
|
success(res) {
|
|
|
|
|
// 状态码 2xx,参考 axios 的设计
|
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
// 2.1 提取核心数据 res.data
|
2024-02-01 19:51:58 +08:00
|
|
|
|
resolve(res.data as IResData<T>)
|
2023-12-23 11:39:46 +08:00
|
|
|
|
} else if (res.statusCode === 401) {
|
|
|
|
|
// 401错误 -> 清理用户信息,跳转到登录页
|
2024-01-19 17:28:04 +08:00
|
|
|
|
// userStore.clearUserInfo()
|
|
|
|
|
// uni.navigateTo({ url: '/pages/login/login' })
|
2023-12-23 11:39:46 +08:00
|
|
|
|
reject(res)
|
|
|
|
|
} else {
|
|
|
|
|
// 其他错误 -> 根据后端错误信息轻提示
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'none',
|
2024-02-01 19:51:58 +08:00
|
|
|
|
title: (res.data as IResData<T>).msg || '请求错误',
|
2023-12-23 11:39:46 +08:00
|
|
|
|
})
|
|
|
|
|
reject(res)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// 响应失败
|
|
|
|
|
fail(err) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'none',
|
|
|
|
|
title: '网络错误,换个网络试试',
|
|
|
|
|
})
|
|
|
|
|
reject(err)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default http
|