228 lines
6.0 KiB
JavaScript
228 lines
6.0 KiB
JavaScript
/**
|
|
* 网络请求工具类
|
|
*/
|
|
|
|
import { getToken, logout } from './auth.js';
|
|
import config from '@/config.js';
|
|
|
|
// 基础URL配置 - 从配置文件读取
|
|
const BASE_URL = config.BASE_URL;
|
|
|
|
// 请求超时时间 - 从配置文件读取
|
|
const TIMEOUT = config.TIMEOUT;
|
|
|
|
/**
|
|
* 网络请求封装
|
|
*/
|
|
export function request(options) {
|
|
return new Promise((resolve, reject) => {
|
|
const token = getToken();
|
|
|
|
// 默认配置
|
|
const defaultOptions = {
|
|
url: BASE_URL + options.url,
|
|
method: options.method || 'GET',
|
|
timeout: TIMEOUT,
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
...options.header,
|
|
},
|
|
data: options.data || {},
|
|
};
|
|
|
|
// 添加Authorization header
|
|
if (token) {
|
|
defaultOptions.header['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
// 显示加载提示
|
|
if (options.loading !== false) {
|
|
uni.showLoading({
|
|
title: options.loadingText || '加载中...',
|
|
mask: true,
|
|
});
|
|
}
|
|
|
|
uni.request({
|
|
...defaultOptions,
|
|
success: (res) => {
|
|
// 隐藏加载提示
|
|
if (options.loading !== false) {
|
|
uni.hideLoading();
|
|
}
|
|
|
|
const { statusCode, data } = res;
|
|
|
|
// 请求成功
|
|
if (statusCode === 200) {
|
|
// 业务成功
|
|
if (data.code === 200 || data.success === true) {
|
|
resolve(data);
|
|
} else {
|
|
// 业务失败
|
|
const message = data.message || data.msg || '请求失败';
|
|
|
|
// Token过期,跳转登录
|
|
if (data.code === 401) {
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none',
|
|
});
|
|
logout();
|
|
return;
|
|
}
|
|
|
|
// 显示错误信息
|
|
if (options.showError !== false) {
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
});
|
|
}
|
|
|
|
reject(new Error(message));
|
|
}
|
|
} else {
|
|
// HTTP状态码错误
|
|
const message = `请求失败 (${statusCode})`;
|
|
|
|
if (options.showError !== false) {
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
});
|
|
}
|
|
|
|
reject(new Error(message));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
// 隐藏加载提示
|
|
if (options.loading !== false) {
|
|
uni.hideLoading();
|
|
}
|
|
|
|
const message = err.errMsg || '网络请求失败';
|
|
|
|
if (options.showError !== false) {
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
});
|
|
}
|
|
|
|
reject(new Error(message));
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* GET请求
|
|
*/
|
|
export function get(url, params = {}, options = {}) {
|
|
return request({
|
|
url,
|
|
method: 'GET',
|
|
data: params,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* POST请求
|
|
*/
|
|
export function post(url, data = {}, options = {}) {
|
|
return request({
|
|
url,
|
|
method: 'POST',
|
|
data,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* PUT请求
|
|
*/
|
|
export function put(url, data = {}, options = {}) {
|
|
return request({
|
|
url,
|
|
method: 'PUT',
|
|
data,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* DELETE请求
|
|
*/
|
|
export function del(url, params = {}, options = {}) {
|
|
return request({
|
|
url,
|
|
method: 'DELETE',
|
|
data: params,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
*/
|
|
export function uploadFile(url, filePath, options = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const token = getToken();
|
|
|
|
// 显示上传进度
|
|
if (options.loading !== false) {
|
|
uni.showLoading({
|
|
title: options.loadingText || '上传中...',
|
|
mask: true,
|
|
});
|
|
}
|
|
|
|
uni.uploadFile({
|
|
url: BASE_URL + url,
|
|
filePath,
|
|
name: options.name || 'file',
|
|
header: {
|
|
Authorization: token ? `Bearer ${token}` : '',
|
|
...options.header,
|
|
},
|
|
formData: options.formData || {},
|
|
success: (res) => {
|
|
if (options.loading !== false) {
|
|
uni.hideLoading();
|
|
}
|
|
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
if (data.code === 200 || data.success === true) {
|
|
resolve(data);
|
|
} else {
|
|
const message = data.message || data.msg || '上传失败';
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
});
|
|
reject(new Error(message));
|
|
}
|
|
} catch (e) {
|
|
reject(new Error('响应数据解析失败'));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
if (options.loading !== false) {
|
|
uni.hideLoading();
|
|
}
|
|
|
|
const message = err.errMsg || '上传失败';
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
});
|
|
reject(new Error(message));
|
|
},
|
|
});
|
|
});
|
|
}
|