|
| 1 | +import { createAlovaRequest } from '@sa/alova'; |
| 2 | +import { createAlovaMockAdapter } from '@sa/alova/mock'; |
| 3 | +import adapterFetch from '@sa/alova/fetch'; |
| 4 | +import { useAuthStore } from '@/store/modules/auth'; |
| 5 | +import { $t } from '@/locales'; |
| 6 | +import { getServiceBaseURL } from '@/utils/service'; |
| 7 | +import featureUsers20241014 from '../mocks/feature-users-20241014'; |
| 8 | +import { getAuthorization, handleRefreshToken, showErrorMsg } from './shared'; |
| 9 | +import type { RequestInstanceState } from './type'; |
| 10 | + |
| 11 | +const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y'; |
| 12 | +const { baseURL } = getServiceBaseURL(import.meta.env, isHttpProxy); |
| 13 | + |
| 14 | +const state: RequestInstanceState = { |
| 15 | + errMsgStack: [] |
| 16 | +}; |
| 17 | +const mockAdapter = createAlovaMockAdapter([featureUsers20241014], { |
| 18 | + // using requestAdapter if not match mock request |
| 19 | + httpAdapter: adapterFetch(), |
| 20 | + |
| 21 | + // response delay time |
| 22 | + delay: 1000, |
| 23 | + |
| 24 | + // global mock toggle |
| 25 | + enable: true, |
| 26 | + matchMode: 'methodurl' |
| 27 | +}); |
| 28 | +export const alova = createAlovaRequest( |
| 29 | + { |
| 30 | + baseURL, |
| 31 | + requestAdapter: import.meta.env.DEV ? mockAdapter : adapterFetch() |
| 32 | + }, |
| 33 | + { |
| 34 | + onRequest({ config }) { |
| 35 | + const Authorization = getAuthorization(); |
| 36 | + config.headers.Authorization = Authorization; |
| 37 | + config.headers.apifoxToken = 'XL299LiMEDZ0H5h3A29PxwQXdMJqWyY2'; |
| 38 | + }, |
| 39 | + tokenRefresher: { |
| 40 | + async isExpired(response) { |
| 41 | + const expiredTokenCodes = import.meta.env.VITE_SERVICE_EXPIRED_TOKEN_CODES?.split(',') || []; |
| 42 | + const { code } = await response.clone().json(); |
| 43 | + return expiredTokenCodes.includes(String(code)); |
| 44 | + }, |
| 45 | + async handler() { |
| 46 | + await handleRefreshToken(); |
| 47 | + } |
| 48 | + }, |
| 49 | + async isBackendSuccess(response) { |
| 50 | + // when the backend response code is "0000"(default), it means the request is success |
| 51 | + // to change this logic by yourself, you can modify the `VITE_SERVICE_SUCCESS_CODE` in `.env` file |
| 52 | + const resp = response.clone(); |
| 53 | + const data = await resp.json(); |
| 54 | + return String(data.code) === import.meta.env.VITE_SERVICE_SUCCESS_CODE; |
| 55 | + }, |
| 56 | + async transformBackendResponse(response) { |
| 57 | + return (await response.clone().json()).data; |
| 58 | + }, |
| 59 | + async onError(error, response) { |
| 60 | + const authStore = useAuthStore(); |
| 61 | + |
| 62 | + let message = error.message; |
| 63 | + let responseCode = ''; |
| 64 | + if (response) { |
| 65 | + const data = await response?.clone().json(); |
| 66 | + message = data.msg; |
| 67 | + responseCode = String(data.code); |
| 68 | + } |
| 69 | + |
| 70 | + function handleLogout() { |
| 71 | + showErrorMsg(state, message); |
| 72 | + authStore.resetStore(); |
| 73 | + } |
| 74 | + |
| 75 | + function logoutAndCleanup() { |
| 76 | + handleLogout(); |
| 77 | + window.removeEventListener('beforeunload', handleLogout); |
| 78 | + state.errMsgStack = state.errMsgStack.filter(msg => msg !== message); |
| 79 | + } |
| 80 | + |
| 81 | + // when the backend response code is in `logoutCodes`, it means the user will be logged out and redirected to login page |
| 82 | + const logoutCodes = import.meta.env.VITE_SERVICE_LOGOUT_CODES?.split(',') || []; |
| 83 | + if (logoutCodes.includes(responseCode)) { |
| 84 | + handleLogout(); |
| 85 | + throw error; |
| 86 | + } |
| 87 | + |
| 88 | + // when the backend response code is in `modalLogoutCodes`, it means the user will be logged out by displaying a modal |
| 89 | + const modalLogoutCodes = import.meta.env.VITE_SERVICE_MODAL_LOGOUT_CODES?.split(',') || []; |
| 90 | + if (modalLogoutCodes.includes(responseCode) && !state.errMsgStack?.includes(message)) { |
| 91 | + state.errMsgStack = [...(state.errMsgStack || []), message]; |
| 92 | + |
| 93 | + // prevent the user from refreshing the page |
| 94 | + window.addEventListener('beforeunload', handleLogout); |
| 95 | + |
| 96 | + window.$dialog?.error({ |
| 97 | + title: $t('common.error'), |
| 98 | + content: message, |
| 99 | + positiveText: $t('common.confirm'), |
| 100 | + maskClosable: false, |
| 101 | + closeOnEsc: false, |
| 102 | + onPositiveClick() { |
| 103 | + logoutAndCleanup(); |
| 104 | + }, |
| 105 | + onClose() { |
| 106 | + logoutAndCleanup(); |
| 107 | + } |
| 108 | + }); |
| 109 | + throw error; |
| 110 | + } |
| 111 | + showErrorMsg(state, message); |
| 112 | + throw error; |
| 113 | + } |
| 114 | + } |
| 115 | +); |
0 commit comments