291 lines
9.3 KiB
TypeScript
291 lines
9.3 KiB
TypeScript
//http.ts
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||
import axios from 'axios'
|
||
import NProgress from 'nprogress'
|
||
// import { mainStore } from 'store/main'
|
||
import { ElMessage } from 'element-plus'
|
||
import { saveAs } from 'file-saver'
|
||
import router from '@/router'
|
||
import { tansParams } from '@/utils/bonus'
|
||
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
|
||
// const store = mainStore()
|
||
|
||
// const CancelToken = axios.CancelToken
|
||
// const source = CancelToken.source()
|
||
const baseUrl = import.meta.env.VITE_API_URL
|
||
const VITE_token = import.meta.env.VITE_token
|
||
const VITE_LocalFlag = import.meta.env.VITE_LocalFlag
|
||
|
||
const VITE_ENV = import.meta.env.VITE_ENV
|
||
const iwsData: any = sessionStorage.getItem('data') || null
|
||
|
||
const service = axios.create({
|
||
baseURL: baseUrl,
|
||
timeout: 60000,
|
||
})
|
||
|
||
// 请求拦截
|
||
service.interceptors.request.use(
|
||
(config) => {
|
||
config.headers['Authorization'] = localStorage.getItem('tokenNew')
|
||
// 入参是否加密
|
||
config.headers['encryptRequest'] = 'true'
|
||
// 数据完整性校验
|
||
config.headers['checkIntegrity'] = 'true'
|
||
// 回参是否加密
|
||
config.headers['encryptResponse'] = 'true'
|
||
|
||
// 对请求数据进行加密
|
||
if (config.method === 'get' && config.params) {
|
||
let url = config.url + '?' + tansParams(config.params)
|
||
url = url.slice(0, -1)
|
||
config.params = {}
|
||
config.url = url
|
||
}
|
||
if (config.data) {
|
||
let data = typeof config.data === 'object' ? JSON.stringify(config.data) : config.data
|
||
config.data = encryptWithSM4(data + '|' + hashWithSM3AndSalt(data))
|
||
// config.data = data
|
||
}
|
||
return config
|
||
},
|
||
(error) => {
|
||
return error
|
||
},
|
||
)
|
||
// 响应拦截
|
||
service.interceptors.response.use(
|
||
(res) => {
|
||
ElMessage.closeAll()
|
||
let data: any = null
|
||
if (res.headers.encryptresponse && !res.data.hasOwnProperty('code')) {
|
||
data = JSON.parse(decryptWithSM4(res.data))
|
||
} else {
|
||
data = res.data
|
||
}
|
||
// console.log('🚀 ~ 响应-data:', data)
|
||
if (data.code == '200') {
|
||
return data
|
||
} else if (data.code == '403') {
|
||
ElMessage.error('请重新登录')
|
||
// 清除token
|
||
localStorage.removeItem('tokenNew')
|
||
// 根据环境变量 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)
|
||
}
|
||
} else if (data.code == '401') {
|
||
ElMessage.error('请重新登录')
|
||
localStorage.removeItem('tokenNew')
|
||
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)
|
||
}
|
||
} else if (data.code == '500') {
|
||
ElMessage({
|
||
type: 'error',
|
||
message: data.msg,
|
||
duration: 1000,
|
||
})
|
||
} else {
|
||
return data
|
||
}
|
||
},
|
||
(error) => {
|
||
// ElMessage.error('请求失败')
|
||
// console.log('error-异常', error)
|
||
},
|
||
)
|
||
|
||
export function get(url: string, params: any) {
|
||
// 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/',
|
||
// )
|
||
// }
|
||
|
||
// return false
|
||
// })
|
||
// }
|
||
return new Promise((resolve, reject) => {
|
||
// NProgress.start()
|
||
service
|
||
.get(url, { params })
|
||
.then((res: any) => {
|
||
NProgress.done()
|
||
if (res.code == '200') {
|
||
resolve(res)
|
||
} else {
|
||
reject(res)
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
// NProgress.done()
|
||
reject(err.data)
|
||
})
|
||
})
|
||
}
|
||
export function post(url: string, params: any) {
|
||
// 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/',
|
||
// )
|
||
// }
|
||
|
||
// return false
|
||
// })
|
||
// }
|
||
return new Promise((resolve, reject) => {
|
||
// NProgress.start()
|
||
service
|
||
.post(url, params, {
|
||
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
||
})
|
||
.then((res: any) => {
|
||
NProgress.done()
|
||
if (res.code == '200') {
|
||
resolve(res)
|
||
} else {
|
||
reject(res)
|
||
}
|
||
})
|
||
.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()
|
||
if (res.code == '200') {
|
||
resolve(res)
|
||
} else {
|
||
reject(res)
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
NProgress.done()
|
||
reject(err.data)
|
||
})
|
||
})
|
||
}
|
||
export function download(url: string, params: any, fileName: string = 'downloaded_file') {
|
||
return new Promise((resolve, reject) => {
|
||
NProgress.start() // 开始进度条
|
||
|
||
service
|
||
.post(url, params, {
|
||
headers: {
|
||
'Content-Type': 'application/x-www-form-urlencoded',
|
||
},
|
||
responseType: 'blob', // 请求返回数据类型为 Blob(二进制流)
|
||
})
|
||
.then((res: any) => {
|
||
NProgress.done() // 结束进度条
|
||
|
||
// 判断返回的内容类型是否是 Blob
|
||
const contentType = res.headers['content-type'] || ''
|
||
if (
|
||
!contentType.includes('application/octet-stream') &&
|
||
!contentType.includes('application/pdf')
|
||
) {
|
||
reject('文件类型不正确')
|
||
return
|
||
}
|
||
|
||
// 处理文件下载
|
||
const blob = new Blob([res.data], { type: contentType })
|
||
|
||
// 使用 file-saver 保存文件
|
||
saveAs(blob, fileName)
|
||
|
||
resolve('文件下载成功')
|
||
})
|
||
.catch((err) => {
|
||
NProgress.done() // 结束进度条
|
||
reject(err?.data || '下载失败')
|
||
})
|
||
})
|
||
}
|
||
|
||
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' },
|
||
})
|
||
.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)
|
||
})
|
||
})
|
||
}
|