智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 lines
1.8 KiB

import {
Api_url
} from './config'
// 静默请求工具 - 用于字典获取等不需要显示加载提示的场景
const axiosQuiet = {
// 静默请求方法
request(options) {
return new Promise((resolve, reject) => {
// 创建请求配置
const config = {
url: Api_url + options.url,
data: options.data,
method: options.method || 'GET',
header: {
'token': uni.getStorageSync("token")
},
timeout: 10000 // 设置10秒超时
};
console.log('静默请求配置:', config);
uni.request({
...config,
success: (res) => {
try {
const { statusCode, data } = res;
console.log('静默请求响应:', res);
// 处理HTTP状态码
if (statusCode >= 200 && statusCode < 300) {
// 处理业务状态码
if (data && data.code) {
if (data.code === 1) { // 成功状态码为1
resolve(data);
} else if (data.code === 401) {
// 401错误静默处理,不显示提示
console.warn('静默请求401错误:', data.msg);
reject(data);
} else {
// 其他业务错误也静默处理
console.warn('静默请求业务错误:', data.msg);
reject(data);
}
} else {
resolve(data);
}
} else {
// HTTP错误
console.warn('静默请求HTTP错误:', statusCode);
reject(res);
}
} catch (error) {
console.error('静默请求处理失败:', error);
reject(error);
}
},
fail: (error) => {
console.warn('静默请求失败:', error);
reject(error);
}
});
});
},
// GET请求
get(url, data = {}) {
return this.request({
url,
data,
method: 'GET'
});
},
// POST请求
post(url, data = {}) {
return this.request({
url,
data,
method: 'POST'
});
}
};
export default axiosQuiet;