uniapp切换头像
时间: 2023-09-30 08:12:13 浏览: 54
在uniapp中切换头像的步骤如下:
1. 点击头像,调起本地相册选择图片或使用相机拍照。
2. 上传选择的图片,将获取到的临时地址作为参数。
3. 将真实图片地址请求给后端做修改。
具体代码示例如下:
```
// 从本地相册选择图片或使用相机拍照
uni.chooseImage({
count: 1, // 默认选择1张图片
sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
success: (res) => {
console.log(res.tempFilePaths[0]); // 成功则返回图片的本地文件路径列表 tempFilePaths
this.userInfo.headimg = res.tempFilePaths[0]; // 更新本地浏览头像图片
this.update(res.tempFilePaths[0]); // 上传图片
}
});
// 上传图片,将获取到的临时地址带入参数
async update(filePath) {
const _this = this;
const token = uni.getStorageSync('token');
const res = uni.uploadFile({
url: _this.action, // 仅为示例,非真实的接口地址
filePath: filePath,
name: 'file',
header: {
'Authorization': token
},
success: (res) => {
// console.log(JSON.parse(res.data).data) 处理格式
this.weChatRegister({
headimg: REQUEST_BASE_URL + '/' + JSON.parse(res.data).data
}); // 将真实图片地址请求给后端做修改
}
});
}
// 将真实图片地址请求给后端做修改
async weChatRegister(parameter) {
// console.log(parameter)
const res = await weChatRegister(parameter); // 将真实图片地址返回给后端
// console.log(res)
}
```