|
@@ -0,0 +1,132 @@
|
|
|
+import axios from 'axios'
|
|
|
+import { ElButton, ElMessage, ElNotification } from 'element-plus'
|
|
|
+import { h } from 'vue'
|
|
|
+import { YMDHms } from '@/utils/czr-util'
|
|
|
+
|
|
|
+// import {toLogin} from "@/utils/permissions";
|
|
|
+export class Interceptors {
|
|
|
+ public instance: any
|
|
|
+
|
|
|
+ constructor() {
|
|
|
+ this.instance = axios.create({
|
|
|
+ baseURL: '', // api base_url
|
|
|
+ timeout: 1000 * 300,
|
|
|
+ })
|
|
|
+ this.initInterceptors()
|
|
|
+ }
|
|
|
+
|
|
|
+ public getInterceptors() {
|
|
|
+ return this.instance
|
|
|
+ }
|
|
|
+
|
|
|
+ public initInterceptors() {
|
|
|
+ /**
|
|
|
+ * 请求拦截器
|
|
|
+ * 每次请求前,如果存在token则在请求头中携带token
|
|
|
+ */
|
|
|
+ this.instance.interceptors.request.use(
|
|
|
+ (config: any) => {
|
|
|
+ if (!config.headers.Authorization) {
|
|
|
+ const token = localStorage.getItem(
|
|
|
+ (import.meta as any).env.VITE_TOKEN,
|
|
|
+ )
|
|
|
+ if (token) {
|
|
|
+ config.headers.Authorization = token
|
|
|
+ } else {
|
|
|
+ // @ts-ignore
|
|
|
+ delete config.headers.Authorization
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return config
|
|
|
+ },
|
|
|
+ (error: any) => {},
|
|
|
+ )
|
|
|
+
|
|
|
+ // 响应拦截器
|
|
|
+ this.instance.interceptors.response.use(
|
|
|
+ // 请求成功
|
|
|
+ (res: any) => {
|
|
|
+ // if (res.data.success) {
|
|
|
+ if (res.status === 200) {
|
|
|
+ return Promise.resolve(res.data)
|
|
|
+ } else {
|
|
|
+ // this.errorHandle(res)
|
|
|
+ return Promise.reject(res.data)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 请求失败
|
|
|
+ (error: { response: any }) => {
|
|
|
+ const { response } = error
|
|
|
+ if (response) {
|
|
|
+ // 请求已发出,但是不在2xx的范围
|
|
|
+ // this.errorHandle(response)
|
|
|
+ return Promise.reject(response.data)
|
|
|
+ } else {
|
|
|
+ ElMessage.warning('网络连接异常,请稍后再试!')
|
|
|
+ // 抛出报错信息,在页面里需要接收
|
|
|
+ return Promise.reject(error)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ private errorHandle(res: any) {
|
|
|
+ const hasLogin = (import.meta as any).env.VITE_LOGIN_MUST === 'Y'
|
|
|
+ if (hasLogin) {
|
|
|
+ console.error('错误接口:' + res.request.responseURL)
|
|
|
+ const exportLog = () => {
|
|
|
+ let str = ''
|
|
|
+ const arr = [
|
|
|
+ `错误日期:${YMDHms(res.headers.date)}`,
|
|
|
+ `请求方式:${res.config.method.toUpperCase()}`,
|
|
|
+ `接口地址:${res.request.responseURL}`,
|
|
|
+ `接口状态:${res.status} ${res.statusText}`,
|
|
|
+ `请求Token:${res.config.headers.Authorization}`,
|
|
|
+ `请求参数:${res.config.data || ''}`,
|
|
|
+ `返回结果:${res.request.response}`,
|
|
|
+ ]
|
|
|
+ arr.forEach((v) => {
|
|
|
+ str += v
|
|
|
+ str += '\n\n'
|
|
|
+ })
|
|
|
+ const blob = new Blob([str], { type: 'text/plain' })
|
|
|
+ const url = URL.createObjectURL(blob)
|
|
|
+ const a = document.createElement('a')
|
|
|
+ a.href = url
|
|
|
+ a.download = `错误日志(${YMDHms(res.headers.date)}).txt`
|
|
|
+ document.body.appendChild(a)
|
|
|
+ a.click()
|
|
|
+ setTimeout(() => {
|
|
|
+ document.body.removeChild(a)
|
|
|
+ URL.revokeObjectURL(url)
|
|
|
+ }, 0)
|
|
|
+ }
|
|
|
+ ElNotification({
|
|
|
+ title: res.data.message,
|
|
|
+ message: h('div', null, [
|
|
|
+ h(
|
|
|
+ ElButton,
|
|
|
+ {
|
|
|
+ type: 'warning',
|
|
|
+ size: 'small',
|
|
|
+ plain: true,
|
|
|
+ onClick: () => exportLog(),
|
|
|
+ },
|
|
|
+ { default: () => '错误日志' },
|
|
|
+ ),
|
|
|
+ h(
|
|
|
+ 'div',
|
|
|
+ { class: 'mt-2 overflow-y-auto', style: 'max-height: 50vh;' },
|
|
|
+ res.data.data
|
|
|
+ ? typeof res.data.data === 'string'
|
|
|
+ ? res.data.data
|
|
|
+ : JSON.stringify(res.data.data)
|
|
|
+ : '',
|
|
|
+ ),
|
|
|
+ ]),
|
|
|
+ type: 'error',
|
|
|
+ duration: 0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|