2024-11-18 09:05:38 +08:00
|
|
|
|
import { useMemberStore } from '@/stores'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加拦截器
|
|
|
|
|
|
* 拦截 request 请求
|
|
|
|
|
|
* baseURL 设置请求ip地址和端口
|
|
|
|
|
|
*/
|
2024-11-19 09:30:15 +08:00
|
|
|
|
const ENV = process.env.NODE_ENV
|
2025-01-14 10:02:12 +08:00
|
|
|
|
// export const baseURL = ENV === 'development' ? 'http://192.168.0.244:18580' : 'http://192.168.0.244:18580'//测试
|
2025-05-16 09:07:40 +08:00
|
|
|
|
// export const baseURL = ENV === 'development' ? 'http://sgwpdm.ah.sgcc.com.cn/iws/jiju-api/' : 'http://sgwpdm.ah.sgcc.com.cn/iws/jiju-api/'//生产
|
2025-06-23 13:57:08 +08:00
|
|
|
|
export const baseURL = ENV === 'development' ? 'http://192.168.1.114:18080' : '/iws/jiju-api'; // 宏源服务
|
2025-04-30 17:06:33 +08:00
|
|
|
|
// export const baseURL = ENV === 'development' ? 'http://192.168.0.234:18080' : '***'
|
2025-06-23 13:57:08 +08:00
|
|
|
|
// export const baseURL = ENV === 'development' ? 'http://192.168.1.114:18080' : 'http://192.168.1.117:18080'//马
|
2025-01-13 10:04:19 +08:00
|
|
|
|
// export const baseURL = ENV === 'development' ? '/api' : '***'
|
2025-01-14 17:56:14 +08:00
|
|
|
|
|
|
|
|
|
|
// **********OCR识别为NVUE文件页面请求URL需要同步配置**********
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 09:05:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* httpInterceptor 分别拦截 request 和 uploadFile 请求
|
2025-01-14 17:56:14 +08:00
|
|
|
|
*/
|
2024-11-18 09:05:38 +08:00
|
|
|
|
// 添加请求拦截
|
|
|
|
|
|
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,
|
|
|
|
|
|
}
|
2024-11-18 18:28:06 +08:00
|
|
|
|
|
2024-11-18 09:05:38 +08:00
|
|
|
|
// 4. 增加 token 请求头标识
|
|
|
|
|
|
const memberStore = useMemberStore()
|
2024-11-18 18:28:06 +08:00
|
|
|
|
const token = memberStore.token
|
2024-11-19 09:30:15 +08:00
|
|
|
|
|
2024-11-18 18:28:06 +08:00
|
|
|
|
// const token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjQ2NDdmYjlkLWI5OTItNDRiNy05MTdkLTMwZjg0ZjUxYzM5MCIsInVzZXJuYW1lIjoiYWRtaW4ifQ.9xM5bFhrmHK09-4ZgL5SS8WraNIJjIijuB-1P0lJF-n0KlVM5Bglvyjltk1NQbdqgi1hwRocZS1OU41cLiwuig"
|
2024-11-18 09:05:38 +08:00
|
|
|
|
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) {
|
2025-06-21 18:34:05 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 1. 判断是否请求成功
|
|
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
|
if (res.data && res.data.code >= 200 && res.data.code < 300) {
|
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
|
} else if (res.data && res.data.code === 401) {
|
|
|
|
|
|
// 2. 401 表示token过期 去往登录页重新登录
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: `${res.data.msg}`,
|
|
|
|
|
|
})
|
|
|
|
|
|
const memberStore = useMemberStore()
|
|
|
|
|
|
memberStore.clearUserInfo()
|
|
|
|
|
|
memberStore.clearToken()
|
|
|
|
|
|
const pages = getCurrentPages()
|
|
|
|
|
|
const currentPage = pages[pages.length - 1]
|
|
|
|
|
|
if (!currentPage.route.includes('login')) {
|
|
|
|
|
|
uni.reLaunch({
|
|
|
|
|
|
url: '/pages/login/index',
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
reject(res)
|
|
|
|
|
|
} else if (res.data && res.data.code === 500) {
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: `${res.data.msg}`,
|
|
|
|
|
|
})
|
|
|
|
|
|
reject(res)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
title: `${res.data && res.data.msg ? res.data.msg : '请求失败'}`,
|
|
|
|
|
|
})
|
|
|
|
|
|
reject(res)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (res.statusCode === 401) {
|
2024-11-19 11:20:59 +08:00
|
|
|
|
// 2. 401 表示token过期 去往登录页重新登录
|
|
|
|
|
|
const memberStore = useMemberStore()
|
|
|
|
|
|
memberStore.clearUserInfo()
|
|
|
|
|
|
memberStore.clearToken()
|
2025-06-21 18:34:05 +08:00
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: '/pages/login/index',
|
2024-11-20 17:41:05 +08:00
|
|
|
|
})
|
2025-06-21 18:34:05 +08:00
|
|
|
|
reject(res)
|
2025-06-21 10:50:53 +08:00
|
|
|
|
} else {
|
2025-06-21 18:34:05 +08:00
|
|
|
|
// 3. 其他错误
|
2025-06-21 10:50:53 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
2025-06-21 18:34:05 +08:00
|
|
|
|
title: '请求错误',
|
2025-06-21 10:50:53 +08:00
|
|
|
|
})
|
2025-06-21 18:34:05 +08:00
|
|
|
|
reject(res)
|
2024-11-20 17:41:05 +08:00
|
|
|
|
}
|
2025-06-21 18:34:05 +08:00
|
|
|
|
} catch (err) {
|
2024-11-19 09:30:15 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
2025-06-21 18:34:05 +08:00
|
|
|
|
title: '请求失败',
|
2024-11-19 09:30:15 +08:00
|
|
|
|
})
|
2025-06-21 18:34:05 +08:00
|
|
|
|
reject(err)
|
2024-11-18 09:05:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
2024-11-19 09:30:15 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
icon: 'none',
|
2024-11-19 14:52:39 +08:00
|
|
|
|
title: '请求失败',
|
2024-11-19 09:30:15 +08:00
|
|
|
|
})
|
2024-11-18 09:05:38 +08:00
|
|
|
|
console.log(err, '请求失败')
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|