Files
ttstd_family_care_miniapp/pages/screenshot/index.ts
T

69 lines
1.6 KiB
TypeScript

import type { Screenshot } from '../../utils/api'
import { deleteScreenshot, getScreenshots } from '../../utils/api'
import { getDeviceSn } from '../../utils/token'
interface ShotItem {
key: string
id: number
url: string
}
Page({
data: {
items: [] as ShotItem[]
},
onShow() {
this.load()
},
async load() {
const sn = getDeviceSn()
if (!sn) {
this.toast('请先选择设备')
return
}
try {
const shots: Screenshot[] = await getScreenshots(sn)
this.setData({
items: shots
.filter((item) => !!item.url)
.map((item) => ({
key: String(item.id),
id: item.id,
url: item.url || ''
}))
})
} catch (err) {
this.toast((err as Error).message || '列表加载失败')
}
},
onPreview(e: WechatMiniprogram.TouchEvent) {
const { url } = e.currentTarget.dataset as { url: string }
const urls = this.data.items.map((item) => item.url).filter(Boolean)
wx.previewImage({ current: url, urls })
},
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 deleteScreenshot(sn, id)
.then(() => this.load())
.catch((err: Error) => this.toast(err.message || '删除失败'))
}
})
},
toast(title: string) {
wx.showToast({ title, icon: 'none', duration: 1800 })
}
})