uniapp小程序获取用户头像及上传后保存永久链接

微信开发文档有写怎么获取用户头像
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html

<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
    <uv-image :src="userInfo?.avatar?userInfo.avatar:'/static/images/people.png'" width="90rpx" height="90rpx" shape="circle"></uv-image>
</button>

uniapp和微信小程序的uploadFile直接传文件路径,后端接收到的是file文件格式

const onChooseAvatar = async (e : any) => {
  const { avatarUrl } = e.detail  // 临时链接无法长时间使用,会失效
  userInfo.value.avatar = avatarUrl
  uni.setStorageSync("userInfo", userInfo.value)
  uni.uploadFile({
    url: import.meta.env.VITE_WX_BASE_API + "/api/appletAccount/avatar", // 后端上传文件的接口
    filePath: avatarUrl,
    header: {
      Authorization: uni.getStorageSync("token")
    },
    name: 'file',
    success: () => {
    uni.showToast({
      icon: "success",
        title: "头像修改成功"
    })
  },
 })
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容