话不多说直接上代码
buttonclick () {
var that = this
uni.showActionSheet({
itemList: ['拍照', '相册选取'],
itemColor: '',
success: function (res) {
if (!res.cancel) {
console.log(res.tapIndex);
if (res.tapIndex === 0) {
//拍照
uni.chooseImage({
count: 1,
sourceType: ['camera'],
success: (chooseImageRes) => {
uni.showLoading({
title: '加载中'
});
const tempFilePaths = chooseImageRes.tempFilePaths;
uni.uploadFile({
//上传图片接口
url: that.baseUrl + '/common/upload',
filePath: tempFilePaths[0],
name: 'file',
formData: {
// 'user': 'test'
},
header:{
Authorization: 'Bearer '+getToken()
},
success: (uploadFileRes) => {
var a = JSON.parse(uploadFileRes.data)
if (a.code == '200') {
//自己定义的修改图片的接口 a.url为上传成功返回的图片地址
that.bcBtn(a.url)
} else {
uni.showToast({
title: '修改失败请重试',
duration: 2000,
icon: 'none'
});
}
},
fail: (err) => {
uni.showToast({
title: err.errMsg,
duration: 2000,
icon: 'none'
});
}
});
}
});
}
if (res.tapIndex === 1) {
//相册
uni.chooseImage({
count: 1,
sourceType: ['album'],
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
console.log('照片信息1', chooseImageRes)
uni.showLoading({
title: '加载中'
});
uni.uploadFile({
url: that.baseUrl + '/common/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
// 'user': 'test'
},
header:{
Authorization: 'Bearer '+getToken()
},
success: (uploadFileRes) => {
console.log('00', uploadFileRes.data);
var a = JSON.parse(uploadFileRes.data)
if (a.code == '200') {
that.bcBtn(a.url)
} else {
uni.showToast({
title: '修改失败请重试',
duration: 2000,
icon: 'none'
});
}
}
});
}
});
}
}
},
fail: function (res) {
},
complete: function (res) {
},
});
},
修改成功后可以定义一个变量过来刷新用户图片