88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
import http from './request'
|
||
|
||
const HttpUrlMap = [
|
||
'/oldApi',
|
||
'/newApi',
|
||
'/imApi',
|
||
'/xkApi',
|
||
]
|
||
|
||
/**
|
||
* GET请求(Json)
|
||
* @param url
|
||
* @param params
|
||
* @param type
|
||
* @returns {Promise<*>}
|
||
*/
|
||
export async function get(url, params, type = 0) {
|
||
return await http(HttpUrlMap[type] + url, {
|
||
method: 'GET',
|
||
data: params,
|
||
// 传输方式是json
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
}
|
||
}, 1)
|
||
}
|
||
/**
|
||
* POST请求(Json)
|
||
* @param url
|
||
* @param params
|
||
* @param type
|
||
* @returns {Promise<*>}
|
||
*/
|
||
export async function post(url, params, type = 0) {
|
||
return await http(HttpUrlMap[type] + url, {
|
||
method: 'POST',
|
||
data: params,
|
||
// 传输方式是json
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
}
|
||
}, 1)
|
||
}
|
||
|
||
/**
|
||
* 文件上传方法(仅支持 FormData 格式)
|
||
* @param {string} url - 接口路径
|
||
* @param {any} formData - 包含文件的 FormData 对象
|
||
* @param {int} [type=0] - 请求类型(用于映射 HttpUrlMap)
|
||
* @returns {Promise<*>}
|
||
*/
|
||
export function uploadFile(url, formData, type = 3) {
|
||
const fullUrl = 'http://127.0.0.1:18001/api/mobile' + url;
|
||
|
||
console.log(fullUrl, 'sssssssssssss')
|
||
uni.uploadFile({
|
||
url: 'http://127.0.0.1:18001/api/mobile/chat-friends/upload-chat-file',
|
||
filePath: formData,
|
||
name: 'file',
|
||
header: {
|
||
"authorization": uni.getStorageSync('token')
|
||
},
|
||
formData,
|
||
success: (uploadFileRes) => {
|
||
return uploadFileRes;
|
||
},
|
||
fail: (res) => {
|
||
console.log(res, '上传失败')
|
||
}
|
||
})
|
||
// return;
|
||
// try {
|
||
// const response = await http(fullUrl, {
|
||
// method: 'POST',
|
||
// data: formData,
|
||
// // 注意:不手动设置 Content-Type,浏览器会自动添加 multipart/form-data 和 boundary
|
||
// headers: {
|
||
// // 如果需要额外 headers(如 token),可在此添加
|
||
// // 'Authorization': 'Bearer xxx',
|
||
// },
|
||
// }, 1);
|
||
//
|
||
// return response;
|
||
// } catch (error) {
|
||
// console.error('File upload failed:', error);
|
||
// throw error; // 抛出错误供调用者处理
|
||
// }
|
||
} |