93 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
/*
 | 
						|
 * @Descripttion:
 | 
						|
 * @version:
 | 
						|
 * @Author: kBank
 | 
						|
 * @Date: 2022-05-24 14:43:45
 | 
						|
 */
 | 
						|
import { getToken, removeToken } from '@/config/auth.js'
 | 
						|
 | 
						|
module.exports = vm => {
 | 
						|
  // 检查uni.$u.http是否可用
 | 
						|
  if (!uni || !uni.$u || !uni.$u.http) {
 | 
						|
    console.warn('uni.$u.http is not available, request configuration skipped')
 | 
						|
    return
 | 
						|
  }
 | 
						|
 | 
						|
  // 初始化请求配置
 | 
						|
  uni.$u.http.setConfig(config => {
 | 
						|
    // config.baseURL = 'https://p1.hzs413.com/inter-api';
 | 
						|
    // 192.168.0.100:8080
 | 
						|
 | 
						|
    //#ifdef DEV_SERVER
 | 
						|
    console.log('DEV_SERVER')
 | 
						|
    config.baseURL = 'http://t-app.beida777.com/prod-api'
 | 
						|
    //#endif
 | 
						|
 | 
						|
    //#ifdef QA_SERVER
 | 
						|
    console.log('QA_SERVER')
 | 
						|
    config.baseURL = '/prod-api'
 | 
						|
    //#endif
 | 
						|
    config.timeout = 30000
 | 
						|
    return config
 | 
						|
  })
 | 
						|
 | 
						|
  // 请求拦截
 | 
						|
  uni.$u.http.interceptors.request.use(
 | 
						|
    config => {
 | 
						|
      config.data = config.data || {}
 | 
						|
      config.header['Source'] = 3
 | 
						|
      let token = getToken()
 | 
						|
      let mToken = uni.getStorageSync('mToken')
 | 
						|
      let lang = uni.getStorageSync('lang')
 | 
						|
      if (lang) {
 | 
						|
        config.header['Accept-Language'] = lang
 | 
						|
      } else {
 | 
						|
        config.header['Accept-Language'] = 'zh-CN'
 | 
						|
      }
 | 
						|
      if (token) {
 | 
						|
        config.header['Authorization'] = token
 | 
						|
      }
 | 
						|
      config.header['token'] = mToken || ''
 | 
						|
      return config
 | 
						|
    },
 | 
						|
    config => {
 | 
						|
      return Promise.reject(config)
 | 
						|
    }
 | 
						|
  )
 | 
						|
 | 
						|
  // 响应拦截
 | 
						|
  uni.$u.http.interceptors.response.use(
 | 
						|
    response => {
 | 
						|
      const data = response.data
 | 
						|
      // uni.$u.toast(data.msg)
 | 
						|
      // if (data.code == 200) {
 | 
						|
      //   return data;
 | 
						|
      // } else {
 | 
						|
      // token过期,清除token重新获取
 | 
						|
      if (data.code == 400 || data.code == 401 || data.code == 402) {
 | 
						|
        // uni.showToast({
 | 
						|
        //   title: '登录超时',
 | 
						|
        //   icon: 'none',
 | 
						|
        //   duration: 1500,
 | 
						|
        // })
 | 
						|
        removeToken()
 | 
						|
        // 跳转到登录
 | 
						|
        setTimeout(() => {
 | 
						|
          uni.reLaunch({
 | 
						|
            url: '/pages/login/index',
 | 
						|
          })
 | 
						|
        }, 1500)
 | 
						|
      } else if (data.code == 500) {
 | 
						|
        uni.$u.toast(data.msg)
 | 
						|
        return data
 | 
						|
      } else {
 | 
						|
        // uni.$u.toast(data.msg)
 | 
						|
        return data
 | 
						|
      }
 | 
						|
    },
 | 
						|
    response => {
 | 
						|
      uni.$u.toast('服务器错误,请稍后重试')
 | 
						|
    }
 | 
						|
  )
 | 
						|
}
 |