Files
ttstd_family_care_miniapp/pages/device-bind/index.ts
T

83 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { bindDevice, bindDeviceRequest } from '../../utils/api'
import { saveDeviceSn } from '../../utils/token'
Page({
data: {
sn: '',
remark: '',
submitting: false
},
onSnInput(e: WechatMiniprogram.Input) {
this.setData({ sn: e.detail.value.trim() })
},
onRemarkInput(e: WechatMiniprogram.Input) {
this.setData({ remark: e.detail.value })
},
/** 扫码绑定:二维码原文即设备 SN(与 Flutter 设备扫码页一致),识别后填入表单 */
onScan() {
wx.scanCode({
success: (res) => {
const sn = (res.result || '').trim()
if (!sn) {
this.toast('未识别到有效内容')
return
}
this.setData({ sn })
this.toast('已填入设备 SN')
},
fail: (err) => {
if (err.errMsg && err.errMsg.indexOf('cancel') >= 0) return
this.toast('扫码失败,请重试或手动输入 SN')
}
})
},
onBind() {
const sn = this.data.sn
if (!sn) {
this.toast('请输入设备序列号')
return
}
if (this.data.submitting) return
wx.showActionSheet({
itemList: ['直接绑定(已知 SN)', '发起绑定申请(设备确认)'],
success: (res) => {
void this.doBind(res.tapIndex === 1)
}
})
},
async doBind(byRequest: boolean) {
const sn = this.data.sn
this.setData({ submitting: true })
wx.showLoading({ title: '提交中' })
try {
if (byRequest) {
const message = await bindDeviceRequest(sn, this.data.remark)
wx.hideLoading()
this.setData({ submitting: false })
this.toast(message || '绑定申请已下发')
} else {
await bindDevice(sn)
saveDeviceSn(sn)
wx.hideLoading()
this.setData({ submitting: false })
this.toast('绑定成功')
}
// 提示浮层不随页面返回消失,直接回到设备管理页由 onShow 刷新列表
wx.navigateBack()
} catch (err) {
wx.hideLoading()
this.setData({ submitting: false })
this.toast((err as Error).message || '绑定失败')
}
},
toast(title: string) {
wx.showToast({ title, icon: 'none', duration: 1800 })
}
})