112 lines
2.8 KiB
TypeScript
112 lines
2.8 KiB
TypeScript
import type { DevicePicture } from '../../utils/api'
|
|
import {
|
|
deletePicture,
|
|
listDevicePictures,
|
|
sendPictureToDevice,
|
|
setPictureFavorite
|
|
} from '../../utils/api'
|
|
import { getDeviceSn } from '../../utils/token'
|
|
|
|
interface GalleryItem {
|
|
key: string
|
|
url: string
|
|
favorite: number
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
items: [] as GalleryItem[]
|
|
},
|
|
|
|
onShow() {
|
|
this.load()
|
|
},
|
|
|
|
async load() {
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.toast('请先选择设备')
|
|
return
|
|
}
|
|
try {
|
|
const pictures: DevicePicture[] = await listDevicePictures(sn)
|
|
this.setData({
|
|
items: pictures
|
|
.filter((item) => !!item.url)
|
|
.map((item, index) => ({
|
|
key: item.url || `picture-${index}`,
|
|
url: item.url || '',
|
|
favorite: item.favorite || 0
|
|
}))
|
|
})
|
|
} catch (err) {
|
|
this.toast((err as Error).message || '列表加载失败')
|
|
}
|
|
},
|
|
|
|
onSendPicture() {
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.toast('请先选择设备')
|
|
return
|
|
}
|
|
wx.chooseImage({
|
|
count: 1,
|
|
sizeType: ['compressed'],
|
|
sourceType: ['album', 'camera'],
|
|
success: (res) => {
|
|
const filePath = res.tempFilePaths[0]
|
|
if (!filePath) return
|
|
wx.showLoading({ title: '发送中' })
|
|
void sendPictureToDevice(sn, filePath)
|
|
.then(() => {
|
|
wx.hideLoading()
|
|
this.toast('图片已发送到设备')
|
|
return this.load()
|
|
})
|
|
.catch((err: Error) => {
|
|
wx.hideLoading()
|
|
this.toast(err.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 })
|
|
},
|
|
|
|
onFavorite(e: WechatMiniprogram.TouchEvent) {
|
|
const { url, favorite } = e.currentTarget.dataset as { url: string; favorite: number }
|
|
const sn = getDeviceSn()
|
|
if (!sn) return
|
|
const next: 0 | 1 = favorite === 1 ? 0 : 1
|
|
void setPictureFavorite(sn, url, next)
|
|
.then(() => this.load())
|
|
.catch((err: Error) => this.toast(err.message || '操作失败'))
|
|
},
|
|
|
|
onDelete(e: WechatMiniprogram.TouchEvent) {
|
|
const { url } = e.currentTarget.dataset as { url: string }
|
|
const sn = getDeviceSn()
|
|
if (!sn) return
|
|
wx.showModal({
|
|
title: '删除',
|
|
content: '确定删除这张图片吗?',
|
|
confirmColor: '#EF4444',
|
|
success: (res) => {
|
|
if (!res.confirm) return
|
|
void deletePicture(sn, url)
|
|
.then(() => this.load())
|
|
.catch((err: Error) => this.toast(err.message || '删除失败'))
|
|
}
|
|
})
|
|
},
|
|
|
|
toast(title: string) {
|
|
wx.showToast({ title, icon: 'none', duration: 1800 })
|
|
}
|
|
})
|