小程序-修改用户头像

1、调用拍照 / 选择图片

// 修改头像

const onAvatarChange = () => {

  // 调用拍照 / 选择图片

  uni.chooseMedia({

    // 文件个数

    count: 1,

    // 文件类型

    mediaType: ['image'],

    success: (res) => {

      console.log(res)

      // 本地临时文件路径 (本地路径)

      const { tempFilePath } = res.tempFiles[0]

    },

  })

}

2、获取图片路径

3、上传文件

// 文件上传

      uni.uploadFile({

        url: '/member/profile/avatar',

        name: 'file',

        filePath: tempFilePath,

        success: (res) => {

          if (res.statusCode === 200) {

            const avatar = JSON.parse(res.data).result.avatar

            profile.value.avatar = avatar

          }

        },

      })

4、更新头像

5、完整代码实现

<template>
<!-- 头像 -->
  <view class="avatar">
     <view class="avatar-content" @tap="onAvatarChange">
       <image class="image" :src="profile?.avatar" mode="aspectFill" />
       <text class="text">点击修改头像</text>
     </view>
  </view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getMemberProfileAPI } from '@/services/profile'
import type { ProfileDetail } from '@/types/member'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 获取个人信息
const profile = ref<ProfileDetail>()
const getMemberProfileData = async () => {
  const res = await getMemberProfileAPI()
  console.log('获取个人信息', res)
  profile.value = res.result
}
// 页面加载
onLoad(() => {
  getMemberProfileData()
})

// 修改头像
const onAvatarChange = () => {
  // 调用拍照 / 选择图片
  uni.chooseMedia({
    // 文件个数
    count: 1,
    // 文件类型
    mediaType: ['image'],
    success: (res) => {
      console.log(res)
      // 本地临时文件路径 (本地路径)
      const { tempFilePath } = res.tempFiles[0]
      // 文件上传
      uni.uploadFile({
        url: '/member/profile/avatar',
        name: 'file',
        filePath: tempFilePath,
        success: (res) => {
          if (res.statusCode === 200) {
            const avatar = JSON.parse(res.data).result.avatar
            profile.value.avatar = avatar
          }
        },
      })
    },
  })
}
</script>

相关推荐

  1. uniapp获取用户头像

    2024-05-25 18:55:03       84 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 18:55:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 18:55:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 18:55:03       82 阅读
  4. Python语言-面向对象

    2024-05-25 18:55:03       91 阅读

热门阅读

  1. sqlite3创建数据库,创建一个users表格,c语言实现

    2024-05-25 18:55:03       29 阅读
  2. C#的委托和事件

    2024-05-25 18:55:03       35 阅读
  3. C#--WPF自定义控件模板示例

    2024-05-25 18:55:03       34 阅读
  4. 使用Autofit.js和React实现自适应布局

    2024-05-25 18:55:03       32 阅读
  5. 力扣 10. 正则表达式匹配 python AC

    2024-05-25 18:55:03       31 阅读
  6. Vue3设置缓存:storage.ts

    2024-05-25 18:55:03       33 阅读
  7. 利用ChatGPT辅助理解数学建模竞赛题目与拆解问题

    2024-05-25 18:55:03       29 阅读
  8. c++(三)

    c++(三)

    2024-05-25 18:55:03      33 阅读
  9. day32 332.重新安排行程 51. N皇后 37. 解数独

    2024-05-25 18:55:03       36 阅读