67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import { rebootDevice, restoreDevice, shutdownDevice } from '../../utils/api'
|
|
import { getDeviceSn } from '../../utils/token'
|
|
|
|
interface MoreOp {
|
|
key: string
|
|
emoji: string
|
|
color: string
|
|
label: string
|
|
}
|
|
|
|
/** 不宜放在首页直接点击的危险操作,需二次确认后下发 */
|
|
Page({
|
|
data: {
|
|
ops: [
|
|
{ key: 'reboot', emoji: '🔄', color: '#5B8DEF', label: '重启' },
|
|
{ key: 'shutdown', emoji: '🔌', color: '#EF4444', label: '关机' },
|
|
{ key: 'restore', emoji: '♻️', color: '#F5A623', label: '恢复出厂' }
|
|
] as MoreOp[],
|
|
busy: false
|
|
},
|
|
|
|
onOp(e: WechatMiniprogram.TouchEvent) {
|
|
const { key, label } = e.currentTarget.dataset as { key: string; label: string }
|
|
if (!getDeviceSn()) {
|
|
this.toast('请先在首页选择设备')
|
|
return
|
|
}
|
|
wx.showModal({
|
|
title: '确认操作',
|
|
content: `确定要向设备发送「${label}」指令吗?${key === 'restore' ? '此操作不可撤销。' : ''}`,
|
|
confirmText: '确定',
|
|
confirmColor: key === 'restore' ? '#EF4444' : '#07C160',
|
|
cancelText: '取消',
|
|
success: (res) => {
|
|
if (res.confirm) void this.runOp(key, label)
|
|
}
|
|
})
|
|
},
|
|
|
|
async runOp(key: string, label: string): Promise<void> {
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.toast('请先在首页选择设备')
|
|
return
|
|
}
|
|
if (this.data.busy) return
|
|
this.setData({ busy: true })
|
|
wx.showLoading({ title: '处理中' })
|
|
try {
|
|
if (key === 'reboot') await rebootDevice(sn)
|
|
else if (key === 'shutdown') await shutdownDevice(sn)
|
|
else await restoreDevice(sn)
|
|
wx.hideLoading()
|
|
this.toast(`${label}指令已下发`)
|
|
} catch (err) {
|
|
wx.hideLoading()
|
|
this.toast((err as Error).message || `${label}失败`)
|
|
} finally {
|
|
this.setData({ busy: false })
|
|
}
|
|
},
|
|
|
|
toast(title: string) {
|
|
wx.showToast({ title, icon: 'none', duration: 1800 })
|
|
}
|
|
})
|