bonus-material-app/src/utils/http.js

116 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useMemberStore } from '@/stores'
/**
* 添加拦截器
* 拦截 request 请求
* baseURL 设置请求ip地址和端口
*/
const ENV = process.env.NODE_ENV
// export const baseURL = ENV === 'development' ? 'http://192.168.0.244:18580' : 'http://192.168.0.244:18580'//测试
export const baseURL = ENV === 'development' ? 'http://sgwpdm.ah.sgcc.com.cn/iws/clz-api' : 'http://sgwpdm.ah.sgcc.com.cn/iws/clz-api'//生产
// export const baseURL = ENV === 'development' ? '/api' : '/iws/jiju-api'; // 宏源服务
// export const baseURL = ENV === 'development' ? 'http://192.168.0.234:18080' : '***'
// export const baseURL = ENV === 'development' ? 'http://192.168.0.96:18080' : 'http://sgwpdm.ah.sgcc.com.cn/iws/clz-api'//马
console.log('🚀 ~ baseURL:', baseURL)
// export const baseURL = ENV === 'development' ? '/api' : '***'
// **********OCR识别为NVUE文件页面请求URL需要同步配置**********
/**
* httpInterceptor 分别拦截 request 和 uploadFile 请求
*/
// 添加请求拦截
const httpInterceptor = {
invoke(options) {
// 1. 先判断请求 url 是否为完整的 http 请求路径 如果不是则拼接 baseURL
if (!options.url.startsWith('http')) {
options.url = baseURL + options.url
}
// 2. 设置请求超时时间默认为60s设置为 10s
options.timeout = 10000
// 3. 增加小程序端请求头标识
options.header = {
// 'source-client': 'mini',
...options.header,
}
// 4. 增加 token 请求头标识
const memberStore = useMemberStore()
const token = memberStore.token
// const token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjQ2NDdmYjlkLWI5OTItNDRiNy05MTdkLTMwZjg0ZjUxYzM5MCIsInVzZXJuYW1lIjoiYWRtaW4ifQ.9xM5bFhrmHK09-4ZgL5SS8WraNIJjIijuB-1P0lJF-n0KlVM5Bglvyjltk1NQbdqgi1hwRocZS1OU41cLiwuig"
if (token) {
options.header.Authorization = token
}
},
}
uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)
// 设置http请求
export const http = (options) => {
return new Promise((resolve, reject) => {
uni.request({
...options,
success(res) {
// console.log('res', res)
// 1. 判断是否请求成功
if (res.statusCode >= 200 && res.statusCode < 300) {
if (res.data.code >= 200 && res.data.code < 300) {
resolve(res.data)
} else if (res.data.code === 401) {
// 2. 401 表示token过期 去往登录页重新登录
uni.showToast({
icon: 'none',
title: `${res.data.msg}`,
})
const memberStore = useMemberStore()
memberStore.clearUserInfo()
memberStore.clearToken()
uni.navigateTo({
url: '/pages/login/index',
})
reject(res)
} else if (res.data.code === 500) {
uni.showToast({
icon: 'none',
title: `${res.data.msg}`,
})
}
} else if (res.statusCode === 401) {
// 2. 401 表示token过期 去往登录页重新登录
const memberStore = useMemberStore()
memberStore.clearUserInfo()
memberStore.clearToken()
uni.navigateTo({
url: '/pages/login/index',
})
reject(res)
} else {
// 3. 其他错误
uni.showToast({
icon: 'none',
title: '请求错误',
})
reject(res)
}
// console.log(res)
},
fail(err) {
uni.showToast({
icon: 'none',
title: '请求失败',
})
console.log(err, '请求失败')
reject(err)
},
})
})
}