423 lines
11 KiB
Vue
423 lines
11 KiB
Vue
<template>
|
||
<view class="page">
|
||
<view class="info">
|
||
<view v-for="(item, index) in userInfoSettings" :key="index" class="info-row">
|
||
<view class="label">{{ item.label }}</view>
|
||
<image-uploader
|
||
ref="uploader"
|
||
:height="'100rpx'"
|
||
:width="'100rpx'"
|
||
@success="handleUploadSuccess"
|
||
/>
|
||
<!-- <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">
|
||
<logout-button @logout-success="onLogoutSuccess" @logout-error="onLogoutError" />
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import { updateNickName, uploadAvatar } from '@/api/user/user.js'
|
||
import LogoutButton from '@/components/logout-button/logout-button.vue'
|
||
|
||
export default {
|
||
components: {
|
||
LogoutButton,
|
||
},
|
||
data() {
|
||
return {
|
||
userInfoSettings: [
|
||
{
|
||
label: '头像',
|
||
value: '', // 头像URL,从个人中心传递过来
|
||
type: 'avatar',
|
||
},
|
||
// {
|
||
// label: '昵称',
|
||
// value: '昵称', // 默认占位文字,实际应为用户昵称
|
||
// type: 'nickname',
|
||
// },
|
||
],
|
||
}
|
||
},
|
||
onLoad() {
|
||
console.log('设置页面开始加载...')
|
||
console.log('初始userInfoSettings:', this.userInfoSettings)
|
||
|
||
// 页面加载时获取用户信息
|
||
this.loadUserInfo()
|
||
|
||
console.log('设置页面加载完成')
|
||
},
|
||
onShow() {
|
||
// 页面显示时重新加载用户信息,确保数据最新
|
||
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('开始加载用户信息...')
|
||
|
||
try {
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
console.log('从本地存储获取的用户信息:', userInfo)
|
||
|
||
// 检查用户信息是否存在且格式正确
|
||
if (!userInfo) {
|
||
console.log('本地存储中没有用户信息')
|
||
this.setDefaultValues()
|
||
return
|
||
}
|
||
|
||
if (typeof userInfo !== 'object') {
|
||
console.log('用户信息格式不正确,不是对象类型')
|
||
this.setDefaultValues()
|
||
return
|
||
}
|
||
|
||
// 更新昵称显示
|
||
const nicknameItem = this.userInfoSettings.find(item => item.type === 'nickname')
|
||
if (nicknameItem) {
|
||
const nickName = userInfo.nickName || userInfo.nickname || '昵称'
|
||
nicknameItem.value = nickName
|
||
console.log('设置昵称显示:', nickName)
|
||
}
|
||
|
||
// 更新头像显示
|
||
const avatarItem = this.userInfoSettings.find(item => item.type === 'avatar')
|
||
if (avatarItem) {
|
||
const avatar = userInfo.avatar || ''
|
||
avatarItem.value = avatar
|
||
console.log('设置头像显示:', avatar)
|
||
}
|
||
|
||
console.log('用户信息加载成功')
|
||
} catch (error) {
|
||
console.error('加载用户信息时发生错误:', error)
|
||
this.setDefaultValues()
|
||
}
|
||
},
|
||
|
||
// 设置默认值
|
||
setDefaultValues() {
|
||
console.log('设置默认值...')
|
||
|
||
const nicknameItem = this.userInfoSettings.find(item => item.type === 'nickname')
|
||
if (nicknameItem) {
|
||
nicknameItem.value = '昵称'
|
||
}
|
||
|
||
const avatarItem = this.userInfoSettings.find(item => item.type === 'avatar')
|
||
if (avatarItem) {
|
||
avatarItem.value = ''
|
||
}
|
||
|
||
console.log('默认值设置完成')
|
||
},
|
||
|
||
// 处理设置项点击
|
||
handleItemClick(item) {
|
||
if (item.type === 'nickname') {
|
||
this.showNicknameInput()
|
||
} else if (item.type === 'avatar') {
|
||
this.showAvatarUpload()
|
||
}
|
||
},
|
||
|
||
// 显示昵称输入框
|
||
showNicknameInput() {
|
||
const currentNickname = this.userInfoSettings.find(item => item.type === 'nickname').value
|
||
|
||
uni.showModal({
|
||
title: '修改昵称',
|
||
content: '',
|
||
editable: true,
|
||
placeholderText: '请输入昵称',
|
||
confirmText: '确定',
|
||
cancelText: '取消',
|
||
success: async res => {
|
||
if (res.confirm && res.content) {
|
||
const newNickname = res.content.trim()
|
||
if (newNickname) {
|
||
await this.updateNickname(newNickname)
|
||
} else {
|
||
uni.showToast({
|
||
title: '昵称不能为空',
|
||
icon: 'none',
|
||
})
|
||
}
|
||
}
|
||
},
|
||
})
|
||
},
|
||
|
||
// 更新昵称
|
||
async updateNickname(newNickname) {
|
||
try {
|
||
uni.showLoading({
|
||
title: '更新中...',
|
||
})
|
||
|
||
const response = await updateNickName(newNickname)
|
||
|
||
uni.hideLoading()
|
||
|
||
if (response.code === 200) {
|
||
// 更新本地显示
|
||
const nicknameItem = this.userInfoSettings.find(item => item.type === 'nickname')
|
||
if (nicknameItem) {
|
||
nicknameItem.value = newNickname
|
||
}
|
||
|
||
// 更新本地存储的用户信息
|
||
try {
|
||
const userInfo = uni.getStorageSync('userInfo') || {}
|
||
userInfo.nickName = newNickname
|
||
uni.setStorageSync('userInfo', userInfo)
|
||
} catch (error) {
|
||
console.error('更新本地用户信息失败:', error)
|
||
}
|
||
|
||
uni.showToast({
|
||
title: '昵称更新成功',
|
||
icon: 'success',
|
||
})
|
||
|
||
// 通知个人中心页面更新用户信息
|
||
try {
|
||
const userInfo = uni.getStorageSync('userInfo') || {}
|
||
uni.$emit('userInfoUpdated', {
|
||
nickName: newNickname,
|
||
avatar: userInfo.avatar || '',
|
||
})
|
||
} catch (error) {
|
||
console.error('获取用户信息失败:', error)
|
||
uni.$emit('userInfoUpdated', {
|
||
nickName: newNickname,
|
||
avatar: '',
|
||
})
|
||
}
|
||
} else {
|
||
throw new Error(response.msg || '更新失败')
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading()
|
||
console.error('更新昵称失败:', error)
|
||
uni.showToast({
|
||
title: error.message || '更新失败',
|
||
icon: 'none',
|
||
})
|
||
}
|
||
},
|
||
|
||
// 显示头像上传选择
|
||
showAvatarUpload() {
|
||
uni.showActionSheet({
|
||
itemList: ['从相册选择', '拍照'],
|
||
success: res => {
|
||
if (res.tapIndex === 0) {
|
||
this.chooseImageFromAlbum()
|
||
} else if (res.tapIndex === 1) {
|
||
this.takePhoto()
|
||
}
|
||
},
|
||
fail: err => {
|
||
console.log('用户取消选择:', err)
|
||
},
|
||
})
|
||
},
|
||
|
||
// 从相册选择图片
|
||
chooseImageFromAlbum() {
|
||
uni.chooseImage({
|
||
count: 1,
|
||
sizeType: ['compressed'],
|
||
sourceType: ['album'],
|
||
success: res => {
|
||
const filePath = res.tempFilePaths[0]
|
||
this.uploadAvatar(filePath)
|
||
},
|
||
fail: err => {
|
||
console.error('选择图片失败:', err)
|
||
uni.showToast({
|
||
title: '选择图片失败',
|
||
icon: 'none',
|
||
})
|
||
},
|
||
})
|
||
},
|
||
|
||
// 拍照
|
||
takePhoto() {
|
||
uni.chooseImage({
|
||
count: 1,
|
||
sizeType: ['compressed'],
|
||
sourceType: ['camera'],
|
||
success: res => {
|
||
const filePath = res.tempFilePaths[0]
|
||
this.uploadAvatar(filePath)
|
||
},
|
||
fail: err => {
|
||
console.error('拍照失败:', err)
|
||
uni.showToast({
|
||
title: '拍照失败',
|
||
icon: 'none',
|
||
})
|
||
},
|
||
})
|
||
},
|
||
|
||
// 上传头像
|
||
async uploadAvatar(filePath) {
|
||
try {
|
||
uni.showLoading({
|
||
title: '上传中...',
|
||
})
|
||
|
||
console.log('开始上传头像:', filePath)
|
||
|
||
const response = await uploadAvatar(filePath)
|
||
|
||
uni.hideLoading()
|
||
|
||
if (response.code === 200) {
|
||
// 更新本地显示
|
||
const avatarItem = this.userInfoSettings.find(item => item.type === 'avatar')
|
||
if (avatarItem) {
|
||
avatarItem.value = response.data?.avatar || response.data
|
||
}
|
||
|
||
// 更新本地存储的用户信息
|
||
try {
|
||
const userInfo = uni.getStorageSync('userInfo') || {}
|
||
userInfo.avatar = response.data?.avatar || response.data
|
||
uni.setStorageSync('userInfo', userInfo)
|
||
} catch (error) {
|
||
console.error('更新本地用户信息失败:', error)
|
||
}
|
||
|
||
uni.showToast({
|
||
title: '头像上传成功',
|
||
icon: 'success',
|
||
})
|
||
|
||
// 通知其他页面更新头像
|
||
uni.$emit('avatarUpdated', response.data?.avatar || response.data)
|
||
} else {
|
||
throw new Error(response.msg || '上传失败')
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading()
|
||
console.error('头像上传失败:', error)
|
||
uni.showToast({
|
||
title: error.message || '上传失败',
|
||
icon: 'none',
|
||
})
|
||
}
|
||
},
|
||
|
||
// 退出登录成功回调
|
||
onLogoutSuccess(response) {
|
||
console.log('退出登录成功:', response)
|
||
},
|
||
|
||
// 退出登录错误回调
|
||
onLogoutError(error) {
|
||
console.error('退出登录失败:', error)
|
||
},
|
||
},
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.page {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
min-height: 100vh;
|
||
background: #f3f5f6;
|
||
|
||
.info {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
width: 710rpx;
|
||
background: #ffffff;
|
||
|
||
border-radius: 10px;
|
||
opacity: 1;
|
||
margin: 30rpx auto;
|
||
|
||
//border: solid 1rpx red;
|
||
|
||
.info-row {
|
||
display: flex;
|
||
border-bottom: solid 1rpx #d8d8d8;
|
||
width: 646rpx;
|
||
padding: 40rpx 0;
|
||
margin: 0 34rpx;
|
||
cursor: pointer;
|
||
transition: background-color 0.2s ease;
|
||
align-items: center;
|
||
|
||
&:active {
|
||
background-color: #f5f5f5;
|
||
}
|
||
|
||
.label {
|
||
flex: 1;
|
||
color: #3d3d3d;
|
||
font-size: 16px;
|
||
}
|
||
|
||
.value {
|
||
flex: 1;
|
||
text-align: right;
|
||
color: #bbbbbb;
|
||
font-size: 16px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
|
||
.avatar-preview {
|
||
width: 60rpx;
|
||
height: 60rpx;
|
||
border-radius: 50%;
|
||
margin-right: 10rpx;
|
||
}
|
||
|
||
.arrow {
|
||
margin-left: 10rpx;
|
||
color: #bbbbbb;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.logout-container {
|
||
margin-top: 866rpx;
|
||
}
|
||
}
|
||
</style>
|