12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import { extend } from 'umi-request'
- import cleanDeep from 'clean-deep'
- import { browser, getAuth, removeAuth } from '@/helpers/utils'
- import { postMessage } from './native-message'
- import { ElMessage } from 'element-plus'
- export interface SearchInitParams {
- rows?: string | number
- page?: string | number
- }
- const request = extend({
- // requestType: 'form',
- timeout: 20000,
- timeoutMessage: '请求超时'
- })
- // request.use(async (ctx, next) => {
- // const { url, options } = ctx.req
- // const prefix = options.prefix || '';
- // const baseUrl: string = url.replace(prefix, '') || '';
- // const linkUrl: string = (ApiRouter as any)[baseUrl];
- // if (linkUrl) {
- // ctx.req.url = prefix + linkUrl;
- // }
- // await next();
- // })
- // 是否是初始化接口
- let initRequest = false
- request.interceptors.request.use(
- (url, options: any) => {
- // console.log(url)
- initRequest = options.initRequest || false
- const Authorization = getAuth() || ''
- const authHeaders: any = {}
- //console.log(/open/gi.test(url))
- if (
- Authorization &&
- ![
- '/api-auth/usernameLogin',
- '/api-auth/smsLogin',
- '/api-auth/code/sendSms'
- ].includes(url) /*&&
- !/open/gi.test(url)*/
- ) {
- authHeaders.Authorization = Authorization
- }
- return {
- url,
- options: {
- ...options,
- params: cleanDeep(options.params),
- headers: {
- ...options.headers,
- ...authHeaders
- }
- }
- }
- },
- { global: false }
- )
- request.interceptors.response.use(
- async res => {
- if (res.status > 299 || res.status < 200) {
- const msg = '服务器错误,状态码' + res.status
- ElMessage.error(msg)
- throw new Error(msg)
- }
- const data = await res.clone().json()
- if (data.code !== 200 && data.errCode !== 0) {
- const msg = data.msg || data.message || '处理失败,请重试'
- if (data.code === 403 || data.code === 401) {
- // window.location.href = location.origin
- // ElMessage.error(msg)
- }
- if (!(data.code === 403 || data.code === 401)) {
- ElMessage.error(msg)
- }
- if (data.code === 403) {
- removeAuth()
- window.location.href = location.origin
- ElMessage.error('登录已过期,请重新登录')
- }
- throw new Error(msg)
- }
- return res
- },
- { global: false }
- )
- export default request
|