Files
xk-client-wx/request/api/http.js

88 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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; // 抛出错误供调用者处理
// }
}