178 lines
5.6 KiB
TypeScript
178 lines
5.6 KiB
TypeScript
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
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
deviceList: [] as DeviceItem[],
|
|
},
|
|
|
|
onShow() {
|
|
this.loadData()
|
|
},
|
|
|
|
onUnload() {
|
|
lefuService.stopScan()
|
|
lefuService.onDeviceConnect(() => {})
|
|
lefuService.onDeviceInfo(() => {})
|
|
},
|
|
|
|
/** 拉取已绑定设备列表,加载完成后检查缓存自动连接 */
|
|
loadData() {
|
|
this.setData({ loading: true })
|
|
const userInfo = wx.getStorageSync('userInfo')
|
|
get<DeviceRecord[]>('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,
|
|
}
|
|
})
|
|
this.setData({ deviceList: list }, () => {
|
|
this._checkAndAutoConnect()
|
|
})
|
|
})
|
|
.finally(() => {
|
|
this.setData({ loading: false })
|
|
})
|
|
},
|
|
|
|
/** 检查缓存设备:已连接则直接标记,未连接则扫描自动连接 */
|
|
_checkAndAutoConnect() {
|
|
const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null
|
|
if (!cachedRaw) return
|
|
|
|
// 已连接且 deviceInfo 就绪,直接标记列表
|
|
if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) {
|
|
this._markConnected(lefuService.deviceInfo.serialNumber as string)
|
|
return
|
|
}
|
|
|
|
// 未连接,直接用缓存对象连,不走扫描
|
|
wx.showLoading({ title: '连接中...', mask: true })
|
|
wx.openBluetoothAdapter({
|
|
success: () => {
|
|
lefuService.connect(cachedRaw)
|
|
lefuService.onDeviceConnect(() => {
|
|
wx.hideLoading()
|
|
})
|
|
lefuService.onDeviceInfo((info) => {
|
|
this._markConnected(info.serialNumber as string)
|
|
})
|
|
},
|
|
fail: () => {
|
|
wx.hideLoading()
|
|
},
|
|
})
|
|
},
|
|
|
|
/** 将 serialNumber 对应的列表项标记为已连接,其余置为未连接 */
|
|
_markConnected(serialNumber: string) {
|
|
const list = this.data.deviceList.map(item => ({
|
|
...item,
|
|
connected: item.scaleDeviceId === serialNumber,
|
|
}))
|
|
this.setData({ deviceList: list })
|
|
},
|
|
|
|
/** 点击「连接」:断开当前、直接用 connectDeviceInfo 连接指定设备 */
|
|
onTapConnect(e: WechatMiniprogram.TouchEvent) {
|
|
const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string
|
|
let rawDevice: RawDevice | null = null
|
|
try {
|
|
rawDevice = JSON.parse(connectDeviceInfoStr || '{}')
|
|
} catch {}
|
|
if (!rawDevice) return
|
|
|
|
if (lefuService.isConnected) {
|
|
lefuService.disconnect()
|
|
}
|
|
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
|
|
this.setData({ deviceList: list })
|
|
|
|
wx.showLoading({ title: '连接中...', mask: true })
|
|
wx.openBluetoothAdapter({
|
|
success: () => {
|
|
lefuService.connect(rawDevice!)
|
|
lefuService.onDeviceConnect(() => {
|
|
wx.hideLoading()
|
|
wx.setStorageSync('connectDeviceInfo', rawDevice)
|
|
})
|
|
lefuService.onDeviceInfo((info) => {
|
|
this._markConnected(info.serialNumber as string)
|
|
})
|
|
},
|
|
fail: () => {
|
|
wx.hideLoading()
|
|
wx.showToast({ title: '蓝牙未开启', icon: 'none' })
|
|
},
|
|
})
|
|
},
|
|
|
|
/* 点击「成员管理」→ 跳转成员管理页,sn 由目标页从缓存读取 */
|
|
onTapMember() {
|
|
wx.navigateTo({ url: '/pages/equipmentMember/equipmentMember' })
|
|
},
|
|
|
|
/* 点击「添加设备」→ 跳转设备搜索页 */
|
|
onTapAdd() {
|
|
wx.navigateTo({ url: '/pages/connectedDevice/connectedDevice' })
|
|
},
|
|
})
|