75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import type { ContactItem } from '../../utils/api'
|
|
import { deleteContact, listContacts } from '../../utils/api'
|
|
import { getDeviceSn } from '../../utils/token'
|
|
|
|
Page({
|
|
data: {
|
|
contacts: [] as ContactItem[],
|
|
loading: false
|
|
},
|
|
|
|
onShow() {
|
|
this.load()
|
|
},
|
|
|
|
async load() {
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.toast('请先选择设备')
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
this.setData({ contacts: await listContacts(sn) })
|
|
} catch (err) {
|
|
this.toast((err as Error).message || '通讯录加载失败')
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
/** 新增与编辑统一走独立的联系人编辑页,保存后返回本页刷新列表 */
|
|
onAdd() {
|
|
if (!this.ensureDevice()) return
|
|
wx.navigateTo({ url: '/pages/contact-edit/index' })
|
|
},
|
|
|
|
onEdit(e: WechatMiniprogram.TouchEvent) {
|
|
if (!this.ensureDevice()) return
|
|
const { id } = e.currentTarget.dataset as { id: number }
|
|
wx.navigateTo({ url: `/pages/contact-edit/index?id=${id}` })
|
|
},
|
|
|
|
ensureDevice(): boolean {
|
|
if (!getDeviceSn()) {
|
|
this.toast('请先选择设备')
|
|
return false
|
|
}
|
|
return true
|
|
},
|
|
|
|
onDelete(e: WechatMiniprogram.TouchEvent) {
|
|
const { id } = e.currentTarget.dataset as { id: number }
|
|
const sn = getDeviceSn()
|
|
if (!sn) return
|
|
wx.showModal({
|
|
title: '删除联系人',
|
|
content: '确定删除该联系人吗?',
|
|
confirmColor: '#EF4444',
|
|
success: (res) => {
|
|
if (!res.confirm) return
|
|
void deleteContact(sn, id)
|
|
.then(() => {
|
|
this.toast('已删除')
|
|
return this.load()
|
|
})
|
|
.catch((err: Error) => this.toast(err.message || '删除失败'))
|
|
}
|
|
})
|
|
},
|
|
|
|
toast(title: string) {
|
|
wx.showToast({ title, icon: 'none', duration: 1800 })
|
|
}
|
|
})
|