cxc-szcx-uniapp/utils/http.js
2025-03-12 21:45:06 +08:00

112 lines
2.5 KiB
JavaScript

let baseUrl = import.meta.env.VITE_REQUEST_BASE_URL + '/jeecg-boot'
let loading = false
export function https(config) {
//显示loading
// uni.showLoading({
// title: '加载中...'
// });
if (loading) return
if (uni.getStorageSync('logintime') && uni.getStorageSync('logintime') + 3600000 <= Date.now()) {
loading = true
console.log('token超时');
uni.removeStorageSync('logintime')
uni.navigateTo({
url: '/pages/login/login'
})
loading = false
// return uni.navigateTo({
// url: '/pages/login/login'
// })
// uni.showModal({
// title: '登录',
// content: '请点击确认前往登录',
// success(res) {
// if (res.confirm) {
// uni.removeStorageSync('logintime')
// uni.navigateTo({
// url: '/pages/login/login'
// })
// }
// },
// complete() {
// loading = false
// }
// })
return
}
config.url = baseUrl + config.url; // 请求地址
let token = uni.getStorageSync('token') || '';
config.header = {
//返回数据类型
"content-type": 'application/json;charset=utf-8',
//设置用户访问的token信息
"X-Access-Token": token
}
let promise = new Promise(function(resolve, reject) {
uni.request(config).then(res => {
wx.hideLoading() //隐藏loading
if (res[0]) {
uni.showToast({
title: "数据获取失败",
icon: "none",
duration: 1500
})
resolve(false);
} else {
let data = res.data;
resolve(data);
if (loading) return
if (data.code == 500) {
uni.showToast({
title: data.message,
icon: "none",
duration: 1500
});
}
if (data.code == 510) {
loading = true
uni.showToast({
title: data.message,
icon: "none",
duration: 1500
});
uni.removeStorageSync('token')
uni.removeStorageSync('user')
uni.removeStorageSync('role')
uni.navigateTo({
url: '/pages/login/login'
})
uni.removeStorageSync('logintime')
loading = false
// setTimeout(() => {
// uni.showModal({
// title: '登录',
// content: '请点击确认前往登录',
// success(res) {
// if (res.confirm) {
// uni.navigateTo({
// url: '/pages/login/login'
// })
// }
// },
// complete() {
// loading = false
// }
// })
// loading = false
// }, 2000)
}
}
}).catch(error => {
uni.hideLoading() //隐藏loading
reject(error);
// uni.$showMsg('接口错误')
})
})
return promise
}