HomeLease/pages/set/set.vue

423 lines
11 KiB
Vue
Raw Normal View History

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