100 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| 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 => {
 | |
|     // 192.168.0.100:8080
 | |
| 
 | |
|     //#ifdef DEV_SERVER
 | |
|     console.log('DEV_SERVER')
 | |
|     config.baseURL = 'http://192.168.2.102:8080'
 | |
|     //#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) {
 | |
|         if (['/member/api/member/get-info'].includes(response?.config?.url)) {
 | |
|           uni.showToast({
 | |
|             title: '登录失败',
 | |
|             icon: 'none',
 | |
|             duration: 800,
 | |
|           })
 | |
|           setTimeout(() => {
 | |
|             uni.reLaunch({
 | |
|               url: '/pages/login/index',
 | |
|             })
 | |
|           }, 800)
 | |
|           removeToken()
 | |
|           return data
 | |
|         }
 | |
|         uni.$u.toast(data.msg)
 | |
|         return data
 | |
|       } else {
 | |
|         // uni.$u.toast(data.msg)
 | |
|         return data
 | |
|       }
 | |
|     },
 | |
|     response => {
 | |
|       uni.$u.toast('服务器错误,请稍后重试')
 | |
|     }
 | |
|   )
 | |
| }
 |