Ai_GirlFriend/xuniYou/utils/request.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2026-02-01 10:56:35 +08:00
// 本地开发 - 电脑浏览器调试使用
2026-01-31 19:15:41 +08:00
export const baseURL = 'http://127.0.0.1:8080'
export const baseURLPy = 'http://127.0.0.1:8000'
2026-02-01 10:56:35 +08:00
// 开发环境 - 手机端调试使用局域网IP需要时取消注释
// export const baseURL = 'http://192.168.1.164:8080'
// export const baseURLPy = 'http://192.168.1.164:8000'
2026-01-31 19:15:41 +08:00
export const sid = 2
export const request = (options, url_type = 1, isShowLoad = true) => {
return new Promise((resolve, reject) => {
if(isShowLoad){
uni.showLoading({
title: '加载中',
mask: true
});
}
let url_base = baseURL
if (url_type == 2) {
url_base = baseURLPy
}
uni.request({
url: url_base + options.url, //接口地址:前缀+方法中传入的地址
method: options.method || 'GET', //请求方法传入的方法或者默认是“GET”
data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
timeout: 200000,
header: {
'token': uni.getStorageSync("token") || "", //自定义请求头信息
'sid': sid,
'Authorization': (uni.getStorageSync("token") ? ('Bearer ' + uni.getStorageSync("token")) : ""),
'X-User-Id': '84' // 开发环境调试用
},
success: (res) => {
uni.hideLoading()
if (res.data.code == 1) {
resolve(res.data)
} else {
console.log('接口操作失败',res)
if (res.data.code == 0) {
resolve(res.data)
} else if (res.data.code == 401) {
uni.showToast({
icon: "none",
duration: 5000,
title: res.data.msg || "操作失败"
})
uni.reLaunch({
url: '/pages/login/index'
})
} else {
uni.showToast({
icon: "none",
duration: 5000,
title: res.data.msg || "操作失败"
})
}
}
},
fail: (err) => {
2026-02-01 10:56:35 +08:00
uni.hideLoading(); // 添加这行确保请求失败时也隐藏loading
console.log('接口错误',err);
uni.showToast({
icon: "none",
duration: 3000,
title: "网络请求失败,请检查网络连接"
});
reject(err);
2026-01-31 19:15:41 +08:00
}
})
})
}