diff --git a/miniprogram/pages/connectedDevice/connectedDevice.ts b/miniprogram/pages/connectedDevice/connectedDevice.ts index e39a135..1f442e3 100644 --- a/miniprogram/pages/connectedDevice/connectedDevice.ts +++ b/miniprogram/pages/connectedDevice/connectedDevice.ts @@ -1,5 +1,6 @@ import { lefuService } from '../../lefu/index' import type { ScannedDevice } from '../../lefu/index' +import type { RawDevice } from '../../lefu/types' /** 展示用设备项(仅存入 setData,不含复杂对象) */ interface DeviceItem { @@ -21,6 +22,8 @@ Page({ deviceList: [] as DeviceItem[], /** 防止权限弹框重入 */ _modalLock: false, + /** 自动连接缓存设备的一次性标记,防止扫描回调反复触发 */ + _autoConnectTried: false, }, onShow() { @@ -44,7 +47,7 @@ Page({ console.log(TAG, 'onStartSearch, _modalLock:', this.data._modalLock) if (this.data._modalLock) return _rawDevices = [] - this.setData({ status: 'searching', deviceList: [] }) + this.setData({ status: 'searching', deviceList: [], _autoConnectTried: false }) await this._checkPermissionAndScan() }, @@ -154,14 +157,51 @@ Page({ connected: d.name === connectedName, })) this.setData({ status: 'found', deviceList: list }) + + // 缓存设备命中:自动连接一次 + if (this.data._autoConnectTried) return + const cached = wx.getStorageSync('connectDeviceInfo') as RawDevice | null + const cachedDeviceId = cached?.deviceId + if (!cachedDeviceId) return + const matchedIndex = devices.findIndex(d => d.raw.deviceId === cachedDeviceId) + if (matchedIndex < 0) return + console.log(TAG, '命中缓存设备, deviceId:', cachedDeviceId, 'index:', matchedIndex) + this.setData({ _autoConnectTried: true }) + this._autoConnectByIndex(matchedIndex) }) lefuService.startScan() console.log(TAG, 'lefuService.startScan() 已调用') }, - /** 点击整张卡片:已连接 → 跳转配网页;未连接 → 走连接流程 */ + /** 缓存设备自动连接(与 onConnect 共享连接流程) */ + _autoConnectByIndex(index: number) { + const raw = _rawDevices[index] + if (!raw) return + wx.showLoading({ title: '连接中...', mask: true }) + lefuService.stopScan() + lefuService.connect(raw.raw) + lefuService.onConnectState((state) => { + if (state === lefuService.BLUE_STATE.CONNECTFAILED) { + wx.hideLoading() + wx.showToast({ title: '连接失败,请重试', icon: 'none' }) + } + }) + lefuService.onDeviceConnect(() => { + wx.setStorageSync('connectDeviceInfo', { ...raw.raw, scaleDeviceName: raw.raw.name }) + const list = this.data.deviceList.map((item, i) => ({ + ...item, + connected: i === index, + })) + this.setData({ deviceList: list }) + wx.hideLoading() + console.log(TAG, '缓存设备已自动连接(不跳转)') + }) + }, + + /** 点击整张卡片:已连接 → 跳转配网页;未连接 → 走手动连接流程 */ onTapCard(e: WechatMiniprogram.TouchEvent) { const connected = e.currentTarget.dataset.connected as boolean + console.log(TAG, 'onTapCard, connected:', connected) if (connected) { wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' }) return diff --git a/miniprogram/pages/connectedWifi/connectedWifi.ts b/miniprogram/pages/connectedWifi/connectedWifi.ts index b4f0d38..9045918 100644 --- a/miniprogram/pages/connectedWifi/connectedWifi.ts +++ b/miniprogram/pages/connectedWifi/connectedWifi.ts @@ -1,25 +1,5 @@ import { lefuService } from '../../lefu/index' -import type { LeFuWifiItem, DeviceMember } from '../../lefu/index' - -/** 本地 userInfo 关键字段,用于下发主用户到新设备 */ -interface StoredUserInfo { - userId: string - realname: string - sex: number - height: number - birthday: string -} - -/** 从生日字符串(YYYY-MM-DD)计算周岁 */ -const calcAge = (birthday: string): number => { - if (!birthday) return 0 - const [y, m, d] = birthday.split('-').map(Number) - if (!y || !m || !d) return 0 - const now = new Date() - let age = now.getFullYear() - y - if (now.getMonth() + 1 < m || (now.getMonth() + 1 === m && now.getDate() < d)) age -= 1 - return Math.max(0, age) -} +import type { LeFuWifiItem } from '../../lefu/index' interface WifiItem { ssid: string @@ -146,29 +126,6 @@ Page({ onConfirm() { if (!this.data.hasConnected) return - // 已有 userInfo 表示是从首页/设备页添加新设备,需把主用户下发到新设备后回首页 - const userInfo = wx.getStorageSync('userInfo') as StoredUserInfo | null - if (userInfo) { - const mainUser: DeviceMember = { - id: userInfo.userId, - name: userInfo.realname, - gender: userInfo.sex === 1 ? 1 : 0, - age: calcAge(userInfo.birthday), - height: userInfo.height, - isSelf: true, - } - wx.showLoading({ title: '正在下发主用户...', mask: true }) - lefuService.syncMembersToDevice([mainUser]) - .then(() => { - wx.hideLoading() - wx.switchTab({ url: '/pages/home/home' }) - }) - .catch((err: Error) => { - wx.hideLoading() - wx.showToast({ title: err.message || '主用户下发失败', icon: 'none' }) - }) - return - } wx.navigateTo({ url: '/pages/supplementPersonal/supplementPersonal' }) }, })