55 changed files with 410 additions and 402 deletions
@ -1,164 +0,0 @@ |
|||
import type { App } from 'vue'; |
|||
import Axios from 'axios'; |
|||
import { i18n } from './i18n'; |
|||
import { PConst } from '@/platform/PConst'; |
|||
import { Environment } from '@/platform/plugin/environment'; |
|||
import { NotifyManager, Oauth2Manager } from './manager'; |
|||
import { QuasarTools } from '@/platform/utils'; |
|||
|
|||
const ignoredUrls: string[] = [PConst.API_I18N_MESSAGES_URL, PConst.API_APP_CONFIGURE_URL]; |
|||
const gc = Environment.getConfigure(); |
|||
|
|||
// 请求拦截器
|
|||
const requestInterceptor = (config: any) => { |
|||
config.headers.locale = gc.setting.i18n.locale; |
|||
// 忽略无需认证的请求 URL
|
|||
for (const url of ignoredUrls) { |
|||
if (config.url.includes(url)) { |
|||
return config; |
|||
} |
|||
} |
|||
const result = config; |
|||
result.headers.Authorization = 'Bearer ' + Oauth2Manager.getLocalAccessToken(); |
|||
/* |
|||
// 对于需要认证的请求 URL 添加 basic 认证
|
|||
const result = config; |
|||
if (gc.axios?.basicAuth?.enable) { |
|||
result.headers.Authorization = 'Basic ' + window.btoa(gc.axios.basicAuth.username + ':' + gc.axios.basicAuth.password); |
|||
} |
|||
*/ |
|||
// 如果请求时传入 { loading: true } 属性, 则自动显示 "正在处理..., 请等待" 模态对话框
|
|||
if (config?.loading) { |
|||
QuasarTools.getQuasar()?.loading?.show({ |
|||
message: i18n.global.t('loading'), |
|||
boxClass: 'bg-grey-2 text-grey-9', |
|||
spinnerColor: 'primary', |
|||
}); |
|||
} |
|||
return result; |
|||
}; |
|||
|
|||
// 请求错误拦截器
|
|||
const requestErrorInterceptor = (error: any) => { |
|||
return Promise.reject(error); |
|||
}; |
|||
|
|||
// 响应拦截器
|
|||
const responseInterceptor = (response: any) => { |
|||
// 请求成功, 进入该方法说明 response 的状态码为 2xx
|
|||
QuasarTools.getQuasar()?.loading?.hide(); |
|||
return response.data; |
|||
}; |
|||
|
|||
// 响应错误拦截器
|
|||
const responseErrorInterceptor = (error: any) => { |
|||
QuasarTools.getQuasar()?.loading?.hide(); |
|||
// 请求失败, 进入该方法说明 response 的状态码不为 2xx
|
|||
if (error.code === 'ECONNABORTED' || error.message.indexOf('timeout') !== -1 || error.message === 'Network Error') { |
|||
// 发生网络错误
|
|||
const $t = i18n.global.t; |
|||
NotifyManager.error($t('NetworkError')); |
|||
return Promise.reject({ |
|||
code: 'NetworkError', |
|||
errorMessageI18nKey: 'NetworkError', |
|||
errorMessage: error.message, |
|||
exception: 'NetworkError', |
|||
stackTrace: error.stack, |
|||
data: null, |
|||
}); |
|||
} else { |
|||
// 服务器端错误
|
|||
const response = error?.response; |
|||
const status = response?.status; |
|||
const data = response?.data; |
|||
if (status === 401) { |
|||
if (Oauth2Manager.isAuthorizationCodeRedirectUri()) { |
|||
return Promise.resolve({}); |
|||
} else { |
|||
if (gc.oauth2) { |
|||
let url = '/oauth2/authorize?'; |
|||
url += 'client_id=' + gc.oauth2.clientId; |
|||
url += '&client_secret=' + gc.oauth2.clientSecret; |
|||
url += '&response_type=code'; |
|||
url += '&redirect_uri=' + encodeURIComponent(gc.oauth2.redirectUri); |
|||
window.location.href = Environment.apiContextPath(url); |
|||
} |
|||
} |
|||
return; |
|||
} |
|||
//下载错误
|
|||
if (error.request.responseType === 'blob') { |
|||
NotifyManager.error(i18n.global.t(error?.code)); |
|||
return Promise.reject(error); |
|||
} |
|||
//其他错误
|
|||
if (status === 500) { |
|||
NotifyManager.error(i18n.global.t(data?.errorMessageI18nKey)); |
|||
return Promise.reject(response.data); |
|||
} else { |
|||
NotifyManager.error(i18n.global.t(status)); |
|||
return Promise.reject({ |
|||
code: status, |
|||
errorMessageI18nKey: status, |
|||
errorMessage: error.message, |
|||
exception: error.code, |
|||
stackTrace: error.stack, |
|||
data: null, |
|||
}); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
// 普通 axios
|
|||
const axios = Axios.create({ |
|||
baseURL: gc.axios?.baseURL || '', |
|||
timeout: gc.axios?.timeout || 1000 * 60, |
|||
}); |
|||
axios.interceptors.request.use(requestInterceptor, requestErrorInterceptor); |
|||
axios.interceptors.response.use(responseInterceptor, responseErrorInterceptor); |
|||
|
|||
// 无默认服务器端错误处理的 axios
|
|||
const noErrorAxios = Axios.create({ |
|||
baseURL: gc.axios?.baseURL || '', |
|||
timeout: gc.axios?.timeout || 1000 * 60, |
|||
}); |
|||
noErrorAxios.interceptors.request.use(requestInterceptor, requestErrorInterceptor); |
|||
noErrorAxios.interceptors.response.use(responseInterceptor, (error: any) => { |
|||
QuasarTools.getQuasar()?.loading?.hide(); |
|||
// 请求失败, 进入该方法说明 response 的状态码不为 2xx
|
|||
if (error.code === 'ECONNABORTED' || error.message.indexOf('timeout') !== -1 || error.message === 'Network Error') { |
|||
// 发生网络错误
|
|||
const $t = i18n.global.t; |
|||
NotifyManager.error($t('NetworkError')); |
|||
return Promise.reject({ |
|||
code: 'NetworkError', |
|||
errorMessageI18nKey: 'NetworkError', |
|||
errorMessage: error.message, |
|||
exception: 'NetworkError', |
|||
stackTrace: error.stack, |
|||
data: null, |
|||
}); |
|||
} else { |
|||
return Promise.reject(error); |
|||
} |
|||
}); |
|||
|
|||
// 下载二进制 axios
|
|||
const blobAxios = Axios.create({ |
|||
baseURL: gc.axios?.baseURL || '', |
|||
timeout: gc.axios?.timeout || 1000 * 60, |
|||
}); |
|||
blobAxios.interceptors.request.use(requestInterceptor, requestErrorInterceptor); |
|||
blobAxios.interceptors.response.use((response: any) => { |
|||
QuasarTools.getQuasar()?.loading?.hide(); |
|||
return response; |
|||
}, responseErrorInterceptor); |
|||
|
|||
export default { |
|||
install: (app: App) => { |
|||
app.config.globalProperties.$axios = axios; |
|||
app.config.globalProperties.$blobAxios = blobAxios; |
|||
}, |
|||
}; |
|||
|
|||
export { axios, blobAxios, noErrorAxios }; |
@ -1,12 +0,0 @@ |
|||
package io.sc.platform.lcdp.configure.controller; |
|||
|
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
|
|||
@Controller |
|||
public class ConfigureJsController { |
|||
@RequestMapping("/configure.js") |
|||
public String configureJs(){ |
|||
return "configure.js"; |
|||
} |
|||
} |
Loading…
Reference in new issue