This commit is contained in:
2025-11-24 16:01:45 +08:00
parent aa160793e7
commit 46c975591b
45 changed files with 1311 additions and 559 deletions

View File

@@ -1,27 +1,19 @@
<template>
<view class="container">
<!-- 自定义头部 -->
<view class="header">
<view class="back-btn" @click="goBack">
<uni-icons type="left" size="22" color="#fff" />
</view>
<view class="title">用户信息</view>
<view class="placeholder"></view>
</view>
<!-- 内容区域 -->
<view class="content">
<!-- 昵称输入 -->
<view class="form-item">
<view class="label">昵称:</view>
<input class="input" v-model="userInfo.nickName" placeholder="请输入昵称" />
<input class="input" v-model="userInfo.name" placeholder="请输入昵称" />
</view>
<!-- 头像选择 -->
<view class="form-item">
<view class="label">头像:</view>
<view class="avatar-wrapper" @click="chooseNewAvatar">
<image :src="userInfo.avatarUrl || '/static/logo.png'" mode="aspectFill" class="avatar" />
<image :src="userInfo.avatar || '/static/logo.png'" mode="aspectFill" class="avatar" />
<view class="avatar-tip">点击更新头像</view>
</view>
</view>
@@ -33,20 +25,18 @@
</template>
<script setup>
import { ref, getCurrentInstance } from 'vue'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { updateGlobalUserInfo } from '../../../main.js'
// 用户信息
const userInfo = ref({
id: '',
nickName: '',
avatarUrl: ''
name: '',
avatar: ''
})
// 初始化
onLoad((options) => {
// 从选项中获取用户数据
if (options.userData) {
try {
const data = JSON.parse(decodeURIComponent(options.userData))
@@ -55,27 +45,16 @@ onLoad((options) => {
console.error('解析用户数据失败:', e)
}
}
// 记录来源页面信息,支持返回到正确页面
if (options.from) {
console.log('来源页面:', options.from)
}
})
// 返回上一页
const goBack = () => {
// 检查是否有未保存的修改
if (userInfo.value.nickName.trim()) {
// 使用全局方法保存用户信息,确保全局可用
updateGlobalUserInfo(userInfo.value)
}
wx.navigateBack()
uni.navigateBack()
}
// 选择新头像
const chooseNewAvatar = () => {
wx.chooseMedia({
uni.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
@@ -85,10 +64,10 @@ const chooseNewAvatar = () => {
// 统一处理头像选择逻辑,支持所有平台
try {
const tempFilePath = res.tempFiles[0].tempFilePath
userInfo.value.avatarUrl = tempFilePath
userInfo.value.avatar = tempFilePath
} catch (error) {
console.error('处理头像文件路径失败:', error)
wx.showToast({
uni.showToast({
title: '头像设置失败',
icon: 'none'
})
@@ -96,7 +75,7 @@ const chooseNewAvatar = () => {
},
fail: (err) => {
console.error('选择图片失败:', err)
wx.showToast({
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
@@ -106,45 +85,46 @@ const chooseNewAvatar = () => {
// 保存用户信息
const saveUserInfo = () => {
if (!userInfo.value.nickName.trim()) {
wx.showToast({
if (!userInfo.value.name.trim()) {
uni.showToast({
title: '昵称不能为空',
icon: 'none'
})
return
}
// 使用全局方法保存用户信息,确保全局可用
updateGlobalUserInfo(userInfo.value)
// 保存成功,返回上一页并传递更新的数据
const pages = getCurrentPages()
const prevPage = pages[pages.length - 2]
if (prevPage) {
// 通过事件通知上一页更新数据
// 通过页面实例调用方法更新数据
if (prevPage.$vm && prevPage.$vm.updateUserData) {
prevPage.$vm.updateUserData(userInfo.value)
} else {
// 如果上一页没有updateUserData方法通过全局事件通知
wx.$emit && wx.$emit('userDataUpdated', userInfo.value)
}
}
// 通过全局事件通知更新数据(作为备选方案)
uni.$emit('userDataUpdated', userInfo.value)
// 将用户信息保存到本地存储
uni.setStorageSync('userInfo', userInfo.value)
uni.setStorageSync('currentUserInfo', userInfo.value)
// 显示保存成功提示
wx.showToast({
uni.showToast({
title: '保存成功',
icon: 'success',
duration: 1500,
success: () => {
setTimeout(() => {
wx.navigateBack()
uni.navigateBack()
}, 1500)
}
})
}
</script>
<style scoped>
<style lang="less" scoped>
.container {
min-height: 100vh;
background-color: #f5f5f5;