302 lines
6.4 KiB
JavaScript
302 lines
6.4 KiB
JavaScript
|
|
const config = {
|
|
baseURL: 'http://121.40.135.94:8090',
|
|
// baseURL: 'http://localhost:8090',
|
|
timeout: 100000, // 请求超时时间
|
|
header: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
// 处理图片URL,添加baseURL前缀
|
|
const getImageUrl = (url) => {
|
|
if (!url) return ''
|
|
|
|
// 如果已经是完整的URL(http开头),直接返回
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
return url
|
|
}
|
|
|
|
// 如果是相对路径,添加baseURL前缀
|
|
const baseUrl = config.baseURL
|
|
// 确保baseURL和url之间有正确的斜杠
|
|
if (url.startsWith('/')) {
|
|
return baseUrl + url
|
|
} else {
|
|
return baseUrl + '/' + url
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
const requestInterceptor = (options) => {
|
|
// 显示加载提示
|
|
uni.showLoading({
|
|
title: '加载中...',
|
|
mask: true
|
|
});
|
|
|
|
// 拼接完整URL
|
|
if (options.url && !options.url.startsWith('http')) {
|
|
options.url = config.baseURL + options.url;
|
|
}
|
|
|
|
// 处理token
|
|
const token = options.token || uni.getStorageSync('token') || '';
|
|
|
|
// 合并请求头
|
|
options.header = {
|
|
...config.header,
|
|
...options.header
|
|
};
|
|
|
|
// 如果有token,添加到header中
|
|
if (token) {
|
|
options.header['token'] = token;
|
|
}
|
|
|
|
// 设置超时时间
|
|
options.timeout = options.timeout || config.timeout;
|
|
|
|
return options;
|
|
};
|
|
|
|
// 响应拦截器
|
|
const responseInterceptor = (response, resolve, reject) => {
|
|
// 隐藏加载提示
|
|
uni.hideLoading();
|
|
|
|
var { statusCode, data } = response;
|
|
statusCode=data.code
|
|
// HTTP状态码判断
|
|
if (statusCode === 200) {
|
|
// 业务状态码判断(根据后端接口规范调整)
|
|
if (data.code === 0 || data.success === true) {
|
|
resolve(data);
|
|
} else {
|
|
// 业务错误
|
|
uni.showToast({
|
|
title: data.message || '请求失败',
|
|
icon: 'none'
|
|
});
|
|
|
|
reject(data);
|
|
}
|
|
} else if (statusCode === 401) {
|
|
// token过期或无效
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
});
|
|
|
|
// 清除本地存储的token和用户信息
|
|
uni.removeStorageSync('token');
|
|
uni.removeStorageSync('userInfo');
|
|
|
|
// 跳转到登录页
|
|
uni.reLaunch({ url: '/pages/login/login' });
|
|
reject(response)
|
|
} else {
|
|
// 其他HTTP错误
|
|
uni.showToast({
|
|
title: ` ${data.message}`,
|
|
icon: 'none'
|
|
});
|
|
reject(response);
|
|
}
|
|
};
|
|
|
|
// 通用请求方法
|
|
const request = (options) => {
|
|
return new Promise((resolve, reject) => {
|
|
// 请求拦截
|
|
const finalOptions = requestInterceptor(options);
|
|
|
|
uni.request({
|
|
...finalOptions,
|
|
success: (response) => {
|
|
responseInterceptor(response, resolve, reject);
|
|
},
|
|
fail: (error) => {
|
|
uni.hideLoading();
|
|
uni.showToast({
|
|
title: '网络请求失败',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
// GET请求
|
|
const get = (url, params = {}, options = {}) => {
|
|
// 将参数拼接到URL上
|
|
let finalUrl = url;
|
|
if (params && Object.keys(params).length > 0) {
|
|
const queryString = Object.keys(params)
|
|
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
|
.join('&');
|
|
finalUrl = url.includes('?') ? `${url}&${queryString}` : `${url}?${queryString}`;
|
|
}
|
|
|
|
return request({
|
|
url: finalUrl,
|
|
method: 'GET',
|
|
...options
|
|
});
|
|
};
|
|
|
|
// POST请求
|
|
const post = (url, data = {}, options = {}) => {
|
|
return request({
|
|
url,
|
|
method: 'POST',
|
|
data,
|
|
...options
|
|
});
|
|
};
|
|
|
|
// PUT请求
|
|
const put = (url, data = {}, options = {}) => {
|
|
return request({
|
|
url,
|
|
method: 'PUT',
|
|
data,
|
|
...options
|
|
});
|
|
};
|
|
|
|
// DELETE请求
|
|
const del = (url, data = {}, options = {}) => {
|
|
return request({
|
|
url,
|
|
method: 'DELETE',
|
|
data,
|
|
...options
|
|
});
|
|
};
|
|
|
|
// 文件上传
|
|
const upload = (url, filePath, options = {}) => {
|
|
return new Promise((resolve, reject) => {
|
|
uni.showLoading({
|
|
title: '上传中...',
|
|
mask: true
|
|
});
|
|
|
|
// 拼接完整URL
|
|
if (url && !url.startsWith('http')) {
|
|
url = config.baseURL + url;
|
|
}
|
|
|
|
// 处理token
|
|
const token = options.token || uni.getStorageSync('token') || '';
|
|
const uploadHeader = {
|
|
...options.header // 不要包含config.header,因为它包含Content-Type: application/json
|
|
};
|
|
|
|
// 如果有token,添加到header中
|
|
if (token) {
|
|
uploadHeader['token'] = token;
|
|
}
|
|
|
|
uni.uploadFile({
|
|
url,
|
|
filePath,
|
|
name: options.name || 'file',
|
|
header: uploadHeader,
|
|
formData: options.formData || {},
|
|
success: (response) => {
|
|
uni.hideLoading();
|
|
try {
|
|
const data = JSON.parse(response.data);
|
|
if (data.code === 0 || data.success === true) {
|
|
resolve(data);
|
|
} else {
|
|
uni.showToast({
|
|
title: data.message || '上传失败',
|
|
icon: 'none'
|
|
});
|
|
reject(data);
|
|
}
|
|
} catch (e) {
|
|
reject(response);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
uni.hideLoading();
|
|
uni.showToast({
|
|
title: '上传失败',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
// 文件下载
|
|
const download = (url, options = {}) => {
|
|
return new Promise((resolve, reject) => {
|
|
uni.showLoading({
|
|
title: '下载中...',
|
|
mask: true
|
|
});
|
|
|
|
// 拼接完整URL
|
|
if (url && !url.startsWith('http')) {
|
|
url = config.baseURL + url;
|
|
}
|
|
|
|
// 处理token
|
|
const token = options.token || uni.getStorageSync('token') || '';
|
|
const downloadHeader = {
|
|
...config.header,
|
|
...options.header
|
|
};
|
|
|
|
// 如果有token,添加到header中
|
|
if (token) {
|
|
downloadHeader['token'] = token;
|
|
}
|
|
|
|
uni.downloadFile({
|
|
url,
|
|
header: downloadHeader,
|
|
success: (response) => {
|
|
uni.hideLoading();
|
|
if (response.statusCode === 200) {
|
|
resolve(response);
|
|
} else {
|
|
uni.showToast({
|
|
title: '下载失败',
|
|
icon: 'none'
|
|
});
|
|
reject(response);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
uni.hideLoading();
|
|
uni.showToast({
|
|
title: '下载失败',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
config,
|
|
request,
|
|
get,
|
|
post,
|
|
put,
|
|
delete: del,
|
|
upload,
|
|
download,
|
|
getImageUrl
|
|
}; |