Files
device-voice-h5/utils/request.js
sexygoat 8dc8600b52
All checks were successful
构建并部署前端到生产环境 / build-and-deploy (push) Successful in 46s
fix: 弹窗提示
2026-04-23 16:49:24 +08:00

52 lines
1.1 KiB
JavaScript

import { BASE_URL } from './env.js';
const request = (options) => {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token') || '';
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : '',
...options.header
},
success: (res) => {
if (res.statusCode === 401) {
uni.removeStorageSync('token');
uni.removeStorageSync('identifier');
uni.showToast({
title: '登录已过期,请重新登录',
icon: 'none',
duration: 2000
});
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/login' });
}, 2000);
reject(res.data);
return;
}
const { code, msg, data } = res.data;
if (code === 0 || code === 200) {
resolve(data);
} else {
reject(res.data);
}
},
fail: (err) => {
uni.showToast({
title: '网络请求失败',
icon: 'none',
duration: 2000
});
reject(err);
}
});
});
};
export default request;