uniapp 实现个人信息的修改

news/2024/10/1 21:30:53

今天设计用户界面和用户基本信息设计页面,完成登录后用户的信息就会显示在此页面,然后进入修改页面后就可以对用户的对应信息进行修改。

复制代码
<template><view class="container"><view class="profile"><image :src="userInfo.headshot || defaultAvatar" class="avatar" @dblclick="toLogin()"></image><text class="username">{{ userInfo.username || '用户名未设置' }}</text><view v-if="showDetails" class="details-container" ref="details" @dblclick="changeInfo"><view class="detail-item"><text class="detail-label">手机号:</text><text class="detail-value">{{ userInfo.phone || '暂无' }}</text></view><view class="detail-item"><text class="detail-label">性别:</text><text class="detail-value">{{ userInfo.gender || '暂无' }}</text></view><view class="detail-item"><text class="detail-label">生日:</text><text class="detail-value">{{ userInfo.birthday || '暂无' }}</text></view><view class="detail-item"><text class="detail-label">位置:</text><text class="detail-value">{{ userInfo.location || '暂无' }}</text></view><view class="detail-item"><text class="detail-label">账号:</text><text class="detail-value">{{ userInfo.account || '暂无' }}</text></view></view><text v-if="!showDetails" class="details-hint" @click="toggleDetails">查看详细信息</text><text v-else class="details-hint" @click="toggleDetails">收起</text></view></view>
</template><script>
export default {data() {return {isPopupVisible: false,showDetails: false,userInfo: {},defaultAvatar:'/static/logo.png'};},onShow() {// 页面显示时调用的方法console.log('页面显示了');// 获取存储的数据this.userInfo = uni.getStorageSync('userInfo');//读取登录成功后的存入的信息// 检查存储的数据是否存在if (this.userInfo) {// 存储的数据存在,您可以在这里使用它console.log(this.userInfo===null);} else {// 如果存储的数据不存在,则可能出现错误或者之前没有存储任何数据console.log("没有找到存储的数据");console.log(this.userInfo===null);}},methods: {changeInfo(){console.log(this.userInfo===null)if(this.userInfo){uni.navigateTo({url:"/pages/userinformation/changeinfo/changeinfo"})}else{uni.showToast({title:"请登陆后再试",icon:"none",})uni.navigateTo({url:"/pages/userinformation/login/login"})}},toLogin()//登录或切换账号{uni.navigateTo({url:"/pages/userinformation/login/login"})},toggleDetails() {this.showDetails = !this.showDetails;},},
}
</script><style >
body, html {margin: 0;padding: 0;height: 100%;display: flex;flex-direction: column;justify-content: flex-start; /* 页面内容置于顶部 */align-items: center;background-color: #f0f0f0; /* 设置页面背景颜色 */}.container {width: 80%;max-width: 800px;height: auto; /* 让容器高度自适应内容 */border: 1px solid #ccc;border-radius: 10px;padding: 20px;margin-top: 20px; /* 上方留出一些空白 */background-color: white; /* 设置容器背景颜色 */}.profile {display: flex;flex-direction: column;align-items: center;cursor: pointer;background-color: #fff;padding: 15px 20px;border-radius: 10px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);transition: background-color 0.3s ease;
}.profile:hover {background-color: #f0f0f0;
}.avatar {width: 100px;height: 100px;border-radius: 50%;margin-bottom: 10px;
}.username {font-size: 20px;
}.details-hint {font-size: 14px;color: #ccc; /* 浅一点的颜色 */margin-top: 10px;
}.details-container {background-color: #fff;border-radius: 10px;padding: 15px 20px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}.detail-item {margin-bottom: 10px;
}.detail-label {font-weight: bold;
}.detail-value {margin-left: 10px;color: #666;
}
</style>
复制代码

未登录时:

 登录完成后:

 

 然后进入修改页面

复制代码
<template><view class="container"><view class="header"><text class="title">用户信息</text></view><view class="avatar"><image class="avatar-img" :src="userInfo.headshot || defaultAvatar" mode="aspectFill" @tap="changeAvatar"></image></view><view class="form-group"><text class="label">用户名:</text><input class="input" type="text" v-model="userInfo.username" placeholder="未填写用户名" /></view><view class="form-group"><text class="label">手机号:</text><input class="input" type="tel" v-model="userInfo.phone" placeholder="手机号" disabled/></view><view class="form-group"><text class="label">账号:</text><input class="input" type="text" v-model="userInfo.account" disabled /></view><view class="form-group"><text class="label">性别:</text><picker class="input" mode="selector" :range="genderOptions" @change="handleGenderChange"><text>{{ userInfo.gender||'未填写' }}</text></picker></view><view class="form-group"><text class="label">生日:</text><picker class="input" mode="date" @change="handleDateChange"><view>{{ userInfo.birthday||'未填写' }}</view></picker></view><view class="form-group"><text class="label">位置:</text><input class="input" type="text" v-model="userInfo.location" placeholder="未填写位置" /></view><view class="form-group"><text class="label">个人简介:</text><input class="input" type="text" v-model="userInfo.message" placeholder="这个人很懒,什么都没有留下" /></view><view class="form-group"><button class="btn" @click="updateUserInfo">更新信息</button></view></view>
</template><script>
export default {data() {return {genderOptions: ['男', '女', '其他'], // 性别选项列表defaultAvatar:'/static/logo.png',username:"",message:"",location:"",userInfo: {},};},onShow() {// 页面显示时调用的方法console.log('页面显示了');// 获取存储的数据this.userInfo = uni.getStorageSync('userInfo');this.gender+=this.userInfo.gender;// 检查存储的数据是否存在if (this.userInfo) {// 存储的数据存在,您可以在这里使用它console.log(this.userInfo);} else {// 如果存储的数据不存在,则可能出现错误或者之前没有存储任何数据console.log("没有找到存储的数据");}},methods: {handleGenderChange(event) {const index = event.detail.value; // 获取选择的索引this.userInfo.gender = this.genderOptions[index]; // 更新性别// 在这里可以添加其他逻辑,例如将选择的性别发送到后端或执行其他操作},handleDateChange(event) {// 处理日期选择器变化事件this.userInfo.birthday = event.detail.value;},changeAvatar() {uni.chooseImage({count: 1,success: (res) => {// 选择成功后将头像地址赋给userInfo.headshotthis.defaultAvatar = res.tempFilePaths[0];uni.uploadFile({url: this.$BASE_URL.BASE_URL+'/upload',name: 'file',filePath: this.defaultAvatar,success: function(uploadRes) {console.log(uploadRes.data);this.defaultAvatar=uploadRes.data;uni.setStorageSync('headshot', this.defaultAvatar);console.log(this.defaultAvatar)},fail(){uni.showToast({title:"请检查网络设置",icon:"none"})}})},});},updateUserInfo() {console.info(this.userInfo)this.username+=this.userInfo.username;this.message+=this.userInfo.message;this.location+=this.userInfo.location;console.log(this.defaultAvatar)this.userInfo.headshot=uni.getStorageSync('headshot', this.defaultAvatar);console.log(this.userInfo);if(this.username.length>20){uni.showToast({title:"用户名长度应小于20位",icon:"none"})return;};if(this.location.length>50){uni.showToast({title:"位置信息长度应小于20位",icon:"none"})return;};if(this.message.length>200){uni.showToast({title:"个人简介长度应小于200位",icon:"none"})return;}uni.request({url:this.$BASE_URL.BASE_URL+'/updata/user',method:"POST",data:this.userInfo,success: (res) => {if(res.data.code===1){uni.showToast({title:"信息修改成功",})uni.setStorageSync('userInfo', this.userInfo);uni.redirectTo({url:"/pages/userinformation/userinformation"})}else{uni.showToast({title:"信息修改失败",icon:"none"})}}})}, },
};
</script><style>
.error {color: #ff0000;font-size: 12px;
}
.container {padding: 20px;
}.header {text-align: center;margin-bottom: 20px;
}.title {font-size: 24px;font-weight: bold;
}.avatar {text-align: center;margin-bottom: 20px;
}.avatar-img {width: 100px;height: 100px;border-radius: 50%;
}.btn-edit-avatar {background-color: #007bff;color: #fff;border: none;padding: 8px 16px;border-radius: 20px;cursor: pointer;font-size: 14px;margin-top: 10px;
}.form-group {margin-bottom: 15px;display: flex;align-items: center;
}.label {width: 80px;margin-right: 10px;
}.input {flex: 1;padding: 5px;border: none;border-bottom: 1px solid #ccc;
}.btn {background-color: #007bff;color: #fff;border: none;padding: 10px 20px;border-radius: 5px;cursor: pointer;font-size: 16px;
}
</style>
复制代码

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/44961.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

uniapp 随机抽取视频播放

编写了一个视频播放界面,然后再从后端再随机读取10条视频信息,最后在我们前端app的页面上显示出来。<template><view class="index"><transition name="fade"><view class="video-container" @touchstart="touchSta…

uniapp手机号认证注册的一个页面

今天主要在uniapp设计了一个通过手机号认证注册的一个页面,但是今天只完成了前端页面的部分。并且能成功的连接上对应的后端地址。<template><view class="container"><view class="logo"><!-- 这里放置你的应用 logo --><ima…

增补博客 第二十九篇 计算机网络复习四

第五章 运输层 1.运输层的作用 运输层向它上面的应用层提供通信服务(提供端到端,进程到进程的可靠通信),为运行在不同host上的进程提供逻辑通信,向高层用户屏蔽通信子网的细节 2.UDP和TCP的特点,及使用它们的应用程序,熟知端口号 UDP和TCP的特点:UDP支持单播、多播、广…

LeetCode 633 Sum of Square Numbers All In One

LeetCode 633 Sum of Square Numbers All In One 633. Sum of Square Numbers Math.sqrt Math.trunc Math.powLeetCode 633 Sum of Square Numbers All In OneSum of Square NumberssolutionsMath.sqrt / 平方根function judgeSquareSum(c: number): boolean {let max = Math.t…

又跳槽!3年Java经验收割成都大厂的面试心得(干货满满文末有福利)

中厂->阿里->字节,成都->杭州->成都 系列文章目录和关于我 0.前言 笔者在不足两年经验的时候从成都一家金融科技中厂跳槽到杭州阿里淘天集团,又于今年5月份从杭州淘天跳槽到成都字节。自认为自己在面试这方面有一点心得,处于记录和分享的目的便有了此文,此文纯…

leetcode 206. 反转链表

题目描述 206. 反转链表给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。示例 1:输入:head = [1,2,3,4,5] 输出:[5,4,3,2,1]示例 2:输入:head = [1,2] 输出:[2,1]示例 3: 输入:head = [] 输出:[] 提示:链表中节点的数目范围是 [0, 5000],-5000 <= …

AgileConfig-1.9.4 发布,支持 OpenTelemetry

Hello 大家好,最新版的 AgileConfig 1.9.4 发布了。现在它可以通过 OpenTelemetry 对外提供 logs,traces,metrics 三个维度的数据。用户可以自由选择支持 otlp 协议的工具来进行查询与分析。比如 Seq,loki,prometheus, grafana 等等。 本来 AgileConfig 的日志是通过 Nlog…