import type { DeviceApp } from '../../utils/api' import { clearAppData, launchApp, listDeviceApps, stopApp, uninstallApp } from '../../utils/api' import { getDeviceSn } from '../../utils/token' Page({ data: { apps: [] as DeviceApp[] }, onShow() { this.load() }, async load() { const sn = getDeviceSn() if (!sn) { this.toast('请先选择设备') return } wx.showLoading({ title: '读取应用' }) try { this.setData({ apps: await listDeviceApps(sn) }) } catch (err) { this.toast((err as Error).message || '应用列表加载失败') } finally { wx.hideLoading() } }, onAction(e: WechatMiniprogram.TouchEvent) { const { packageName, appName } = e.currentTarget.dataset as { packageName: string appName: string } const sn = getDeviceSn() if (!sn || !packageName) return wx.showActionSheet({ itemList: ['启动应用', '停止应用', '清除数据', '卸载应用'], success: (res) => { const actions = [launchApp, stopApp, clearAppData, uninstallApp] const action = actions[res.tapIndex] if (!action) return const labels = ['启动', '停止', '清除数据', '卸载'] if (res.tapIndex === 3) { wx.showModal({ title: '卸载应用', content: `确定卸载「${appName || packageName}」吗?`, confirmColor: '#EF4444', success: (confirm) => { if (confirm.confirm) void this.run(action, sn, packageName, labels[res.tapIndex]) } }) return } void this.run(action, sn, packageName, labels[res.tapIndex]) } }) }, async run( action: (sn: string, packageName: string) => Promise, sn: string, packageName: string, label: string ) { wx.showLoading({ title: '处理中' }) try { await action(sn, packageName) wx.hideLoading() this.toast(`${label}指令已下发`) if (label === '卸载') setTimeout(() => this.load(), 1000) } catch (err) { wx.hideLoading() this.toast((err as Error).message || `${label}失败`) } }, toast(title: string) { wx.showToast({ title, icon: 'none', duration: 1800 }) } })