fix: 修复蓝牙连接误报与 WiFi 列表获取问题,完善一键添加本人
- connectedDevice 忽略 connect 过程中的 UNAVAILABLE 抖动,避免误报蓝牙关闭 - getWifiList 加 settled 保护,onStartSearch 加防重入 - userManagement 一键添加本人(仅加后台、不下发设备) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
b4d3a034ba
commit
dbb2ab8423
@@ -324,25 +324,28 @@ class LeFuService {
|
|||||||
reject(new Error('设备未连接,无法获取 WiFi 列表'))
|
reject(new Error('设备未连接,无法获取 WiFi 列表'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
let settled = false
|
||||||
let timer: number | null = null
|
let timer: number | null = null
|
||||||
let delayTimer: number | null = null
|
let delayTimer: number | null = null
|
||||||
// 15s 整体超时
|
const settle = (err: Error | null, data: LeFuWifiItem[] = []) => {
|
||||||
timer = setTimeout(() => {
|
if (settled) return
|
||||||
|
settled = true
|
||||||
|
if (timer !== null) { clearTimeout(timer); timer = null }
|
||||||
if (delayTimer !== null) { clearTimeout(delayTimer); delayTimer = null }
|
if (delayTimer !== null) { clearTimeout(delayTimer); delayTimer = null }
|
||||||
reject(new Error('获取 WiFi 列表超时'))
|
err ? reject(err) : resolve(data)
|
||||||
}, 15000)
|
}
|
||||||
|
// 15s 整体超时
|
||||||
|
timer = setTimeout(() => settle(new Error('获取 WiFi 列表超时')), 15000)
|
||||||
// 延时 2s 后下发查询指令(等待设备进入配网模式)
|
// 延时 2s 后下发查询指令(等待设备进入配网模式)
|
||||||
delayTimer = setTimeout(() => {
|
delayTimer = setTimeout(() => {
|
||||||
this._activeProtocol.dataFindSurroundDevice((res: any[]) => {
|
this._activeProtocol.dataFindSurroundDevice((res: any[]) => {
|
||||||
if (timer !== null) { clearTimeout(timer); timer = null }
|
|
||||||
if (delayTimer !== null) { clearTimeout(delayTimer); delayTimer = null }
|
|
||||||
console.log(TAG, 'getWifiList 结果:', res)
|
console.log(TAG, 'getWifiList 结果:', res)
|
||||||
if (!res?.length) {
|
if (!res?.length) {
|
||||||
this._activeProtocol.dataExitWifiConfig(() => { })
|
this._activeProtocol.dataExitWifiConfig(() => { })
|
||||||
resolve([])
|
settle(null, [])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve(res.map((item: any) => ({ ssid: item.ssid || item.name || '', signal: item.signal })))
|
settle(null, res.map((item: any) => ({ ssid: item.ssid || item.name || '', signal: item.signal })))
|
||||||
})
|
})
|
||||||
}, 2000)
|
}, 2000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ Page({
|
|||||||
|
|
||||||
onLoad(options: Record<string, string>) {
|
onLoad(options: Record<string, string>) {
|
||||||
targetSn = decodeURIComponent(options.sn || '')
|
targetSn = decodeURIComponent(options.sn || '')
|
||||||
|
console.log(targetSn);
|
||||||
editId = decodeURIComponent(options.id || '')
|
editId = decodeURIComponent(options.id || '')
|
||||||
this.setData({ isEdit: !!editId })
|
this.setData({ isEdit: !!editId })
|
||||||
if (editId) {
|
if (editId) {
|
||||||
|
|||||||
@@ -325,11 +325,10 @@ Page({
|
|||||||
if (state === leFuService.BLUE_STATE.CONNECTFAILED) {
|
if (state === leFuService.BLUE_STATE.CONNECTFAILED) {
|
||||||
finish()
|
finish()
|
||||||
wx.showToast({ title: '连接失败,请重试', icon: 'none' })
|
wx.showToast({ title: '连接失败,请重试', icon: 'none' })
|
||||||
} else if (state === leFuService.BLUE_STATE.UNAVAILABLE) {
|
|
||||||
// 连接过程中蓝牙被关闭:立即结束 loading,避免卡到超时
|
|
||||||
finish()
|
|
||||||
wx.showToast({ title: '蓝牙已断开,请重新开启', icon: 'none' })
|
|
||||||
}
|
}
|
||||||
|
// UNAVAILABLE 不在此处理:connect 过程中 clearProtocol 会触发一次 UNAVAILABLE 抖动,
|
||||||
|
// 后面紧跟 CONNECTSUCCESS,误判会打断连接流程并误报「蓝牙关闭」。
|
||||||
|
// 真正「连接后关闭蓝牙」由 onDisconnected 回调处理。
|
||||||
})
|
})
|
||||||
|
|
||||||
connectCleanup.unsubConnect = leFuService.onDeviceConnect(() => {
|
connectCleanup.unsubConnect = leFuService.onDeviceConnect(() => {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { leFuService } from '../../lefu/index'
|
|||||||
let unsubDisconnected: (() => void) | null = null
|
let unsubDisconnected: (() => void) | null = null
|
||||||
let unsubDeviceConnect: (() => void) | null = null
|
let unsubDeviceConnect: (() => void) | null = null
|
||||||
let retryTimer: number | null = null
|
let retryTimer: number | null = null
|
||||||
|
/** 获取 WiFi 列表防重入标记 */
|
||||||
|
let searching = false
|
||||||
|
|
||||||
/** 渲染用的 WiFi 项(在 SSID 基础上追加连接状态) */
|
/** 渲染用的 WiFi 项(在 SSID 基础上追加连接状态) */
|
||||||
interface WifiItem {
|
interface WifiItem {
|
||||||
@@ -74,10 +76,12 @@ Page({
|
|||||||
|
|
||||||
/** 通过设备协议获取周围 WiFi 列表 */
|
/** 通过设备协议获取周围 WiFi 列表 */
|
||||||
async onStartSearch() {
|
async onStartSearch() {
|
||||||
|
if (searching) return
|
||||||
if (!this._ensureConnected()) {
|
if (!this._ensureConnected()) {
|
||||||
this.setData({ status: 'idle', wifiList: [] })
|
this.setData({ status: 'idle', wifiList: [] })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
searching = true
|
||||||
this.setData({ status: 'searching', wifiList: [] })
|
this.setData({ status: 'searching', wifiList: [] })
|
||||||
wx.showLoading({ title: '正在获取 WiFi 列表...', mask: true })
|
wx.showLoading({ title: '正在获取 WiFi 列表...', mask: true })
|
||||||
try {
|
try {
|
||||||
@@ -95,6 +99,8 @@ Page({
|
|||||||
wx.hideLoading()
|
wx.hideLoading()
|
||||||
this.setData({ status: 'empty', wifiList: [] })
|
this.setData({ status: 'empty', wifiList: [] })
|
||||||
wx.showToast({ title: '获取 WiFi 列表失败', icon: 'none' })
|
wx.showToast({ title: '获取 WiFi 列表失败', icon: 'none' })
|
||||||
|
} finally {
|
||||||
|
searching = false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -167,6 +173,6 @@ Page({
|
|||||||
onConfirm() {
|
onConfirm() {
|
||||||
if (!this.data.hasConnected) return
|
if (!this.data.hasConnected) return
|
||||||
if (!this._ensureConnected()) return
|
if (!this._ensureConnected()) return
|
||||||
wx.navigateTo({ url: '/pages/equipment/equipment' })
|
wx.reLaunch({ url: '/pages/equipment/equipment' })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { get, post, del } from '../../utils/request/index'
|
import { get, post, del, put } from '../../utils/request/index'
|
||||||
import { leFuService } from '../../lefu/index'
|
import { leFuService } from '../../lefu/index'
|
||||||
import type { SyncUserData } from '../../lefu/index'
|
import type { SyncUserData } from '../../lefu/index'
|
||||||
import { calcAge } from '../../utils/idCard/index'
|
import { calcAge } from '../../utils/idCard/index'
|
||||||
@@ -72,6 +72,10 @@ const markSynced = (data: { sn: string; list: string[] }) =>
|
|||||||
const deleteMember = (id: string) =>
|
const deleteMember = (id: string) =>
|
||||||
del('weighingScale/v6/del/user', { id }, { loading: false })
|
del('weighingScale/v6/del/user', { id }, { loading: false })
|
||||||
|
|
||||||
|
/** 添加设备成员(本人) */
|
||||||
|
const addSelfUser = (data: Record<string, any>) =>
|
||||||
|
put('weighingScale/v6/edit/user', data)
|
||||||
|
|
||||||
/** equipment 页传入的目标设备 */
|
/** equipment 页传入的目标设备 */
|
||||||
let targetDevice: { sn: string; deviceId: string; name: string; connected: boolean; connectDeviceInfo: string } | null = null
|
let targetDevice: { sn: string; deviceId: string; name: string; connected: boolean; connectDeviceInfo: string } | null = null
|
||||||
|
|
||||||
@@ -413,9 +417,36 @@ Page({
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/** 一键添加本人 */
|
/** 一键添加本人:仅调接口把当前登录用户添加为设备成员,不下发设备 */
|
||||||
onTapAddSelf() {
|
onTapAddSelf() {
|
||||||
// 一键添加本人(待接入)
|
const userInfo = wx.getStorageSync('userInfo') as Record<string, any> | null
|
||||||
|
if (!userInfo?.userId) {
|
||||||
|
wx.showToast({ title: '请先完善个人信息', icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const sn = targetDevice?.sn
|
||||||
|
if (!sn) return
|
||||||
|
|
||||||
|
addSelfUser({
|
||||||
|
dataType: 0,
|
||||||
|
parentUserId: userInfo.userId,
|
||||||
|
sn,
|
||||||
|
scaleDeviceId: sn,
|
||||||
|
realname: userInfo.realname ?? '',
|
||||||
|
idCard: userInfo.idCard || null,
|
||||||
|
sex: userInfo.sex ?? 0,
|
||||||
|
birthday: userInfo.birthday ?? '',
|
||||||
|
height: userInfo.height ?? 0,
|
||||||
|
weight: userInfo.weight ?? 0,
|
||||||
|
mode: getApp<IAppOption>().globalData.operationsEngineer ? 'ops' : 'normal',
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
wx.showToast({ title: '添加成功', icon: 'success' })
|
||||||
|
this._loadMembers()
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
wx.showToast({ title: '添加失败,请重试', icon: 'none' })
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/** 添加新用户 */
|
/** 添加新用户 */
|
||||||
|
|||||||
Reference in New Issue
Block a user