12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import { type CreateAxiosDefaults } from 'axios'
- import { Request, type RequestConfig, type RequestError } from '@jg_widgets/utils'
- import { Message } from 'element-ui'
- // import { PAGE_PATH, UN_AUTH_PATH } from '../../config'
- // import { userService } from '../index'
- import cache from '../../utils/cache'
- class App extends Request {
- constructor (config: CreateAxiosDefaults & RequestConfig) {
- const handleError = (nErr: RequestError, err: any) => {
- if (['401'].includes(nErr.code)) {
- // this.navigateReplace(`${PAGE_PATH.login}?redirect=${location.pathname}`)
- } else {
- Message.error(nErr.message)
- }
- }
- super(
- {
- loading: false,
- cancel: false,
- headers: {
- Authorization: 'Bearer ' + cache.session.get('token')
- },
- ...config
- },
- {
- handleError,
- addHeader: () => [
- // { key: 'token', value: userService.userInfo?.token ?? '', whitelist: UN_AUTH_PATH }
- ],
- responseInterceptor: response => {
- response.use(
- res => {
- console.log(res,'响应数据');
-
- if (['200', 0].includes(res?.data?.code)) {
- return Promise.resolve(res.data.data ?? null)
- }
- const err = new Error(res?.data?.msg || '')
- handleError(super.getError(err), err)
- return Promise.reject(err)
- },
- err => Promise.reject(err)
- )
- }
- }
- )
- }
- navigatePush (url: string) {
- url && (window.location.href = url)
- }
- navigateReplace (url: string) {
- url && window.location.replace(url)
- }
- getCache = (key: string, type: 'session' | 'local' = 'session') => {
- const data = type === 'session' ? sessionStorage.getItem(key) : localStorage.getItem(key)
- return data ? JSON.parse(data) : null
- }
- setCache = (key: string, value: any, type: 'session' | 'local' = 'session') => {
- if (!value) {
- type === 'session' ? sessionStorage.removeItem(key) : localStorage.removeItem(key)
- } else {
- type === 'session'
- ? sessionStorage.setItem(key, JSON.stringify(value))
- : localStorage.setItem(key, JSON.stringify(value))
- }
- }
- }
- export default App
|