|  | @@ -3,10 +3,12 @@
 | 
	
		
			
				|  |  |   * 更详细的 api 文档: https://github.com/umijs/umi-request
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  import { extend } from 'umi-request'
 | 
	
		
			
				|  |  | -import { notification } from 'antd'
 | 
	
		
			
				|  |  | -import { getTokenid } from './common'
 | 
	
		
			
				|  |  | +import { message, notification } from 'antd'
 | 
	
		
			
				|  |  | +import { getTokenid, getUserAccount, redirectToLogin, saveTokenId } from './common'
 | 
	
		
			
				|  |  |  import consts from '@/consts'
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +const RETRY_TOKEN_TIME = 3 // 重新获取token次数
 | 
	
		
			
				|  |  | +let currentTokenRetry = RETRY_TOKEN_TIME // 当前token刷新次数
 | 
	
		
			
				|  |  |  const codeMessage = {
 | 
	
		
			
				|  |  |    200: '服务器成功返回请求的数据。',
 | 
	
		
			
				|  |  |    201: '新建或修改数据成功。',
 | 
	
	
		
			
				|  | @@ -47,6 +49,16 @@ const errorHandler = error => {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    return response
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * 获取token令牌
 | 
	
		
			
				|  |  | + * @param {*} account 账号
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +async function apiGetToken(account) {
 | 
	
		
			
				|  |  | +  const data = await request.get(consts.TOKEN_API, {})
 | 
	
		
			
				|  |  | +  return data
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * 配置request请求时的默认参数
 | 
	
		
			
				|  |  |   */
 | 
	
	
		
			
				|  | @@ -56,15 +68,60 @@ const request = extend({
 | 
	
		
			
				|  |  |    // 默认错误处理
 | 
	
		
			
				|  |  |    credentials: 'include' // 默认请求是否带上cookie
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  | -// 请求前拦截器
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// 请求拦截器
 | 
	
		
			
				|  |  |  request.interceptors.request.use((url, options) => {
 | 
	
		
			
				|  |  |    const token = getTokenid()
 | 
	
		
			
				|  |  | -  // if (!token && )
 | 
	
		
			
				|  |  | -  console.log(url, options)
 | 
	
		
			
				|  |  | +  if (token) {
 | 
	
		
			
				|  |  | +    // 在白名单里的请求放过
 | 
	
		
			
				|  |  | +    if (consts.TOKEN_WHITE_LIST.includes(url)) {
 | 
	
		
			
				|  |  | +      return { url, options }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    // eslint-disable-next-line no-param-reassign
 | 
	
		
			
				|  |  | +    options.headers[consts.TOKEN_KEY] = token
 | 
	
		
			
				|  |  | +    return { url, options }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -request.interceptors.response.use((response, options) => {
 | 
	
		
			
				|  |  | -  console.log(response, options)
 | 
	
		
			
				|  |  | +const refreshTokenIfNeed = async (response, config) => {
 | 
	
		
			
				|  |  | +  if (
 | 
	
		
			
				|  |  | +    response &&
 | 
	
		
			
				|  |  | +    response.data &&
 | 
	
		
			
				|  |  | +    response.data.code &&
 | 
	
		
			
				|  |  | +    consts.TOKEN_INVALID_CODE.includes(response.data.code)
 | 
	
		
			
				|  |  | +  ) {
 | 
	
		
			
				|  |  | +    const account = getUserAccount()
 | 
	
		
			
				|  |  | +    if (!account || !currentTokenRetry) {
 | 
	
		
			
				|  |  | +      redirectToLogin()
 | 
	
		
			
				|  |  | +      return response
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    currentTokenRetry -= 1
 | 
	
		
			
				|  |  | +    // token过期或者没有传token
 | 
	
		
			
				|  |  | +    const data = await apiGetToken(account)
 | 
	
		
			
				|  |  | +    if (data && data.data && data.data.code === consts.RET_CODE.SUCCESS) {
 | 
	
		
			
				|  |  | +      currentTokenRetry = RETRY_TOKEN_TIME // 重置token重试次数
 | 
	
		
			
				|  |  | +      // 刷新token成功,保存token
 | 
	
		
			
				|  |  | +      const token = data.data && data.data.token
 | 
	
		
			
				|  |  | +      saveTokenId(token)
 | 
	
		
			
				|  |  | +      // 重新发起请求
 | 
	
		
			
				|  |  | +      const newResponse = await request(response.url, config)
 | 
	
		
			
				|  |  | +      return newResponse
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  return response
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// 响应拦截器
 | 
	
		
			
				|  |  | +request.interceptors.response.use(async (response, options) => {
 | 
	
		
			
				|  |  | +  if (
 | 
	
		
			
				|  |  | +    response &&
 | 
	
		
			
				|  |  | +    response.data &&
 | 
	
		
			
				|  |  | +    response.data.code !== consts.RET_CODE.SUCCESS &&
 | 
	
		
			
				|  |  | +    !consts.TOKEN_INVALID_CODE.includes(response.data.code)
 | 
	
		
			
				|  |  | +  ) {
 | 
	
		
			
				|  |  | +    message.error((response.data && response.data.msg) || '请求错误!')
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  return refreshTokenIfNeed(response, options)
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  export default request
 |