You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.7 KiB
85 lines
1.7 KiB
import axios from 'axios'
|
|
|
|
import { getFullURL } from '@/utils/http'
|
|
import { getAccessToken } from '@/utils/auth'
|
|
|
|
// const baseURL1 = import.meta.env.VITE_BASE_URL
|
|
const upload = axios.create({
|
|
baseURL: import.meta.env.VITE_BASE_URL,
|
|
adapter(config) {
|
|
const { url, method, data, params, headers, baseURL, paramsSerializer, filePath } =
|
|
config
|
|
headers['tenant-id'] = '1';
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: getFullURL(baseURL || '', url!, params, paramsSerializer),
|
|
filePath: filePath,
|
|
name: 'file',
|
|
header: headers,
|
|
formData: {
|
|
'user': 'test'
|
|
},
|
|
success: (res) => {
|
|
resolve(res)
|
|
},
|
|
fail: (error) => {
|
|
reject(error)
|
|
}
|
|
})
|
|
|
|
})
|
|
}
|
|
})
|
|
|
|
|
|
/**
|
|
* 请求拦截
|
|
*/
|
|
upload.interceptors.request.use((config) => {
|
|
const { method, params, url } = config
|
|
// 附带鉴权的token
|
|
const headers : any = {
|
|
token: getAccessToken(),
|
|
'Authorization': 'Bearer ' + getAccessToken()
|
|
}
|
|
if (uni.getStorageSync('openId')) {
|
|
headers['openId'] = uni.getStorageSync('openId')
|
|
}
|
|
// 不缓存get请求
|
|
if (method === 'get') {
|
|
headers['Cache-Control'] = 'no-cache'
|
|
}
|
|
// delete请求参数放入body中
|
|
if (method === 'delete') {
|
|
headers['Content-type'] = 'application/json;'
|
|
Object.assign(config, {
|
|
data: params,
|
|
params: {}
|
|
})
|
|
}
|
|
|
|
return {
|
|
...config,
|
|
headers
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 响应拦截
|
|
*/
|
|
upload.interceptors.response.use((v) => {
|
|
if (v.data?.code === 401) {
|
|
// alert('即将跳转登录页。。。', '登录过期')
|
|
// setTimeout(redirectHome, 1500)
|
|
return v.data
|
|
}
|
|
|
|
// @ts-ignore
|
|
if ((v.status || v.statusCode) === 200) {
|
|
return v.data
|
|
}
|
|
// alert(v.statusText, '网络错误')
|
|
return Promise.reject(v)
|
|
})
|
|
|
|
export default upload
|