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.
212 lines
4.6 KiB
212 lines
4.6 KiB
import {
|
|
Api_url
|
|
} from './config'
|
|
// import {Token} from './token.js'
|
|
// var token = new Token();
|
|
|
|
// 防抖函数
|
|
const debounce = (fn, delay = 1000) => {
|
|
let timer = null;
|
|
return function (...args) {
|
|
return new Promise((resolve, reject) => {
|
|
if (timer) clearTimeout(timer);
|
|
timer = setTimeout(() => {
|
|
try {
|
|
const result = fn.apply(this, args);
|
|
resolve(result);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
}, delay);
|
|
});
|
|
}
|
|
}
|
|
|
|
// 请求拦截器
|
|
const requestInterceptor = (config) => {
|
|
// 在这里可以统一处理请求头、token等
|
|
const token = uni.getStorageSync("token");
|
|
if (token) {
|
|
config.header = {
|
|
...config.header,
|
|
'token': token
|
|
}
|
|
}
|
|
return config;
|
|
}
|
|
|
|
// 响应拦截器
|
|
const responseInterceptor = (response) => {
|
|
const { statusCode, data } = response;
|
|
console.log('响应数据:', response);
|
|
|
|
// 处理HTTP状态码
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|
// 处理业务状态码
|
|
if (data && data.code) {
|
|
if (data.code === 1) { // 成功状态码为1
|
|
// 登录成功,保存token
|
|
if (data.data && data.data.token) {
|
|
uni.setStorageSync("token", data.data.token);
|
|
// 保存用户类型
|
|
if (data.data.user_type) {
|
|
uni.setStorageSync("userType", data.data.user_type);
|
|
}
|
|
// 保存过期时间
|
|
if (data.data.expires_time) {
|
|
uni.setStorageSync("expires_time", data.data.expires_time);
|
|
}
|
|
}
|
|
return data;
|
|
} else if (data.code === 401) {
|
|
// 未授权或token过期
|
|
uni.removeStorageSync("token");
|
|
uni.removeStorageSync("userType");
|
|
uni.removeStorageSync("expires_time");
|
|
uni.showToast({
|
|
title: data.msg || '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
});
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/student/login/login?code=401'
|
|
});
|
|
}, 1500);
|
|
return Promise.reject(data);
|
|
} else {
|
|
// 其他业务错误
|
|
uni.showToast({
|
|
title: data.msg || '请求失败',
|
|
icon: 'none'
|
|
});
|
|
return Promise.reject(data);
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
// HTTP错误处理
|
|
uni.showToast({
|
|
title: '网络请求失败',
|
|
icon: 'none'
|
|
});
|
|
return Promise.reject(response);
|
|
};
|
|
|
|
export default {
|
|
// 存储正在进行的请求
|
|
pendingRequests: new Map(),
|
|
|
|
// 生成请求的唯一key
|
|
generateRequestKey(config) {
|
|
const { url, method, data } = config;
|
|
return [url, method, JSON.stringify(data)].join('&');
|
|
},
|
|
|
|
// 取消重复请求
|
|
cancelRequest(config) {
|
|
const requestKey = this.generateRequestKey(config);
|
|
if (this.pendingRequests.has(requestKey)) {
|
|
const controller = this.pendingRequests.get(requestKey);
|
|
controller.abort();
|
|
this.pendingRequests.delete(requestKey);
|
|
}
|
|
},
|
|
|
|
// 防抖处理
|
|
uni_request: debounce((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秒超时
|
|
};
|
|
|
|
// 应用请求拦截器
|
|
const interceptedConfig = requestInterceptor(config);
|
|
|
|
uni.showLoading({
|
|
title: '加载中...'
|
|
});
|
|
|
|
console.log('请求配置:', interceptedConfig);
|
|
console.log('请求URL:', interceptedConfig.url);
|
|
console.log('请求方法:', interceptedConfig.method);
|
|
console.log('请求数据:', interceptedConfig.data);
|
|
|
|
uni.request({
|
|
...interceptedConfig,
|
|
success: (res) => {
|
|
try {
|
|
console.log('原始响应数据:', res);
|
|
const response = responseInterceptor(res);
|
|
console.log('处理后的响应数据:', response);
|
|
resolve(response);
|
|
} catch (error) {
|
|
console.error('请求处理失败:', error);
|
|
uni.showToast({
|
|
title: error.msg || '请求失败',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('请求失败:', error);
|
|
uni.showToast({
|
|
title: '网络请求失败',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
},
|
|
complete: () => {
|
|
uni.hideLoading();
|
|
}
|
|
});
|
|
});
|
|
}, 100),
|
|
|
|
// 封装请求方法
|
|
post(url, data = {}) {
|
|
return this.uni_request({
|
|
url,
|
|
data,
|
|
method: 'POST'
|
|
});
|
|
},
|
|
|
|
get(url, data = {}) {
|
|
return this.uni_request({
|
|
url,
|
|
data,
|
|
method: 'GET'
|
|
});
|
|
},
|
|
|
|
put(url, data = {}) {
|
|
return this.uni_request({
|
|
url,
|
|
data,
|
|
method: 'PUT'
|
|
});
|
|
},
|
|
|
|
// 统一的错误处理
|
|
handleError(error) {
|
|
if (error.statusCode === 401) {
|
|
uni.navigateTo({
|
|
url: `/pages/student/login/login?res_codes=${error.data.code}`
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: error.data?.msg || '请求异常',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|