feat(home,my): 首页和我的页从缓存回显用户信息

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-08-24 15:31:12 +08:00
co-authored by Claude Sonnet 4.6
parent 90f84cc7df
commit e25e797b35
6 changed files with 84 additions and 12 deletions
+9 -2
View File
@@ -59,7 +59,13 @@
overflow: hidden; overflow: hidden;
border-radius: 24rpx; border-radius: 24rpx;
margin-right: 15rpx; margin-right: 15rpx;
background-color: red; color: #FFFFFF;
font-size: 40rpx;
font-weight: bold;
line-height: 88rpx;
text-align: center;
border: 5rpx solid #ffffff;
background-color: #1385FA;
} }
.userInfo { .userInfo {
@@ -70,8 +76,9 @@
color: #252535; color: #252535;
height: 32rpx; height: 32rpx;
font-size: 32rpx; font-size: 32rpx;
font-weight: bolder;
line-height: 32rpx; line-height: 32rpx;
margin-bottom: 20rpx; margin-bottom: 16rpx;
} }
.userSex { .userSex {
+25
View File
@@ -1,4 +1,14 @@
import { get } from '../../utils/request/index' import { get } from '../../utils/request/index'
import { calcAge } from '../../utils/idCard/index'
import type { UserInfo } from '../../api/index'
/** 页面展示用用户信息 */
interface PageUserInfo {
realname: string
sex_dictText: string
age: number
height: number
}
/** 将体重数字拆成整数 + 小数字符串 */ /** 将体重数字拆成整数 + 小数字符串 */
const splitWeight = (weight: number): { int: number; decimal: string } => { const splitWeight = (weight: number): { int: number; decimal: string } => {
@@ -10,10 +20,25 @@ const splitWeight = (weight: number): { int: number; decimal: string } => {
Page({ Page({
data: { data: {
record: null, record: null,
userInfo: null as PageUserInfo | null,
}, },
onShow() { onShow() {
this._loadUserInfo()
},
// 从缓存回显用户信息
_loadUserInfo() {
const raw = wx.getStorageSync('userInfo') as UserInfo | null
if (!raw?.userId) return
this.setData({
userInfo: {
realname: raw.realname ?? '',
sex_dictText: raw.sex === 1 ? '男' : raw.sex === 2 ? '女' : '',
age: calcAge(raw.birthday),
height: raw.height ?? 0,
},
})
}, },
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent { onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
+4 -4
View File
@@ -9,11 +9,11 @@
<view class="title_2">设备智联 · 掌握健康</view> <view class="title_2">设备智联 · 掌握健康</view>
</view> </view>
<view class="user"> <view class="user" wx:if="{{ userInfo }}">
<view class="userAvatar"></view> <view class="userAvatar">{{ userInfo.realname[0] }}</view>
<view class="userInfo"> <view class="userInfo">
<view class="userName">张晓敏</view> <view class="userName">{{ userInfo.realname }}</view>
<view class="userSex">男·36岁<text>丨</text>172cm</view> <view class="userSex">{{ userInfo.sex_dictText }}·{{ userInfo.age }}岁<text>丨</text>{{ userInfo.height }}cm</view>
</view> </view>
</view> </view>
+26
View File
@@ -1,3 +1,14 @@
import { calcAge } from '../../utils/idCard/index'
import type { UserInfo } from '../../api/index'
/** 页面展示用用户信息 */
interface PageUserInfo {
realname: string
sex_dictText: string
age: number
height: number
}
// pages/my/my.ts // pages/my/my.ts
Page({ Page({
@@ -7,6 +18,7 @@ Page({
data: { data: {
// 导航栏背景色,根据滚动位置动态变化 // 导航栏背景色,根据滚动位置动态变化
navBgColor: 'rgba(255,255,255,0)', navBgColor: 'rgba(255,255,255,0)',
userInfo: null as PageUserInfo | null,
}, },
/** /**
@@ -44,7 +56,21 @@ Page({
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
*/ */
onShow() { onShow() {
this._loadUserInfo()
},
// 从缓存回显用户信息
_loadUserInfo() {
const raw = wx.getStorageSync('userInfo') as UserInfo | null
if (!raw?.userId) return
this.setData({
userInfo: {
realname: raw.realname ?? '',
sex_dictText: raw.sex === 1 ? '男' : raw.sex === 2 ? '女' : '',
age: calcAge(raw.birthday),
height: raw.height ?? 0,
},
})
}, },
/** /**
+5 -5
View File
@@ -6,14 +6,14 @@
<image src="/images/my/bg.png" mode="aspectFit" /> <image src="/images/my/bg.png" mode="aspectFit" />
</view> </view>
<view class="user"> <view class="user" wx:if="{{ userInfo }}">
<view class="userAvatar"></view> <view class="userAvatar">{{ userInfo.realname[0] }}</view>
<view class="userInfo"> <view class="userInfo">
<view class="userName">陈沐辰</view> <view class="userName">{{ userInfo.realname }}</view>
<view class="userSex"> <view class="userSex">
<text class="meta-text">男·36岁</text> <text class="meta-text">{{ userInfo.sex_dictText }}·{{ userInfo.age }}岁</text>
<text class="meta-divider">|</text> <text class="meta-divider">|</text>
<text class="meta-text">172cm</text> <text class="meta-text">{{ userInfo.height }}cm</text>
</view> </view>
</view> </view>
</view> </view>
+14
View File
@@ -38,3 +38,17 @@ export const parseIdCard = (idCard: string): IdCardParsed => {
sex, sex,
} }
} }
/** 从生日字符串(YYYY-MM-DD)计算周岁 */
export const calcAge = (birthday: string): number => {
if (!birthday) return 0
const parts = birthday.split('-').map(Number)
const [year, month, day] = parts
if (!year || !month || !day) return 0
const now = new Date()
let age = now.getFullYear() - year
const nowMonth = now.getMonth() + 1
const nowDay = now.getDate()
if (nowMonth < month || (nowMonth === month && nowDay < day)) age -= 1
return Math.max(0, age)
}