[AI Generated]: feat(*): 新增 lefu 蓝牙服务层、mock 业务数据层及 request 请求封装,接入各页面
This commit is contained in:
@@ -1,102 +1,102 @@
|
||||
/** 设备项模型 */
|
||||
import { lefuService } from '../../lefu/index'
|
||||
import type { ScannedDevice } from '../../lefu/index'
|
||||
|
||||
/** 展示用设备项 */
|
||||
interface DeviceItem {
|
||||
/** 设备唯一 id */
|
||||
id: string
|
||||
/** 设备名称 */
|
||||
name: string
|
||||
/** 设备图标 */
|
||||
icon: string
|
||||
/** 是否已连接 */
|
||||
connected: boolean
|
||||
/** 原始设备对象(连接时传回给 SDK) */
|
||||
_raw: ScannedDevice
|
||||
}
|
||||
|
||||
/** 页面状态 */
|
||||
type DeviceStatus = 'idle' | 'searching' | 'found' | 'empty'
|
||||
|
||||
// 是否 mock 能搜索到设备,false 则走空状态
|
||||
const MOCK_HAS_DEVICES = true
|
||||
|
||||
// 模拟搜索耗时(毫秒)
|
||||
const MOCK_SEARCH_DURATION = 2000
|
||||
|
||||
Page({
|
||||
/* 搜索定时器引用,onUnload 时清理 */
|
||||
_searchTimer: null as number | null,
|
||||
|
||||
data: {
|
||||
/**
|
||||
* 页面状态
|
||||
* - idle: 未搜索(进入页面默认态)
|
||||
* - searching:搜索中
|
||||
* - found: 搜索完成且有设备
|
||||
* - empty: 搜索完成但无设备
|
||||
* - idle: 未搜索(进入页面默认态)
|
||||
* - searching: 扫描中
|
||||
* - found: 扫描完成且有设备
|
||||
* - empty: 扫描完成但无设备
|
||||
*/
|
||||
status: 'idle' as DeviceStatus,
|
||||
|
||||
/* 设备列表(包含已连接和未连接,每项含 connected: boolean) */
|
||||
/** 扫描到的设备列表 */
|
||||
deviceList: [] as DeviceItem[],
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
onShow() {
|
||||
// 注册回调后开始扫描
|
||||
lefuService.onDevicesList((devices) => {
|
||||
if (!devices.length) {
|
||||
this.setData({ status: 'empty', deviceList: [] })
|
||||
return
|
||||
}
|
||||
const list: DeviceItem[] = devices.map(d => ({
|
||||
name: d.name,
|
||||
connected: false,
|
||||
_raw: d,
|
||||
}))
|
||||
this.setData({ status: 'found', deviceList: list })
|
||||
})
|
||||
|
||||
lefuService.onConnectState((state) => {
|
||||
const plugin = lefuService['_plugin']
|
||||
if (!plugin) return
|
||||
// 连接成功后跳转配网页
|
||||
if (state === plugin.BLUE_STATE.CONNECTSUCCESS || state === plugin.BLUE_STATE.WIFISUCCESS) {
|
||||
wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' })
|
||||
}
|
||||
})
|
||||
|
||||
this.onStartSearch()
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 页面隐藏时停止扫描,释放 bus 订阅
|
||||
lefuService.stopScan()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
/* 清除搜索定时器,防止页面卸载后还触发 setData */
|
||||
if (this._searchTimer) {
|
||||
clearTimeout(this._searchTimer)
|
||||
this._searchTimer = null
|
||||
}
|
||||
lefuService.stopScan()
|
||||
},
|
||||
|
||||
/* 开始搜索 / 重新搜索 */
|
||||
/** 开始 / 重新搜索 */
|
||||
onStartSearch() {
|
||||
this.setData({
|
||||
status: 'searching',
|
||||
deviceList: [],
|
||||
})
|
||||
|
||||
/* 模拟搜索,后续接入蓝牙真实 API 时删除 */
|
||||
this._searchTimer = setTimeout(() => {
|
||||
if (MOCK_HAS_DEVICES) {
|
||||
this.setData({
|
||||
status: 'found',
|
||||
deviceList: [
|
||||
{ id: 'mock-connected', name: '智能体重秤 Pro', icon: '', connected: true },
|
||||
{ id: 'mock-1', name: '体重秤 Basic', icon: '', connected: false },
|
||||
{ id: 'mock-2', name: '体脂秤 Plus', icon: '', connected: false },
|
||||
],
|
||||
})
|
||||
} else {
|
||||
this.setData({ status: 'empty' })
|
||||
}
|
||||
}, MOCK_SEARCH_DURATION)
|
||||
this.setData({ status: 'searching', deviceList: [] })
|
||||
lefuService.startScan()
|
||||
},
|
||||
|
||||
/* 连接某台设备:同时只能一台已连接,自动断开其他设备 */
|
||||
/** 连接指定设备 */
|
||||
onConnect(e: WechatMiniprogram.TouchEvent) {
|
||||
const { id } = e.currentTarget.dataset
|
||||
const deviceList = this.data.deviceList.map(item => ({
|
||||
const index = e.currentTarget.dataset.index as number
|
||||
const target = this.data.deviceList[index]
|
||||
if (!target) return
|
||||
|
||||
// 更新 UI 连接状态
|
||||
const list = this.data.deviceList.map((item, i) => ({
|
||||
...item,
|
||||
connected: item.id === id,
|
||||
connected: i === index,
|
||||
}))
|
||||
this.setData({ deviceList })
|
||||
/* TODO: 接入真实蓝牙连接逻辑 */
|
||||
this.setData({ deviceList: list })
|
||||
|
||||
// 调用 SDK 连接(BleAdv 类型已在 startScan 内自动处理,BleConnect 类型需此处触发)
|
||||
lefuService.connect(target._raw)
|
||||
},
|
||||
|
||||
/* 断开当前已连接设备 */
|
||||
/** 断开当前连接 */
|
||||
onDisconnect() {
|
||||
const deviceList = this.data.deviceList.map(item => ({
|
||||
...item,
|
||||
connected: false,
|
||||
}))
|
||||
this.setData({ deviceList })
|
||||
/* TODO: 接入真实蓝牙断开逻辑 */
|
||||
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
|
||||
this.setData({ deviceList: list })
|
||||
lefuService.disconnect()
|
||||
},
|
||||
|
||||
/* 查看帮助 */
|
||||
/** 查看帮助 */
|
||||
onOpenHelp() {
|
||||
console.log('查看帮助')
|
||||
/* TODO: 跳帮助页 */
|
||||
wx.showToast({ title: '请确保设备已开机且在附近', icon: 'none' })
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user