Ai_GirlFriend/xuniYou/utils/request.js

73 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Windows 本地开发 - 混合架构
export const baseURL = 'http://192.168.1.164:30100' // PHP 处理用户管理和界面
export const baseURLPy = 'http://192.168.1.164:30101' // FastAPI 处理 AI 功能
// 远程服务器 - 需要时取消注释
// export const baseURL = 'http://1.15.149.240:30100'
// export const baseURLPy = 'http://1.15.149.240:30100/api'
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) => {
console.log('请求成功:', url_base + options.url, '状态码:', res.statusCode, '数据:', res.data);
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('请求失败:', url_base + options.url, '错误:', err);
uni.hideLoading(); // 添加这行确保请求失败时也隐藏loading
console.log('接口错误',err);
uni.showToast({
icon: "none",
duration: 3000,
title: "网络请求失败,请检查网络连接"
});
reject(err);
}
})
})
}