import { get } from '../../utils/request/index' /** 接口 type 参数:0=全部 1=近7天 2=近30天 3=近3月 */ type RangeType = 0 | 1 | 2 | 3 /** 时间范围 Tab 项 */ interface RangeTab { key: RangeType label: string } /** 缓存中的用户信息 */ interface StorageUserInfo { userId: string realname: string sex: number height: number birthday: string avatar: string | null relationType: number | string | null [key: string]: any } /** 页面展示用用户信息 */ interface PageUserInfo extends StorageUserInfo { sex_dictText: '男' | '女' | '' age: number isSelf: boolean } /** GET /weighingScale/v3/getMeasureStatistics 返回结构 */ interface MeasureStatistics { maxWeight: number minWeight: number avgWeight: number weightChange: number } /** GET /weighingScale/v3/getMeasureHistoryPage records 单条 */ interface MeasureRecord { recordId: string examTime: string weight: number bmi: number fatRate: number } /** 统计空态默认值 */ const EMPTY_STATS: MeasureStatistics = { maxWeight: 0, minWeight: 0, avgWeight: 0, weightChange: 0, } /** 从生日字符串(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: { activeType: 1 as RangeType, rangeTabs: [ { key: 1, label: '近7天' }, { key: 2, label: '近30天' }, { key: 3, label: '近3月' }, ] as RangeTab[], userInfo: null as PageUserInfo | null, stats: EMPTY_STATS as MeasureStatistics, records: [] as MeasureRecord[], pageNo: 1, total: 0, hasMore: false, refresherTriggered: false, showSelectDrawer: false, }, onLoad() { const raw = wx.getStorageSync('userInfo') as StorageUserInfo | null if (!raw?.userId) return const userInfo: PageUserInfo = { ...raw, sex_dictText: raw.sex === 1 ? '男' : raw.sex === 2 ? '女' : '', age: calcAge(raw.birthday), isSelf: true, } this.setData({ userInfo }) this.loadData() }, /** 重置分页,并行拉统计 + 第一页列表 */ loadData() { const userInfo = this.data.userInfo if (!userInfo?.userId) return this.setData({ status: 'loading', pageNo: 1, records: [] }) const statsReq = get('weighingScale/v3/getMeasureStatistics', { userId: userInfo.userId, type: this.data.activeType, }, { loading: false }) const pageReq = get('weighingScale/v3/getMeasureHistoryPage', { userId: userInfo.userId, type: this.data.activeType, pageNo: 1, pageSize: 10, }, { loading: false }) wx.showLoading({ title: '加载中...', mask: true }) Promise.all([statsReq, pageReq]) .then(([statsRes, pageRes]: any[]) => { const rawStats = statsRes.result const stats: MeasureStatistics = rawStats ? { maxWeight: rawStats.maxWeight ?? 0, minWeight: rawStats.minWeight ?? 0, avgWeight: rawStats.avgWeight ?? 0, weightChange: rawStats.weightChange ?? 0, } : EMPTY_STATS const page = pageRes.result ?? {} const records: MeasureRecord[] = (page.records ?? []).map((r: any) => ({ recordId: r.recordId ?? '', examTime: r.examTime ?? '', weight: r.weight ?? 0, bmi: r.bmi ?? 0, fatRate: r.fatRate ?? 0, })) const total = page.total ?? 0 this.setData({ stats, records, total, hasMore: records.length < total, }) }) .finally(() => { wx.hideLoading() this.setData({ refresherTriggered: false }) }) }, /** scroll-view 触底,加载下一页 */ onScrollToLower() { if (!this.data.hasMore) return const userInfo = this.data.userInfo if (!userInfo?.userId) return const nextPage = this.data.pageNo + 1 wx.showLoading({ title: '加载中...', mask: true }) get('weighingScale/v3/getMeasureHistoryPage', { userId: userInfo.userId, type: this.data.activeType, pageNo: nextPage, pageSize: 10, }, { loading: false }) .then((res: any) => { const page = res.result ?? {} const newRecords: MeasureRecord[] = (page.records ?? []).map((r: any) => ({ recordId: r.recordId ?? '', examTime: r.examTime ?? '', weight: r.weight ?? 0, bmi: r.bmi ?? 0, fatRate: r.fatRate ?? 0, })) const allRecords = [...this.data.records, ...newRecords] this.setData({ records: allRecords, pageNo: nextPage, hasMore: allRecords.length < this.data.total, }) }) .finally(() => { wx.hideLoading() }) }, /** scroll-view 下拉刷新 */ onRefresh() { this.setData({ refresherTriggered: true }) this.loadData() }, /** 切换时间范围 Tab */ onTapRange(e: WechatMiniprogram.TouchEvent) { const key = Number(e.currentTarget.dataset.key) as RangeType if (key === this.data.activeType) return this.setData({ activeType: key }) this.loadData() }, /** 点击记录行,携带 recordId + userId 跳转测量报告 */ onTapRecord(e: WechatMiniprogram.TouchEvent) { const recordId = e.currentTarget.dataset.recordId as string const userId = this.data.userInfo?.userId ?? '' wx.navigateTo({ url: `/pages/measurementReport/measurementReport?recordId=${recordId}&userId=${userId}`, }) }, /** 打开切换成员抽屉 */ onTapSwitchMember() { this.setData({ showSelectDrawer: true }) }, /** 抽屉关闭 */ onCloseDrawer() { this.setData({ showSelectDrawer: false }) }, /** 选中成员后更新 userInfo 并重新请求数据 */ onSelectMember(e: WechatMiniprogram.CustomEvent) { const m = e.detail as { userId?: string id: string name: string gender: '男' | '女' age: number height: number sex?: number birthday?: string relationType?: number | string | null avatar?: string } const selectedUserId = m.userId ?? m.id const storedUserId = (wx.getStorageSync('userInfo') as StorageUserInfo | null)?.userId ?? '' const userInfo: PageUserInfo = { userId: selectedUserId, realname: m.name, sex: m.sex ?? (m.gender === '男' ? 1 : 2), height: m.height, birthday: m.birthday ?? '', avatar: m.avatar ?? null, relationType: m.relationType ?? null, sex_dictText: m.gender, age: m.age, isSelf: selectedUserId === storedUserId, } this.setData({ showSelectDrawer: false, userInfo }) this.loadData() }, })