Files
bodyWeight/miniprogram/pages/my/my.ts
T

55 lines
1.5 KiB
TypeScript

/** 缓存中的用户信息 */
interface StorageUserInfo {
userId: string
realname: string
sex: number
height: number
birthday: string
[key: string]: any
}
/** 页面展示用用户信息 */
interface PageUserInfo {
realname: string
sex_dictText: '男' | '女' | ''
age: number
height: number
}
/** 从生日字符串(YYYY-MM-DD)计算周岁 */
const calcAge = (birthday: string): number => {
if (!birthday) return 0
const [year, month, day] = birthday.split('-').map(Number)
if (!year || !month || !day) return 0
const now = new Date()
let age = now.getFullYear() - year
if (now.getMonth() + 1 < month || (now.getMonth() + 1 === month && now.getDate() < day)) age -= 1
return Math.max(0, age)
}
Page({
data: {
userInfo: null as PageUserInfo | null,
},
onShow() {
const raw = wx.getStorageSync('userInfo') as StorageUserInfo | null
if (!raw?.userId) return
const userInfo: PageUserInfo = {
realname: raw.realname ?? '',
sex_dictText: raw.sex === 1 ? '男' : raw.sex === 2 ? '女' : '',
age: calcAge(raw.birthday),
height: raw.height ?? 0,
}
this.setData({ userInfo })
},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
})