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'
|
2025-02-27 15:29:36 +08:00
|
|
|
|
import { ElLoading, ElMessage } from 'element-plus'
|
2024-12-05 16:58:35 +08:00
|
|
|
|
import { saveAs } from 'file-saver'
|
2025-02-07 10:46:30 +08:00
|
|
|
|
import router from '@/router'
|
2025-02-07 16:27:08 +08:00
|
|
|
|
import { tansParams } from '@/utils/bonus'
|
|
|
|
|
|
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
|
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,
|
2025-02-07 10:46:30 +08:00
|
|
|
|
timeout: 60000,
|
2023-11-30 10:49:45 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-02-07 16:27:08 +08:00
|
|
|
|
// 请求拦截
|
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')
|
2025-02-07 16:27:08 +08:00
|
|
|
|
// 入参是否加密
|
2025-07-15 19:42:09 +08:00
|
|
|
|
config.headers['encryptRequest'] = 'true'
|
2025-02-07 16:27:08 +08:00
|
|
|
|
// 数据完整性校验
|
2025-07-15 19:42:09 +08:00
|
|
|
|
config.headers['checkIntegrity'] = 'true'
|
2025-02-07 16:27:08 +08:00
|
|
|
|
// 回参是否加密
|
2025-07-15 19:42:09 +08:00
|
|
|
|
config.headers['encryptResponse'] = 'true'
|
2025-02-07 16:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 对请求数据进行加密
|
|
|
|
|
|
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
|
2025-02-24 15:40:42 +08:00
|
|
|
|
if (config.headers['encryptRequest'] == 'true') {
|
|
|
|
|
|
config.data = encryptWithSM4(data + '|' + hashWithSM3AndSalt(data))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
config.data = data
|
|
|
|
|
|
}
|
2025-02-07 16:27:08 +08:00
|
|
|
|
}
|
2023-11-30 10:49:45 +08:00
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
return error
|
2025-02-07 10:46:30 +08:00
|
|
|
|
},
|
2023-11-30 10:49:45 +08:00
|
|
|
|
)
|
2025-02-27 15:29:36 +08:00
|
|
|
|
const openLoading = () => {
|
|
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
|
|
const loading = ElLoading.service({
|
|
|
|
|
|
lock: true,
|
|
|
|
|
|
text: 'Loading...',
|
|
|
|
|
|
background: 'rgba(0, 0, 0, 0.7)',
|
|
|
|
|
|
})
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
loading.close()
|
|
|
|
|
|
resolve()
|
|
|
|
|
|
}, 2000)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-11-30 10:49:45 +08:00
|
|
|
|
// 响应拦截
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(res) => {
|
2024-11-26 15:38:36 +08:00
|
|
|
|
ElMessage.closeAll()
|
2025-02-13 15:30:20 +08:00
|
|
|
|
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)
|
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('请重新登录')
|
2025-02-14 18:01:24 +08:00
|
|
|
|
// 清除token
|
|
|
|
|
|
localStorage.removeItem('tokenNew')
|
2025-02-27 15:29:36 +08:00
|
|
|
|
openLoading()
|
2025-02-07 10:46:30 +08:00
|
|
|
|
// 根据环境变量 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-13 15:30:20 +08:00
|
|
|
|
ElMessage.error('请重新登录')
|
2025-02-14 18:01:24 +08:00
|
|
|
|
localStorage.removeItem('tokenNew')
|
2025-02-27 15:29:36 +08:00
|
|
|
|
openLoading()
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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,
|
|
|
|
|
|
})
|
2025-02-07 10:46:30 +08:00
|
|
|
|
} 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)
|
2025-02-07 10:46:30 +08:00
|
|
|
|
},
|
2023-11-30 10:49:45 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
export function get(url: string, params: any) {
|
2025-02-11 16:12:38 +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
|
|
|
|
|
2025-02-11 16:12:38 +08:00
|
|
|
|
// return false
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
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-11 16:12:38 +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
|
|
|
|
|
2025-02-11 16:12:38 +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
|
|
|
|
|
2025-02-11 16:12:38 +08:00
|
|
|
|
// return false
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
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, {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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, {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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) => {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
NProgress.start() // 开始进度条
|
2024-12-05 16:58:35 +08:00
|
|
|
|
|
2023-11-30 10:49:45 +08:00
|
|
|
|
service
|
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
|
headers: {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
2023-11-30 10:49:45 +08:00
|
|
|
|
},
|
2025-02-07 10:46:30 +08:00
|
|
|
|
responseType: 'blob', // 请求返回数据类型为 Blob(二进制流)
|
2023-11-30 10:49:45 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then((res: any) => {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
NProgress.done() // 结束进度条
|
2024-12-05 16:58:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 判断返回的内容类型是否是 Blob
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理文件下载
|
2025-02-07 10:46:30 +08:00
|
|
|
|
const blob = new Blob([res.data], { type: contentType })
|
2024-12-05 16:58:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用 file-saver 保存文件
|
2025-02-07 10:46:30 +08:00
|
|
|
|
saveAs(blob, fileName)
|
2024-12-05 16:58:35 +08:00
|
|
|
|
|
2025-02-07 10:46:30 +08:00
|
|
|
|
resolve('文件下载成功')
|
2023-11-30 10:49:45 +08:00
|
|
|
|
})
|
|
|
|
|
|
.catch((err) => {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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, {
|
2025-02-07 10:46:30 +08:00
|
|
|
|
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)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|