65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
import {} from "@/store"
|
||
|
||
const baseURL = "http://192.168.0.180:3000"
|
||
import { useUserStore } from '@/store';
|
||
|
||
// 请求拦截器配置
|
||
const httpInterceptor = {
|
||
invoke(options) {
|
||
// 拼接请求地址
|
||
options.url = baseURL + options.url
|
||
console.log(111, options);
|
||
// 请求超时
|
||
options.timeout = 5000
|
||
|
||
// 添加 token 请求头标识
|
||
// const useUser = useUserStore()
|
||
// const token = useUser
|
||
// if (token) {
|
||
// options.header.Authorization = token
|
||
// }
|
||
},
|
||
}
|
||
|
||
// 拦截 request 请求
|
||
uni.addInterceptor("request", httpInterceptor)
|
||
|
||
/*
|
||
请求函数
|
||
*/
|
||
export const http = (options) => {
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
...options,
|
||
// 响应成功
|
||
success(res) {
|
||
// 请求成功
|
||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||
//提取数据
|
||
resolve(res.data)
|
||
} else if (res.statusCode === 401) {
|
||
// 401错误,清理用户信息,跳转到登录页
|
||
const memberStore = useMemberStore()
|
||
memberStore.clearProfile()
|
||
uni.navigateTo({ url: '/pages/login/login' })
|
||
reject(res)
|
||
} else {
|
||
// 其他错误,根据后端错误信息轻提示
|
||
uni.showToast({
|
||
icon: 'none',
|
||
title: (res.data).msg || '请求错误',
|
||
})
|
||
reject(res)
|
||
}
|
||
},
|
||
// 响应失败
|
||
fail(err) {
|
||
uni.showToast({
|
||
icon: 'none',
|
||
title: '网络错误',
|
||
})
|
||
reject(err)
|
||
},
|
||
})
|
||
})
|
||
} |