Files
ttstd_family_care_miniapp/pages/alarm-edit/index.ts
T

129 lines
3.8 KiB
TypeScript

import type { DeviceAlarm } from '../../utils/api'
import { addAlarm, updateAlarm } from '../../utils/api'
import { getDeviceSn } from '../../utils/token'
interface SwitchEvent {
detail: { value: boolean }
currentTarget: { dataset: Record<string, unknown> }
}
interface PickerEvent {
detail: { value: number | string }
currentTarget: { dataset: Record<string, unknown> }
}
/** 重复类型文案,索引即后端 repeatType */
const REPEAT_LABELS = ['一次', '每天', '工作日', '自定义']
const emptyAlarm = (): DeviceAlarm => ({
hour: 7,
minute: 0,
label: '',
repeatType: 1,
isEnabled: 1,
vibration: 1,
volume: 60,
snoozeInterval: 5
})
/** 补零到两位,picker 的 time 值固定为 HH:mm */
const pad2 = (value?: number): string => String(Math.max(0, Number(value) || 0)).padStart(2, '0')
/** 由闹钟时/分生成 picker 需要的 "HH:mm" */
const timeOf = (alarm: DeviceAlarm): string => `${pad2(alarm.hour)}:${pad2(alarm.minute)}`
Page({
data: {
form: emptyAlarm(),
timeValue: timeOf(emptyAlarm()),
repeatLabels: REPEAT_LABELS,
repeatLabel: REPEAT_LABELS[1],
editing: false,
saving: false
},
/** 编辑模式:列表页通过 eventChannel 传入被编辑的闹钟,避免再查一次列表 */
onLoad(options: Record<string, string | undefined>) {
const id = Number(options.id)
if (!(id > 0)) return
this.setData({ editing: true })
wx.setNavigationBarTitle({ title: '编辑闹钟' })
this.getOpenerEventChannel().on?.('alarm', (alarm: DeviceAlarm) => {
if (!alarm) return
this.setData({
form: { ...alarm },
timeValue: timeOf(alarm),
repeatLabel: REPEAT_LABELS[Number(alarm.repeatType)] || REPEAT_LABELS[1]
})
})
},
/** 时间选择器:上下滑动选出 HH:mm,拆成时/分写回表单 */
onTimeChange(e: WechatMiniprogram.PickerChange) {
const timeValue = String(e.detail.value)
const [hour, minute] = timeValue.split(':').map(Number)
this.setData({
timeValue,
form: { ...this.data.form, hour, minute }
})
},
onNumberInput(e: WechatMiniprogram.Input) {
const { field } = e.currentTarget.dataset as { field: 'volume' }
const value = Number(e.detail.value)
this.setData({ form: { ...this.data.form, [field]: Number.isFinite(value) ? value : 0 } })
},
onTextInput(e: WechatMiniprogram.Input) {
const { field } = e.currentTarget.dataset as { field: 'label' | 'customDays' }
this.setData({ form: { ...this.data.form, [field]: e.detail.value } })
},
onPicker(e: PickerEvent) {
const repeatType = Number(e.detail.value)
this.setData({
form: { ...this.data.form, repeatType },
repeatLabel: REPEAT_LABELS[repeatType] || REPEAT_LABELS[1]
})
},
onSwitch(e: SwitchEvent) {
const { field } = e.currentTarget.dataset as { field: 'isEnabled' | 'vibration' }
this.setData({
form: { ...this.data.form, [field]: e.detail.value ? 1 : 0 }
})
},
async onSave() {
const form = this.data.form
if (!(Number(form.hour) >= 0) || !(Number(form.minute) >= 0)) {
this.toast('请填写闹钟时间')
return
}
const sn = getDeviceSn()
if (!sn) {
this.toast('请先选择设备')
return
}
if (this.data.saving) return
this.setData({ saving: true })
wx.showLoading({ title: '保存中' })
try {
if (this.data.editing && form.id) await updateAlarm(sn, form)
else await addAlarm(sn, form)
wx.hideLoading()
this.toast('闹钟已同步到设备')
wx.navigateBack()
} catch (err) {
wx.hideLoading()
this.toast((err as Error).message || '保存失败')
} finally {
this.setData({ saving: false })
}
},
toast(title: string) {
wx.showToast({ title, icon: 'none', duration: 1800 })
}
})