//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' // 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') return config }, (error) => { return error }, ) // 响应拦截 service.interceptors.response.use( (res) => { ElMessage.closeAll() const { data } = res if (data.code == '200') { return data } else if (data.code == '403') { 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) } } else if (data.code == '401') { 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) } } 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) }) }) }