Files
ttstd_family_care_miniapp/pages/home-layout/index.ts
T
tongtong 9030938c84 feat(miniapp): 补齐设备功能页面与统一 UI 规范
- 新增系统设置、壁纸、图标包、设备管理、通讯录、通话、短信、相册、文件、应用、告警、信息、首页布局、账号安全、绑定手机号、法律条款等页面
- utils/api.ts 补齐设备操作、策略、壁纸、图标包等 /api/v1/client/** 接口,新增多文件上传
- 新增 utils/sse.ts 与 utils/wx-session.ts
- 「更多/进入」指示统一改为图片箭头,首卡统一 .hero-card 高度,页面首卡贴顶
- AGENTS.md 补充功能对接状态与小程序 UI 约定
2026-11-28 18:19:19 +08:00

158 lines
4.2 KiB
TypeScript

import type { HomeLayoutPage } from '../../utils/api'
import { getHomeLayout, resetHomeLayout, saveHomeLayout } from '../../utils/api'
import { getDeviceSn } from '../../utils/token'
const SLOT_LABELS: Record<string, string> = {
home: '主页',
contact: '通讯录',
settings: '设置'
}
interface SwitchEvent {
detail: { value: boolean }
currentTarget: { dataset: Record<string, unknown> }
}
interface LayoutRow extends HomeLayoutPage {
label: string
}
Page({
data: {
rows: [] as LayoutRow[],
defaultPage: 'home',
saving: false
},
onShow() {
this.load()
},
async load() {
const sn = getDeviceSn()
if (!sn) {
this.toast('请先选择设备')
return
}
try {
const layout = await getHomeLayout(sn)
const rows: LayoutRow[] = (layout.pages || [])
.map((page, index) => ({
...page,
position: page.position ?? index,
hidden: !!page.hidden,
label: SLOT_LABELS[page.slot] || page.slot
}))
.sort((a, b) => a.position - b.position)
if (rows.length !== 3) {
this.toast('设备布局数据不完整,将使用默认三项')
this.setData({ rows: this.defaultRows(), defaultPage: layout.defaultPage || 'home' })
return
}
this.setData({ rows, defaultPage: layout.defaultPage || 'home' })
} catch (err) {
this.toast((err as Error).message || '布局读取失败')
}
},
defaultRows(): LayoutRow[] {
return ['home', 'contact', 'settings'].map((slot, position) => ({
slot,
position,
hidden: false,
label: SLOT_LABELS[slot] || slot
}))
},
onHiddenChange(e: SwitchEvent) {
const { index } = e.currentTarget.dataset as { index: number }
const rows = [...this.data.rows]
const row = rows[index]
if (!row) return
// 开关语义是“显示”,后端字段是 hidden,需要取反
row.hidden = !e.detail.value
this.setData({ rows })
},
onMoveUp(e: WechatMiniprogram.TouchEvent) {
this.move(Number(e.currentTarget.dataset.index), -1)
},
onMoveDown(e: WechatMiniprogram.TouchEvent) {
this.move(Number(e.currentTarget.dataset.index), 1)
},
move(index: number, delta: -1 | 1) {
const rows = [...this.data.rows]
const target = index + delta
if (target < 0 || target >= rows.length) return
const current = rows[index]
const next = rows[target]
if (!current || !next) return
rows[index] = next
rows[target] = current
this.setData({
rows: rows.map((row, position) => ({ ...row, position }))
})
},
onDefaultChange(e: WechatMiniprogram.TouchEvent) {
const { slot } = e.currentTarget.dataset as { slot: string }
this.setData({ defaultPage: slot })
},
async onSave() {
const sn = getDeviceSn()
if (!sn) return
if (this.data.saving) return
const rows = this.data.rows.map((row, position) => ({
slot: row.slot,
position,
hidden: !!row.hidden
}))
if (rows.length !== 3) {
this.toast('布局必须包含 3 个分页')
return
}
this.setData({ saving: true })
try {
await saveHomeLayout(sn, rows, this.data.defaultPage)
this.toast('布局已同步到设备')
} catch (err) {
this.toast((err as Error).message || '布局保存失败')
} finally {
this.setData({ saving: false })
}
},
onReset() {
const sn = getDeviceSn()
if (!sn) return
wx.showModal({
title: '恢复默认',
content: '确定恢复设备首页默认布局吗?',
success: (res) => {
if (!res.confirm) return
void resetHomeLayout(sn)
.then((layout) => {
this.setData({
rows: (layout.pages || []).map((page, index) => ({
...page,
label: SLOT_LABELS[page.slot] || page.slot,
position: page.position ?? index,
hidden: !!page.hidden
})),
defaultPage: layout.defaultPage || 'home'
})
this.toast('已恢复默认布局')
})
.catch((err: Error) => this.toast(err.message || '恢复失败'))
}
})
},
toast(title: string) {
wx.showToast({ title, icon: 'none', duration: 1800 })
}
})