Zlpt_Portal/src/http/index.ts

260 lines
8.0 KiB
TypeScript
Raw Normal View History

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'
2024-12-25 08:52:58 +08:00
// import { mainStore } from 'store/main'
import { ElMessage } from 'element-plus'
2024-12-05 16:58:35 +08:00
import { saveAs } from 'file-saver'
import router from '@/router'
2024-12-25 08:52:58 +08:00
// const store = mainStore()
2023-11-30 10:49:45 +08:00
// const CancelToken = axios.CancelToken
// const source = CancelToken.source()
const baseUrl = import.meta.env.VITE_API_URL
2023-12-06 13:40:33 +08:00
const VITE_token = import.meta.env.VITE_token
2023-12-06 13:45:03 +08:00
const VITE_LocalFlag = import.meta.env.VITE_LocalFlag
2023-12-06 13:40:33 +08:00
const VITE_ENV = import.meta.env.VITE_ENV
2025-02-05 09:12:35 +08:00
const iwsData: any = sessionStorage.getItem('data') || null
2023-12-05 09:45:01 +08:00
2023-11-30 10:49:45 +08:00
const service = axios.create({
baseURL: baseUrl,
timeout: 60000,
2023-11-30 10:49:45 +08:00
})
service.interceptors.request.use(
(config) => {
2024-12-25 08:52:58 +08:00
config.headers['Authorization'] = localStorage.getItem('tokenNew')
2023-11-30 10:49:45 +08:00
return config
},
(error) => {
return error
},
2023-11-30 10:49:45 +08:00
)
// 响应拦截
service.interceptors.response.use(
(res) => {
2024-11-26 15:38:36 +08:00
ElMessage.closeAll()
2023-12-05 09:45:01 +08:00
const { data } = res
2023-12-06 15:58:21 +08:00
if (data.code == '200') {
2023-12-05 09:45:01 +08:00
return data
2023-12-06 15:58:21 +08:00
} else if (data.code == '403') {
2023-12-05 09:45:01 +08:00
ElMessage.error('请重新登录')
// 根据环境变量 VITE_API_URL 判断是否是本地开发环境
if (import.meta.env.VITE_API_URL == '/proxyApi') {
router.push('/login')
} else {
setTimeout(() => {
window.location.replace(
'http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/',
)
}, 500)
}
2023-12-09 17:00:58 +08:00
} else if (data.code == '401') {
2025-02-05 09:12:35 +08:00
ElMessage.error('请登录')
if (import.meta.env.VITE_API_URL == '/proxyApi') {
router.push('/login')
} else {
setTimeout(() => {
window.location.replace(
'http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/',
)
}, 500)
}
2024-11-26 15:38:36 +08:00
} else if (data.code == '500') {
2024-12-06 17:48:56 +08:00
ElMessage({
type: 'error',
message: data.msg,
duration: 1000,
})
} else {
2023-12-05 09:45:01 +08:00
return data
}
2023-11-30 10:49:45 +08:00
},
(error) => {
2024-12-06 17:48:56 +08:00
// ElMessage.error('请求失败')
// console.log('error-异常', error)
},
2023-11-30 10:49:45 +08:00
)
export function get(url: string, params: any) {
2025-02-05 09:12:35 +08:00
if (iwsData) {
service
.get(
`http://sgwpdm.ah.sgcc.com.cn/iws/cas/api/validate/sk?sessionKey=${iwsData.sessionKey}`,
)
.then((res: any) => {
console.log(res, '请求结果')
if (res.code != 200) {
window.location.replace(
'http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/',
)
}
2025-02-05 09:12:35 +08:00
return false
})
2025-02-05 09:12:35 +08:00
}
2023-11-30 10:49:45 +08:00
return new Promise((resolve, reject) => {
2024-12-25 14:24:34 +08:00
// NProgress.start()
2023-11-30 10:49:45 +08:00
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) => {
2024-12-25 14:24:34 +08:00
// NProgress.done()
2023-11-30 10:49:45 +08:00
reject(err.data)
})
})
}
export function post(url: string, params: any) {
2025-02-05 09:12:35 +08:00
if (iwsData) {
service
.get(
`http://sgwpdm.ah.sgcc.com.cn/iws/cas/api/validate/sk?sessionKey=${iwsData.sessionKey}`,
)
.then((res: any) => {
console.log(res, '请求结果')
2025-02-05 09:12:35 +08:00
if (res.code != 200) {
window.location.replace(
'http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/',
)
}
2025-02-05 09:12:35 +08:00
return false
})
2025-02-05 09:12:35 +08:00
}
2023-11-30 10:49:45 +08:00
return new Promise((resolve, reject) => {
2024-12-25 14:24:34 +08:00
// NProgress.start()
2023-11-30 10:49:45 +08:00
service
.post(url, params, {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
2023-11-30 10:49:45 +08:00
})
.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) => {
2024-12-25 14:24:34 +08:00
// NProgress.done()
2023-11-30 10:49:45 +08:00
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' },
2023-11-30 10:49:45 +08:00
})
.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)
})
})
}
2024-12-05 16:58:35 +08:00
export function download(url: string, params: any, fileName: string = 'downloaded_file') {
2023-11-30 10:49:45 +08:00
return new Promise((resolve, reject) => {
NProgress.start() // 开始进度条
2024-12-05 16:58:35 +08:00
2023-11-30 10:49:45 +08:00
service
.post(url, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
2023-11-30 10:49:45 +08:00
},
responseType: 'blob', // 请求返回数据类型为 Blob二进制流
2023-11-30 10:49:45 +08:00
})
.then((res: any) => {
NProgress.done() // 结束进度条
2024-12-05 16:58:35 +08:00
// 判断返回的内容类型是否是 Blob
const contentType = res.headers['content-type'] || ''
if (
!contentType.includes('application/octet-stream') &&
!contentType.includes('application/pdf')
) {
reject('文件类型不正确')
return
2024-12-05 16:58:35 +08:00
}
// 处理文件下载
const blob = new Blob([res.data], { type: contentType })
2024-12-05 16:58:35 +08:00
// 使用 file-saver 保存文件
saveAs(blob, fileName)
2024-12-05 16:58:35 +08:00
resolve('文件下载成功')
2023-11-30 10:49:45 +08:00
})
.catch((err) => {
NProgress.done() // 结束进度条
reject(err?.data || '下载失败')
})
})
2023-11-30 10:49:45 +08:00
}
2023-12-06 14:12:36 +08:00
export function put(url: string, params: any) {
return new Promise((resolve, reject) => {
NProgress.start()
service
.put(url, params, {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
2023-12-06 14:12:36 +08:00
})
.then((res: any) => {
NProgress.done()
if (res.code == '200') {
resolve(res)
} else {
reject(res)
}
})
.catch((err) => {
NProgress.done()
reject(err)
})
})
}
export function detele(url: string, params: any) {
return new Promise((resolve, reject) => {
NProgress.start()
service
.delete(url, { params })
.then((res: any) => {
NProgress.done()
if (res.code == '200') {
resolve(res)
} else {
reject(res)
}
})
.catch((err) => {
NProgress.done()
reject(err.data)
})
})
}