Ai_GirlFriend/xuniYou/utils/request.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

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'
// export const baseURL = 'http://192.168.1.178:8000'
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) => {
console.log('接口错误',err)
reject(err)
}
})
})
}