uni-app头像编辑上传

发布于:2024-03-06 ⋅ 阅读:(69) ⋅ 点赞:(0)

实现比较简单,文档中都有描述,就是第一次做可能会有疏漏,记录一下:

<view class="edict-item" @click="selectPic">
	<text class="item-name" :style="$em.$getThemeStyle(['avatarConText'])">头像</text>
	<image class="icon-head" :src="headSrc" mode="aspectFill"></image>
	<image class="item-arrow" src="@/static/icon_editinfo_arrow_right.png" mode="heightFix">
	</image>
</view>
selectPic() {
				let that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						that.headSrc = res.tempFilePaths[0]
						that.saveAvatar(that.headSrc)
					}
				});
			},
			saveAvatar(url){
				let header = {
					'Content-Type': 'application/json'
				}
			
				// 获取本地token  此处 有例外 不需要token的请求 后续添加
				console.log('当前请求token====' + loginUtil.getToken())
				if (loginUtil.getToken()) {
					header['Authorization'] = 'Bearer ' + loginUtil.getToken();
				}
				
				uni.uploadFile({
					url: 'https://smarteye-app-uat.gwm.cn/prod-api/system/user/profile/avatar', //仅为示例,非真实的接口地址
					filePath: url,
					name: 'file',
					header:header,
					// success: (res) => {
					// 	console.log('save', res);
					// }
				});
			},

在这里插入图片描述