| | |
| | | import type { AxiosInstance, AxiosRequestConfig } from 'axios'; |
| | | import axios from 'axios'; |
| | | import { ElMessage } from 'element-plus'; |
| | | import { NO_AUTH_API_LIST } from '../api/ai/chat'; |
| | | import { LOGIN_URL, TEL_LOGIN_URL } from '../api/ai/user'; |
| | | import emitter from './mitt'; |
| | | import { debounce } from './util'; |
| | | import { debounce, decodeFormData } from './util'; |
| | | import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants'; |
| | | import { Local, LoginInfo, Session } from '/@/utils/storage'; |
| | | import { Logger } from '../model/logger/Logger'; |
| | | // import JSONbig from 'json-bigint'; |
| | | |
| | | //#region ====================== 后端 res.err_code ====================== |
| | | export const enum ErrorCode { |
| | | /** @description 权限验证失败 */ |
| | | Message = 'MESSAGE', |
| | | /** @description 内部错误 */ |
| | | Exception = 'EXCEPTION', |
| | | /** @description 无权使用 */ |
| | | Auth = 'AUTH', |
| | | } |
| | | //#endregion |
| | | |
| | | const handleNoAuth = debounce(() => { |
| | | const emitNoAuth = () => { |
| | | emitter.emit('logout'); |
| | | emitter.emit('openLoginDlg'); |
| | | }; |
| | | |
| | | export const handleNormalAuth = () => { |
| | | const accessSession = Local.get(accessSessionKey); |
| | | if (!accessSession) { |
| | | emitter.emit('logout'); |
| | | emitter.emit('openLoginDlg'); |
| | | } |
| | | return !!accessSession; |
| | | }; |
| | | |
| | | export const handleNoAuth = debounce(() => { |
| | | emitNoAuth(); |
| | | }); |
| | | const loginUrl = '/login'; |
| | | const initRequestInterceptor = (request: AxiosInstance) => { |
| | | // 添加请求拦截器 |
| | | request.interceptors.request.use( |
| | | (config) => { |
| | | // 获取本地的 token |
| | | const accessSession = Local.get(accessSessionKey); |
| | | |
| | | if (accessSession) { |
| | | // 将 token 添加到请求报文头中 |
| | | config.headers['hswatersession'] = accessSession; |
| | | } else { |
| | | if (config.url !== loginUrl) { |
| | | } |
| | | if (!NO_AUTH_API_LIST.includes(config.url)) { |
| | | if (!accessSession && config.url !== LOGIN_URL && config.url !== TEL_LOGIN_URL) { |
| | | handleNoAuth(config.url); |
| | | |
| | | throw '权限验证失败'; |
| | | } |
| | | } |
| | | |
| | | return config; |
| | | }, |
| | | (error) => { |
| | |
| | | if (!serveData.json_ok) { |
| | | switch (serveData?.err_code) { |
| | | case ErrorCode.Auth: |
| | | if (res.config.url !== loginUrl) { |
| | | if (res.config.url !== LOGIN_URL && res.config.url !== TEL_LOGIN_URL) { |
| | | handleNoAuth(); |
| | | throw '权限验证失败'; |
| | | } |
| | | break; |
| | | case ErrorCode.Exception: |
| | | const param = res.config.data ? `\n 请求参数:${JSON.stringify(decodeFormData(res.config.data))}\n` : ''; |
| | | ElMessage.error('内部错误!'); |
| | | Logger.error(`${res.config.url} 响应失败${param}`, serveData?.json_msg && new Error(serveData?.json_msg)); |
| | | return res.data; |
| | | } |
| | | // const msg = serveData.json_msg ?? ''; |
| | | |
| | | // const error = serveData?.err_code ? `${msg ? `【${serveData.err_code}】` : serveData.err_code}` : ''; |
| | | // const tip = error + msg || '请求失败'; |
| | | // ElMessage.error(tip); |
| | | // const url = res.request.responseURL; |
| | | // throw new Error(url + '\n' + tip); |
| | | } |
| | | return res.data; |
| | | }, |
| | | (error) => { |
| | | if (typeof error === 'string') { |
| | | // ElMessage.error(error); |
| | | return Promise.reject(error); |
| | | } |
| | | // 处理响应错误 |
| | | if (error.response) { |
| | | if (error.response.status === 401) { |