| | |
| | | import { debounce, decodeFormData } from './util'; |
| | | import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants'; |
| | | import { Local, LoginInfo, Session } from '/@/utils/storage'; |
| | | import { isSharePage } from '../stores/chatRoom'; |
| | | // import JSONbig from 'json-bigint'; |
| | | |
| | | //#region ====================== 后端 res.err_code ====================== |
| | |
| | | } |
| | | return !!accessSession; |
| | | }; |
| | | |
| | | export const SESSION_KEY = 'hswatersession'; |
| | | export const handleNoAuth = debounce(() => { |
| | | emitNoAuth(); |
| | | }); |
| | |
| | | const accessSession = Local.get(accessSessionKey); |
| | | if (accessSession) { |
| | | // 将 token 添加到请求报文头中 |
| | | config.headers['hswatersession'] = accessSession; |
| | | config.headers[SESSION_KEY] = accessSession; |
| | | } |
| | | if (!NO_AUTH_API_LIST.includes(config.url)) { |
| | | if (!NO_AUTH_API_LIST.includes(config.url) && !isSharePage.value) { |
| | | if (!accessSession && config.url !== LOGIN_URL && config.url !== TEL_LOGIN_URL) { |
| | | handleNoAuth(config.url); |
| | | throw '权限验证失败'; |
| | |
| | | if (!serveData.json_ok) { |
| | | switch (serveData?.err_code) { |
| | | case ErrorCode.Auth: |
| | | if (res.config.url !== LOGIN_URL && res.config.url !== TEL_LOGIN_URL) { |
| | | if (res.config.url !== LOGIN_URL && res.config.url !== TEL_LOGIN_URL && !isSharePage.value) { |
| | | handleNoAuth(); |
| | | throw '权限验证失败'; |
| | | } |
| | |
| | | }; |
| | | |
| | | const service = createAxiosInstance(); |
| | | |
| | | export const mainRequest = service; |
| | | |
| | | //#region ====================== 流响应数据 ====================== |
| | |
| | | * @description 域名前缀 |
| | | * 防止类似于 http://sqi.beng35.com/airp 和 http://sqi.beng35.com/test 公用同一个 token 或 userInfo |
| | | */ |
| | | export const getDomainPrefix = (win: Window) => { |
| | | export const getDomainPrefix = (win: Window = window) => { |
| | | const subDomainName = win.location.pathname |
| | | .split('/') |
| | | .filter((item) => !!item) |
| | |
| | | return domainPrefix; |
| | | }; |
| | | |
| | | export const domainPrefix = getDomainPrefix(window); |
| | | |
| | | // token 键定义 |
| | | export const sessionName = 'access-session'; |
| | | export const userName = 'userName'; |
| | | |
| | | export const getSessionKey = (win: Window) => { |
| | | return getDomainPrefix(win) + sessionName; |
| | | return sessionName; |
| | | }; |
| | | export const getUserNameKey = (win: Window) => { |
| | | return getDomainPrefix(win) + userName; |
| | | return userName; |
| | | }; |
| | | |
| | | export const accessSessionKey = getSessionKey(window); |
| | |
| | | export const refreshAccessTokenKey = `x-${accessSessionKey}`; |
| | | |
| | | // userInfo键定义 |
| | | export const userInfoKey = getDomainPrefix(window) + 'userInfo'; |
| | | |
| | | export const userInfoKey ='userInfo'; |
| | | // 获取 token |
| | | export const getSession = () => { |
| | | return Local.get(accessSessionKey); |