Ai_GirlFriend/xuniYou/utils/request.js

75 lines
2.4 KiB
JavaScript
Raw Normal View History

2026-02-02 20:08:28 +08:00
// Windows 本地开发 - 混合架构
2026-02-05 11:28:34 +08:00
// export const baseURL = 'http://192.168.1.164:30100' // PHP 处理用户管理和界面
export const baseURL = 'http://1.15.149.240:30100' // PHP 处理用户管理和界面
// export const baseURLPy = 'http://192.168.1.164:30101' // FastAPI 处理 AI 功能
export const baseURLPy = 'http://1.15.149.240:30101' // FastAPI 处理 AI 功能
2026-02-01 10:56:35 +08:00
2026-02-02 20:08:28 +08:00
// 远程服务器 - 需要时取消注释
// export const baseURL = 'http://1.15.149.240:30100'
// export const baseURLPy = 'http://1.15.149.240:30100/api'
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")) : ""),
},
success: (res) => {
2026-02-02 20:08:28 +08:00
console.log('请求成功:', url_base + options.url, '状态码:', res.statusCode, '数据:', res.data);
2026-01-31 19:15:41 +08:00
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-02 20:08:28 +08:00
console.log('请求失败:', url_base + options.url, '错误:', 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
}
})
})
}