// 选择图片
const uniChooseImage = () => {
return new Promise((resolve, rejct) => {
uni.chooseImage({
// 从本地相册选择图片或使用相机拍照。
count: 1, //默认选择1张图片
sizeType: ['original', 'compressed'], //original 原图,compressed 压缩图,默认二者都有
success: res1 => {
resolve(res1.tempFilePaths[0]);
}
});
});
}
//上传图片
const uniUpLoadFile = async () => {
let promise;
await uniChooseImage().then(filePath => {
promise = new Promise((resolve, rejct) => {
uni.showLoading({
title: '上传中..'
})
uni.uploadFile({
url: BASE_URL + '/api/index/upload_image',//服务器地址
filePath: filePath,
name: 'file',//我公司后端接口接收的参数是file(看你公司用什么参数名)
header: {
'Content-Type': 'multipart/form-data',
Authorization: 'Bearer ' + getApp().globalData.token//token,可加可不加
},
success: res => {
uni.hideLoading();
if (res.statusCode == 403) {} else {
var data = res.data ? JSON.parse(res.data) : {};
if (data.status == 1) {
resolve(data.data)
} else {}
}
},
fail: res => {
uni.showToast({
title: '上传图片失败'
});
uni.hideLoading();
}
});
});
})
return promise;
}
//调用方法
const img = await this.uniUpLoadFile();
this.head_img = img;