修改上传头像-使用七牛云存储

This commit is contained in:
WindowBird 2025-09-15 09:25:26 +08:00
parent 6d5924e842
commit cb5b55badd
2 changed files with 40 additions and 44 deletions

View File

@ -188,34 +188,19 @@ export function submitWithdraw(data) {
/**
* 上传头像
* @param {string} filePath - 文件路径
* @param {string} avatarUrl - 文件路径
* @returns {Promise} 返回上传结果
*/
export function uploadAvatar(filePath) {
return uploadFile(
'/app/user/avatar',
filePath,
'avatarfile',
{},
{
timeout: 60000,
}
)
.then(data => {
// 上传成功后更新本地存储
const userInfo = uni.getStorageSync('userInfo') || {}
userInfo.avatar = data.data?.avatar || data.data
uni.setStorageSync('userInfo', userInfo)
// 通知其他页面更新头像
uni.$emit('avatarUpdated', userInfo.avatar)
console.log('头像上传成功,已更新本地存储:', userInfo.avatar)
return data
})
.catch(error => {
console.error('头像上传失败:', error)
throw error
export function uploadAvatar(avatarUrl) {
return request({
url: '/app/user/avatar',
method: 'PUT',
data: {
avatarUrl: avatarUrl,
},
header: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
}

View File

@ -1,24 +1,25 @@
<template>
<view class="page">
<view class="info">
<view
v-for="(item, index) in userInfoSettings"
:key="index"
class="info-row"
@click="handleItemClick(item)"
>
<view v-for="(item, index) in userInfoSettings" :key="index" class="info-row">
<view class="label">{{ item.label }}</view>
<view class="value">
<image
v-if="item.type === 'avatar' && item.value"
:src="item.value"
class="avatar-preview"
mode="aspectFill"
<image-uploader
ref="uploader"
:height="'100rpx'"
:width="'100rpx'"
@success="handleUploadSuccess"
/>
<text v-else-if="item.type === 'avatar'">点击设置头像</text>
<text v-else>{{ item.value }}</text>
<text class="arrow">></text>
</view>
<!-- <view class="value">-->
<!-- <image-->
<!-- v-if="item.type === 'avatar' && item.value"-->
<!-- :src="item.value"-->
<!-- class="avatar-preview"-->
<!-- mode="aspectFill"-->
<!-- />-->
<!-- <text v-else-if="item.type === 'avatar'">点击设置头像</text>-->
<!-- <text v-else>{{ item.value }}</text>-->
<!-- <text class="arrow">></text>-->
<!-- </view>-->
</view>
</view>
<view class="logout-container">
@ -65,6 +66,15 @@ export default {
this.loadUserInfo()
},
methods: {
handleUploadSuccess(result) {
console.log('图片上传成功:', result.url)
this.userInfoSettings.value = result.url
this.uploadAvatar(this.userInfoSettings.value)
uni.showToast({
title: '上传成功',
icon: 'success',
})
},
//
loadUserInfo() {
console.log('开始加载用户信息...')
@ -369,6 +379,7 @@ export default {
margin: 0 34rpx;
cursor: pointer;
transition: background-color 0.2s ease;
align-items: center;
&:active {
background-color: #f5f5f5;