2023-11-30 10:49:45 +08:00
|
|
|
//http.ts
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
import NProgress from 'nprogress'
|
|
|
|
|
import { useStore } from 'store/main'
|
|
|
|
|
const store = useStore()
|
|
|
|
|
// const CancelToken = axios.CancelToken
|
|
|
|
|
// const source = CancelToken.source()
|
|
|
|
|
const baseUrl = import.meta.env.VITE_API_URL
|
|
|
|
|
// const mode = import.meta.env.VITE_BUILD_MODE
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
baseURL: baseUrl,
|
|
|
|
|
timeout: 60000
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
(config) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
config.headers['Authorization'] = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6Ijc5MjRkNDc1LTRjMmUtNGViYy05ZDU0LTA2NzNmNWU0MDhiMyIsInVzZXJuYW1lIjoiYWRtaW4ifQ.M3H9jHnfFAKJ3szdiDb79hIHfiS8AWvaI51mP65l01Q2G0jcLSTvjlub8FykYV3A27If7V6GBRo83u8spRDquw"
|
2023-11-30 10:49:45 +08:00
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return error
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
// 响应拦截
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
(res) => {
|
|
|
|
|
return res.data
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
|
2023-11-30 10:49:45 +08:00
|
|
|
console.log('error-异常', error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export function get(url: string, params: any) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
service
|
|
|
|
|
.get(url, { params })
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
NProgress.done()
|
2023-12-04 10:19:21 +08:00
|
|
|
if (res.code == '200') {
|
2023-12-04 10:26:00 +08:00
|
|
|
resolve(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
} else {
|
2023-12-04 10:26:00 +08:00
|
|
|
reject(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
reject(err.data)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
export function post(url: string, params: any) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
service
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
headers: { 'Content-Type': 'application/json; charset=utf-8' }
|
|
|
|
|
})
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
console.log('---------------------------', res)
|
2023-12-04 10:19:21 +08:00
|
|
|
if (res.code == '200') {
|
2023-12-04 13:10:38 +08:00
|
|
|
resolve(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
} else {
|
2023-12-04 13:10:38 +08:00
|
|
|
reject(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
reject(err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
export function upload(url: string, params: any) {
|
|
|
|
|
const formData = new FormData()
|
|
|
|
|
for (const key in params) {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(params, key)) {
|
|
|
|
|
formData.append(key, params[key])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
service
|
|
|
|
|
.post(url, formData, {
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
|
|
|
})
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
NProgress.done()
|
2023-12-04 10:19:21 +08:00
|
|
|
if (res.code == '200') {
|
2023-12-04 13:10:38 +08:00
|
|
|
resolve(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
} else {
|
2023-12-04 13:10:38 +08:00
|
|
|
reject(res)
|
2023-11-30 10:49:45 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
reject(err.data)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
export function download(url: string, params: any) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
service
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
|
},
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
})
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
resolve(res)
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
reject(err.data)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|