import { get } from '../../utils/request/index' import { lefuService } from '../../lefu/index' import type { RawDevice } from '../../lefu/types' /** API 返回的设备记录 */ interface DeviceRecord { id: string userId: string parentUserId: string | null scaleDeviceId: string deviceId: string | null deviceType: string dataType: number userType: number idCard: string height: number weight: number birthday: string sex: number avatar: string | null phone: string | null thirdId: string realname: string bmi: number createBy: string createTime: string updateBy: string updateTime: string delFlag: number sn: string | null workNo: string remark: string | null relationType: string | null sex_dictText: string /** 设备连接信息 JSON 字符串,与登录接口返回格式一致 */ connectDeviceInfo: string } /** 展示用设备项 */ interface DeviceItem { id: string scaleDeviceId: string scaleDeviceName: string connectDeviceInfo: string realname: string /** 蓝牙连接状态,接口不返回,由业务层更新 */ connected: boolean userId: string sex: number height: number birthday: string avatar: string | null relationType: string | null sex_dictText: string } Page({ data: { loading: false, deviceList: [] as DeviceItem[], }, onShow() { this.loadData() }, onUnload() { lefuService.stopScan() lefuService.onDeviceConnect(() => {}) lefuService.onDeviceInfo(() => {}) lefuService.onDisconnected(() => {}) }, /** 拉取已绑定设备列表,加载完成后检查缓存自动连接 */ loadData() { this.setData({ loading: true }) const userInfo = wx.getStorageSync('userInfo') get('weighingScale/v3/list/device', { userId: userInfo?.userId }) .then(res => { const list: DeviceItem[] = res.result.map(item => { let scaleDeviceName = '' try { const info = JSON.parse(item.connectDeviceInfo || '{}') scaleDeviceName = info.name ?? '' } catch {} return { id: item.id, scaleDeviceId: item.scaleDeviceId, scaleDeviceName, connectDeviceInfo: item.connectDeviceInfo ?? '', realname: item.realname, connected: false, userId: item.userId, sex: item.sex, height: item.height, birthday: item.birthday, avatar: item.avatar, relationType: item.relationType, sex_dictText: item.sex_dictText, } }) this.setData({ deviceList: list }, () => { this._checkAndAutoConnect() }) }) .finally(() => { this.setData({ loading: false }) }) }, /** 每次进入页面都走真实扫描连接,确保状态最新 */ _checkAndAutoConnect() { lefuService.onDeviceInfo((info) => { this._markConnected(info.serialNumber as string) }) lefuService.onDisconnected(() => { this._markAllDisconnected() }) lefuService.autoConnect() }, /** 将 serialNumber 对应的列表项标记为已连接,其余置为未连接 */ _markConnected(serialNumber: string) { const list = this.data.deviceList.map(item => ({ ...item, connected: item.scaleDeviceId === serialNumber, })) this.setData({ deviceList: list }) }, /** 将所有设备标记为未连接 */ _markAllDisconnected() { const list = this.data.deviceList.map(item => ({ ...item, connected: false })) this.setData({ deviceList: list }) }, /** 点击「连接」:扫描周边设备,找到 deviceId 匹配的设备后连接 */ onTapConnect(e: WechatMiniprogram.TouchEvent) { const userInfo = e.currentTarget.dataset.userInfo as DeviceItem const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string let cachedDevice: RawDevice | null = null try { cachedDevice = JSON.parse(connectDeviceInfoStr || '{}') } catch {} const targetDeviceId = cachedDevice?.deviceId as string | undefined if (!targetDeviceId) return if (lefuService.isConnected) lefuService.disconnect() this.setData({ deviceList: this.data.deviceList.map(item => ({ ...item, connected: false })) }) let scanTimer: number | null = null wx.showLoading({ title: '扫描中...', mask: true }) wx.openBluetoothAdapter({ success: () => { scanTimer = setTimeout(() => { lefuService.stopScan() wx.hideLoading() wx.showToast({ title: '未找到设备', icon: 'none' }) }, 15000) lefuService.onDevicesList((devices) => { const matched = devices.find(d => d.raw.deviceId === targetDeviceId) if (!matched) return if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } lefuService.stopScan() wx.showLoading({ title: '连接中...', mask: true }) lefuService.connect(matched.raw) lefuService.onDeviceConnect(() => { wx.hideLoading() wx.setStorageSync('connectDeviceInfo', matched.raw) // 连接成功后缓存当前设备关联的用户信息 if (userInfo) { wx.setStorageSync('userInfo', userInfo) } }) lefuService.onDeviceInfo((info) => { this._markConnected(info.serialNumber as string) }) }) lefuService.startScan() }, fail: () => { wx.hideLoading() wx.showToast({ title: '蓝牙未开启', icon: 'none' }) }, }) }, /* 点击「用户管理」→ 跳转用户管理页,sn 由目标页从缓存读取 */ onTapMember() { wx.navigateTo({ url: '/pages/equipmentMember/equipmentMember' }) }, /* 点击「添加设备」→ 跳转设备搜索页 */ onTapAdd() { wx.navigateTo({ url: '/pages/connectedDevice/connectedDevice' }) }, })