feat(member): 删除成员先同步设备再调接口;新增 clearDeviceMembers 方法

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-05-27 13:59:21 +08:00
co-authored by Claude Sonnet 4.6
parent d4ab21ca6f
commit 1548ebbb74
2 changed files with 61 additions and 13 deletions
@@ -202,11 +202,11 @@ Page({
'deviceCard.capacityUsed': list.length,
})
// 首次加载不同步,后续数据变更触发的刷新才同步
if (this._firstLoad) {
this._firstLoad = false
} else {
// if (this._firstLoad) {
// this._firstLoad = false
// } else {
syncToDevice(list)
}
// }
})
.finally(() => {
this.setData({ loading: false })
@@ -225,6 +225,12 @@ Page({
const target = this.data.memberList.find(m => m.id === id)
if (!target) return
// 未连接设备不允许删除
if (!this.data.connected) {
wx.showToast({ title: '请先连接设备', icon: 'none' })
return
}
// 主用户有子用户时不允许删除
if (target.userType === 1) {
const hasSubUser = this.data.memberList.some(m => m.userType !== 1)
@@ -235,19 +241,49 @@ Page({
}
wx.showModal({
title: '提示',
content: '确定要删除该用户吗?',
title: '确认删除',
content: `确定要删除成员「${target.realname}」吗?删除后将同步更新设备数据。`,
success: (res) => {
if (!res.confirm) return
del('weighingScale/v2/del/user', { id }, { loadingTitle: '正在删除...' })
// 1. 从列表中移除该成员(立即更新 UI)
const newList = this.data.memberList.filter(m => m.id !== id)
this.setData({
memberList: newList,
'deviceCard.capacityUsed': newList.length,
})
wx.showLoading({ title: '正在同步设备数据...', mask: true })
// 2. 同步设备
let devicePromise: Promise<void>
if (newList.length) {
// 有剩余成员:清除设备数据并重新下发
devicePromise = lefuService.syncMembersToDevice(
newList.map(toDeviceMember),
(current, total) => {
wx.showLoading({ title: `正在同步设备数据 ${current}/${total}...`, mask: true })
},
)
} else {
// 删除最后一个成员:仅清除设备数据,不下发
devicePromise = lefuService.clearDeviceMembers()
}
devicePromise
.then(() => {
wx.showToast({ title: '删除成功', icon: 'success' })
setTimeout(() => {
this._loadData(this.data.sn)
}, 1500)
// 3. 调用删除接口
return del('weighingScale/v2/del/user', { id }, { loading: false })
})
.catch(() => {
wx.showToast({ title: '删除失败,请重试', icon: 'none' })
.then(() => {
wx.hideLoading()
wx.showToast({ title: '删除成功', icon: 'success' })
})
.catch((err: Error) => {
wx.hideLoading()
// 失败时重新拉取列表恢复数据
this._loadData(this.data.sn)
wx.showToast({ title: err.message || '删除失败,请重试', icon: 'none' })
})
},
})