- fix manual bind stuck at BLE scan, redirect to bind-success - add 15s timeout and stopScan to ble-connect - add stopScan method to ble.js - add back-to-home button on bind-success page - fix field name in subscribe-plans (plan -> plan_type) - fix history.js to handle created_at field - fix index.js to handle device name field - add subscription route alias in request.js - extend admin cloud function from 1 to 9 actions - add mock mode to admin-console request.js - remove unused discover page and legacy record function Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
88 行
2.2 KiB
JavaScript
88 行
2.2 KiB
JavaScript
var http = require('../../utils/request')
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
scanning: false,
|
|
deviceId: '',
|
|
error: ''
|
|
},
|
|
|
|
onLoad: function () {
|
|
var app = getApp()
|
|
this.setData({ statusBarHeight: app.globalData.statusBarHeight })
|
|
},
|
|
|
|
onScan: function () {
|
|
var self = this
|
|
self.setData({ scanning: true, error: '' })
|
|
|
|
wx.scanCode({
|
|
onlyFromCamera: true,
|
|
scanType: ['qrCode'],
|
|
success: function (res) {
|
|
var deviceId = self.parseDeviceId(res.result)
|
|
if (!deviceId) {
|
|
self.setData({ scanning: false, error: '无效的设备二维码' })
|
|
return
|
|
}
|
|
self.bindDevice(deviceId)
|
|
},
|
|
fail: function () {
|
|
self.setData({ scanning: false })
|
|
}
|
|
})
|
|
},
|
|
|
|
parseDeviceId: function (content) {
|
|
if (/^[0-9A-Fa-f]{16}$/.test(content)) return content
|
|
try {
|
|
var url = new URL(content)
|
|
var id = url.searchParams.get('device_id') || url.searchParams.get('id')
|
|
return id || null
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
},
|
|
|
|
bindDevice: function (deviceId) {
|
|
var self = this
|
|
self.setData({ scanning: true, error: '' })
|
|
http.post('/api/v1/device/bind', { device_id: deviceId }).then(function (data) {
|
|
self.setData({ scanning: false })
|
|
wx.redirectTo({
|
|
url: '/pages/bind-success/bind-success?device_id=' + deviceId
|
|
})
|
|
}).catch(function (err) {
|
|
self.setData({ scanning: false })
|
|
if (err && err.code === 2001) {
|
|
wx.showToast({ title: '已绑定设备', icon: 'none' })
|
|
setTimeout(function () {
|
|
wx.navigateBack()
|
|
}, 1500)
|
|
} else {
|
|
self.setData({ error: err.message || '绑定失败' })
|
|
}
|
|
})
|
|
},
|
|
|
|
onManualInput: function () {
|
|
var self = this
|
|
wx.showModal({
|
|
title: '手动输入设备号',
|
|
editable: true,
|
|
placeholderText: '请输入16位设备ID',
|
|
success: function (res) {
|
|
if (res.confirm && res.content) {
|
|
var deviceId = res.content.trim()
|
|
if (/^[0-9A-Fa-f]{16}$/.test(deviceId)) {
|
|
self.bindDevice(deviceId)
|
|
} else {
|
|
wx.showToast({ title: '设备号格式不正确', icon: 'none' })
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|