1、如何封装请求👉uniapp请求封装_uniappx uts vue3 api request封装请求login-CSDN博客
2、封装上传图片的功能
// 图片
const Upload = (url, source, formData) => {
return new Promise(function (resolve, reject) {
let header = {};
if (uni.getStorageSync("token")) {
header = {
// 'Content-Type': ContentType,
authorization: uni.getStorageSync("token"),
};
}
uni.uploadFile({
url: BASE_URL + url,
filePath: source,
name: 'file',
formData,
// name,
header,
success: function (res) {
let obj1 = JSON.parse(res.data);
uni.hideLoading();
if (obj1.code !== 200) {
uni.showToast({
title: obj1.message,
icon: "none",
duration: 2000,
});
} else {
uni.showToast({
title: "上传成功",
icon: "success",
duration: 1000,
});
resolve(obj1)
}
},
fail: function (err) {
console.log(JSON.stringify(err), "失败999");
uni.hideLoading();
uni.showToast({
title: "加载失败, 请稍后再试!",
icon: "none",
duration: 2000,
});
},
complete: function () {},
});
});
};
export { Upload };
3、具体接口的请求js,内容如下:
import { Upload } from './request'
// 获取场景列表
const uploadImg = (source, formData) => {
return Upload('/uploadImg', source, formData)
}
export {
uploadImg,
}