根据环境变量控制重定向跳转地址

This commit is contained in:
bb_pan 2025-02-07 10:46:30 +08:00
parent 49c3a8fb50
commit d1e35d9352
5 changed files with 113 additions and 76 deletions

4
env/.env.dev vendored
View File

@ -8,8 +8,8 @@ VITE_API_URL = '/proxyApi'
# VITE_proxyTarget = 'http://10.40.92.74:8080' #盛旭 # VITE_proxyTarget = 'http://10.40.92.74:8080' #盛旭
# VITE_proxyTarget = 'http://192.168.2.246:28080' # 马帅 # VITE_proxyTarget = 'http://192.168.2.246:28080' # 马帅
VITE_proxyTarget = 'http://192.168.2.122:28080' # 梁超 # VITE_proxyTarget = 'http://192.168.2.122:28080' # 梁超
# VITE_proxyTarget = 'http://36.33.26.201:17788/proxyApi' # 测试服务 VITE_proxyTarget = 'http://36.33.26.201:17788/proxyApi' # 测试服务
# VITE_proxyTarget = 'http://192.168.0.244:28580' # 测试服务 # VITE_proxyTarget = 'http://192.168.0.244:28580' # 测试服务
# VITE_proxyTarget = 'http://192.168.2.75:28080' # 盛旭 # VITE_proxyTarget = 'http://192.168.2.75:28080' # 盛旭

View File

@ -55,7 +55,6 @@ const handlerLogout = () => {
type: 'warning', type: 'warning',
}) })
.then(() => { .then(() => {
// router.push('/login') // -
store.cleanUpToken('') store.cleanUpToken('')
store.clearUserInfo('') store.clearUserInfo('')
localStorage.clear() localStorage.clear()
@ -65,12 +64,16 @@ const handlerLogout = () => {
message: '已退出登录', message: '已退出登录',
duration: 1000, duration: 1000,
}) })
// - if (import.meta.env.VITE_API_URL == '/proxyApi') {
setTimeout(() => { router.push('/login') // -
window.location.replace( } else {
'http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/', // -
) setTimeout(() => {
}, 500) 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)
}
}) })
.catch(() => { .catch(() => {
ElMessage({ ElMessage({

View File

@ -3,9 +3,9 @@
import axios from 'axios' import axios from 'axios'
import NProgress from 'nprogress' import NProgress from 'nprogress'
// import { mainStore } from 'store/main' // import { mainStore } from 'store/main'
import { ElMessage } from "element-plus"; import { ElMessage } from 'element-plus'
import { saveAs } from 'file-saver' import { saveAs } from 'file-saver'
import router from "@/router" import router from '@/router'
// const store = mainStore() // const store = mainStore()
// const CancelToken = axios.CancelToken // const CancelToken = axios.CancelToken
@ -19,10 +19,9 @@ const iwsData: any = sessionStorage.getItem('data') || null
const service = axios.create({ const service = axios.create({
baseURL: baseUrl, baseURL: baseUrl,
timeout: 60000 timeout: 60000,
}) })
service.interceptors.request.use( service.interceptors.request.use(
(config) => { (config) => {
config.headers['Authorization'] = localStorage.getItem('tokenNew') config.headers['Authorization'] = localStorage.getItem('tokenNew')
@ -30,7 +29,7 @@ service.interceptors.request.use(
}, },
(error) => { (error) => {
return error return error
} },
) )
// 响应拦截 // 响应拦截
service.interceptors.response.use( service.interceptors.response.use(
@ -41,44 +40,59 @@ service.interceptors.response.use(
return data return data
} else if (data.code == '403') { } else if (data.code == '403') {
ElMessage.error('请重新登录') ElMessage.error('请重新登录')
setTimeout(() => { // 根据环境变量 VITE_API_URL 判断是否是本地开发环境
window.location.replace('http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/') if (import.meta.env.VITE_API_URL == '/proxyApi') {
}, 500) router.push('/login')
// 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') { } else if (data.code == '401') {
ElMessage.error('请登录') ElMessage.error('请登录')
// router.push('/login') if (import.meta.env.VITE_API_URL == '/proxyApi') {
setTimeout(() => { router.push('/login')
window.location.replace('http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/') } else {
}, 500) 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') { } else if (data.code == '500') {
ElMessage({ ElMessage({
type: 'error', type: 'error',
message: data.msg, message: data.msg,
duration: 1000, duration: 1000,
}) })
} } else {
else {
return data return data
} }
}, },
(error) => { (error) => {
// ElMessage.error('请求失败') // ElMessage.error('请求失败')
// console.log('error-异常', error) // console.log('error-异常', error)
} },
) )
export function get(url: string, params: any) { export function get(url: string, params: any) {
if (iwsData) { if (iwsData) {
service.get(`http://sgwpdm.ah.sgcc.com.cn/iws/cas/api/validate/sk?sessionKey=${iwsData.sessionKey}`).then((res: any) => { service
console.log(res, '请求结果') .get(
if (res.code != 200) { `http://sgwpdm.ah.sgcc.com.cn/iws/cas/api/validate/sk?sessionKey=${iwsData.sessionKey}`,
window.location.replace('http://sgwpdm.ah.sgcc.com.cn/iws/cas/login?appId=3874dcb953f184dc75450e33d6d6d4fa&service=http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/') )
} .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 false
}) })
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// NProgress.start() // NProgress.start()
@ -100,22 +114,27 @@ export function get(url: string, params: any) {
} }
export function post(url: string, params: any) { export function post(url: string, params: any) {
if (iwsData) { if (iwsData) {
service.get(`http://sgwpdm.ah.sgcc.com.cn/iws/cas/api/validate/sk?sessionKey=${iwsData.sessionKey}`).then((res: any) => { service
console.log(res, '请求结果') .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) { 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/') 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 false })
})
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// NProgress.start() // NProgress.start()
service service
.post(url, params, { .post(url, params, {
headers: { 'Content-Type': 'application/json; charset=utf-8' } headers: { 'Content-Type': 'application/json; charset=utf-8' },
}) })
.then((res: any) => { .then((res: any) => {
NProgress.done() NProgress.done()
@ -142,7 +161,7 @@ export function upload(url: string, params: any) {
NProgress.start() NProgress.start()
service service
.post(url, formData, { .post(url, formData, {
headers: { 'Content-Type': 'multipart/form-data' } headers: { 'Content-Type': 'multipart/form-data' },
}) })
.then((res: any) => { .then((res: any) => {
NProgress.done() NProgress.done()
@ -160,38 +179,41 @@ export function upload(url: string, params: any) {
} }
export function download(url: string, params: any, fileName: string = 'downloaded_file') { export function download(url: string, params: any, fileName: string = 'downloaded_file') {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
NProgress.start(); // 开始进度条 NProgress.start() // 开始进度条
service service
.post(url, params, { .post(url, params, {
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Type': 'application/x-www-form-urlencoded',
}, },
responseType: 'blob' // 请求返回数据类型为 Blob二进制流 responseType: 'blob', // 请求返回数据类型为 Blob二进制流
}) })
.then((res: any) => { .then((res: any) => {
NProgress.done(); // 结束进度条 NProgress.done() // 结束进度条
// 判断返回的内容类型是否是 Blob // 判断返回的内容类型是否是 Blob
const contentType = res.headers['content-type'] || ''; const contentType = res.headers['content-type'] || ''
if (!contentType.includes('application/octet-stream') && !contentType.includes('application/pdf')) { if (
reject('文件类型不正确'); !contentType.includes('application/octet-stream') &&
return; !contentType.includes('application/pdf')
) {
reject('文件类型不正确')
return
} }
// 处理文件下载 // 处理文件下载
const blob = new Blob([res.data], { type: contentType }); const blob = new Blob([res.data], { type: contentType })
// 使用 file-saver 保存文件 // 使用 file-saver 保存文件
saveAs(blob, fileName); saveAs(blob, fileName)
resolve('文件下载成功'); resolve('文件下载成功')
}) })
.catch((err) => { .catch((err) => {
NProgress.done(); // 结束进度条 NProgress.done() // 结束进度条
reject(err?.data || '下载失败'); reject(err?.data || '下载失败')
}); })
}); })
} }
export function put(url: string, params: any) { export function put(url: string, params: any) {
@ -199,7 +221,7 @@ export function put(url: string, params: any) {
NProgress.start() NProgress.start()
service service
.put(url, params, { .put(url, params, {
headers: { 'Content-Type': 'application/json; charset=utf-8' } headers: { 'Content-Type': 'application/json; charset=utf-8' },
}) })
.then((res: any) => { .then((res: any) => {
NProgress.done() NProgress.done()

View File

@ -83,7 +83,6 @@ const handlerLogout = () => {
type: 'warning', type: 'warning',
}) })
.then(() => { .then(() => {
// router.push('/login') // -
store.cleanUpToken('') store.cleanUpToken('')
store.clearUserInfo('') store.clearUserInfo('')
localStorage.clear() localStorage.clear()
@ -92,10 +91,14 @@ const handlerLogout = () => {
type: 'success', type: 'success',
message: '已退出登录', message: '已退出登录',
}) })
// - if (import.meta.env.VITE_API_URL == '/proxyApi') {
setTimeout(() => { router.push('/login') // -
window.location.replace('http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/') } else {
}, 500) // -
setTimeout(() => {
window.location.replace('http://sgwpdm.ah.sgcc.com.cn/iws/mall-view/')
}, 500)
}
}) })
.catch(() => { .catch(() => {
ElMessage({ ElMessage({

View File

@ -1103,12 +1103,15 @@ const onMainFileChange = (fileList: any) => {
const isProd = origin.indexOf('sgwpdm.ah.sgcc.com.cn') > -1 const isProd = origin.indexOf('sgwpdm.ah.sgcc.com.cn') > -1
const fileListTemp = fileList.map((e: any) => { const fileListTemp = fileList.map((e: any) => {
let testUrl = ''
if (import.meta.env.VITE_API_URL == '/proxyApi') {
testUrl = e.url // -
} else {
testUrl = origin + '/ahbns/' + e.url // -
}
return { return {
fileName: e.name, fileName: e.name,
fileUrl: isProd fileUrl: isProd ? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url : testUrl,
? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url
: origin + '/ahbns/' + e.url, // -
// : e.url, // -
} }
}) })
addAndEditFormRef.value.clearValidate('mainFileList') addAndEditFormRef.value.clearValidate('mainFileList')
@ -1119,12 +1122,15 @@ const onDetailsFileChange = (fileList: any) => {
const origin = window.location.origin const origin = window.location.origin
const isProd = origin.indexOf('sgwpdm.ah.sgcc.com.cn') > -1 const isProd = origin.indexOf('sgwpdm.ah.sgcc.com.cn') > -1
const fileListTemp = fileList.map((e: any) => { const fileListTemp = fileList.map((e: any) => {
let testUrl = ''
if (import.meta.env.VITE_API_URL == '/proxyApi') {
testUrl = e.url // -
} else {
testUrl = origin + '/ahbns/' + e.url // -
}
return { return {
fileName: e.name, fileName: e.name,
fileUrl: isProd fileUrl: isProd ? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url : testUrl,
? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url
: origin + '/ahbns/' + e.url, // -
// : e.url, // -
} }
}) })
addAndEditFormRef.value.clearValidate('detailsFileList') addAndEditFormRef.value.clearValidate('detailsFileList')
@ -1163,12 +1169,15 @@ const onFileChangeEquip = (fileList: any, row: any, index: number, type: number)
equipTableList.value[index].examinationPdf = [] equipTableList.value[index].examinationPdf = []
} }
const fileListTemp = fileList.map((e: any) => { const fileListTemp = fileList.map((e: any) => {
let testUrl = ''
if (import.meta.env.VITE_API_URL == '/proxyApi') {
testUrl = e.url // -
} else {
testUrl = origin + '/ahbns/' + e.url // -
}
return { return {
fileName: e.name, fileName: e.name,
fileUrl: isProd fileUrl: isProd ? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url : testUrl,
? 'http://sgwpdm.ah.sgcc.com.cn/iws/ahbns/' + e.url
: origin + '/ahbns/' + e.url, // -
// : e.url, // -
} }
}) })
console.log('11111', fileListTemp[0].fileUrl) console.log('11111', fileListTemp[0].fileUrl)