diff --git a/miniprogram/pages/userManagement/userManagement.ts b/miniprogram/pages/userManagement/userManagement.ts index 97858b5..89bcbcf 100644 --- a/miniprogram/pages/userManagement/userManagement.ts +++ b/miniprogram/pages/userManagement/userManagement.ts @@ -19,6 +19,7 @@ interface SysUserDevice { dataType: number relationType: number | null syncStatus?: number + delFlag?: number } /** 同步用的用户项 */ @@ -30,6 +31,7 @@ interface SyncUser { age: number height: number sync: string + delFlag?: number } /** 展示用成员项 */ @@ -44,6 +46,7 @@ interface MemberItem { syncClass: string /** 1=本人 2=自己添加 3=他人添加 */ relationType: number | null + delFlag?: number } /** 展示用分组 */ @@ -147,6 +150,7 @@ Page({ sync: item.syncStatus === 1 ? '已同步' : '待同步', syncClass: item.syncStatus === 1 ? 'done' : 'pending', relationType: item.relationType, + delFlag: item.delFlag, }) const self = list.find(m => m.relationType === 1) this.setData({ @@ -234,35 +238,26 @@ Page({ ...(self ? [{ id: self.id, userId: self.userId, name: self.name, gender: self.gender === '男' ? 1 : 0, - age: self.age, height: self.height, sync: self.sync, + age: self.age, height: self.height, sync: self.sync, delFlag: self.delFlag, }] : []), ...this.data.groups.flatMap(g => g.members.map(m => ({ id: String(m.id), userId: m.userId, name: m.name, - gender: m.gender === '男' ? 1 : 0, age: m.age, height: m.height, sync: m.sync, + gender: m.gender === '男' ? 1 : 0, age: m.age, height: m.height, sync: m.sync, delFlag: m.delFlag, }))), ] }, - /** 同步完成后的统一处理:标记同步 + 更新列表状态 + 关闭弹框 */ - _handleSyncSuccess(users: SyncUser[], successText: string) { + /** 同步完成后的统一处理:上报后刷新列表 + 关闭弹框 */ + _finishSync(reportUsers: SyncUser[] | null, successText: string) { this.setData({ syncPercent: 100, syncStatus: '同步完成' }) wx.showToast({ title: successText, icon: 'success' }) - const syncedIds = users.map(u => u.userId).filter(Boolean) - if (targetDevice?.sn && syncedIds.length) { - markSynced({ sn: targetDevice.sn, list: syncedIds }) - .then(() => { - const syncedSet = new Set(syncedIds) - this.setData({ - self: this.data.self && syncedSet.has(this.data.self.userId) - ? { ...this.data.self, sync: '已同步', syncClass: 'done' } - : this.data.self, - groups: this.data.groups.map(g => ({ - ...g, - members: g.members.map(m => syncedSet.has(m.userId) ? { ...m, sync: '已同步', syncClass: 'done' } : m), - })), - }) - }) - .catch(() => {}) + if (reportUsers && reportUsers.length && targetDevice?.sn) { + const syncedIds = reportUsers.map(u => u.userId).filter(Boolean) + if (syncedIds.length) { + markSynced({ sn: targetDevice.sn, list: syncedIds }) + .then(() => this._loadMembers()) + .catch(() => {}) + } } if (syncCloseTimer !== null) { clearTimeout(syncCloseTimer); syncCloseTimer = null } syncCloseTimer = setTimeout(() => { @@ -271,7 +266,7 @@ Page({ }, 1000) }, - /** 同步用户至设备:只下发「待同步」的用户 */ + /** 同步用户至设备:有删除标记则清空重下发,否则只下发「待同步」 */ onTapSync() { if (!this._ensureConnected()) return @@ -280,13 +275,19 @@ Page({ wx.showToast({ title: '暂无用户可同步', icon: 'none' }) return } - // 只下发「待同步」的用户 + // 有删除标记(delFlag=1):清空设备后下发未删除的用户,上报所有用户 + const hasDeleted = allUsers.some(u => u.delFlag === 1) + if (hasDeleted) { + const normalUsers = allUsers.filter(u => u.delFlag !== 1) + this._clearAndSync(normalUsers, allUsers) + return + } + // 无删除标记:只下发「待同步」的用户 const pendingUsers = allUsers.filter(u => u.sync !== '已同步') if (!pendingUsers.length) { wx.showToast({ title: '没有需要同步的用户', icon: 'none' }) return } - // 先读设备端主用户列表,判断设备是否已有主用户 leFuService.fetchDeviceUserIds() .then((deviceUserIds) => this._syncUsers(pendingUsers, deviceUserIds)) @@ -325,14 +326,14 @@ Page({ syncStatus: `正在同步 ${current}/${total}...`, }) }) - .then(() => this._handleSyncSuccess(pendingUsers, '用户同步成功')) + .then(() => this._finishSync(pendingUsers, '用户同步成功')) .catch((err: Error) => { this.setData({ syncShow: false }) wx.showToast({ title: err.message || '同步失败,请重试', icon: 'none' }) }) }, - /** 长按标题:清除设备用户信息并全部重新下发 */ + /** 长按标题:清除设备用户信息并全部重新下发(不上报) */ onLongPressSectionTitle() { if (!this._ensureConnected()) return const allUsers = this._buildAllUsers() @@ -340,13 +341,13 @@ Page({ wx.showToast({ title: '暂无用户可下发', icon: 'none' }) return } - this._resendAllUsers(allUsers) + this._clearAndSync(allUsers, null) }, - /** 清除设备用户后全部重新下发 */ - _resendAllUsers(allUsers: SyncUser[]) { - const mainUser = allUsers[0] - const syncDataList: SyncUserData[] = allUsers.map((user, index) => ({ + /** 清除设备用户后重新下发;reportUsers 为 null 时不上报后台 */ + _clearAndSync(syncUsers: SyncUser[], reportUsers: SyncUser[] | null) { + const mainUser = syncUsers[0] + const syncDataList: SyncUserData[] = syncUsers.map((user, index) => ({ userID: mainUser.id, userName: user.name, memberID: user.id === mainUser.id ? '' : user.id, @@ -369,7 +370,7 @@ Page({ syncStatus: `正在同步 ${current}/${total}...`, }) })) - .then(() => this._handleSyncSuccess(allUsers, '重新下发成功')) + .then(() => this._finishSync(reportUsers, '重新下发成功')) .catch((err: Error) => { this.setData({ syncShow: false }) wx.showToast({ title: err.message || '重新下发失败', icon: 'none' })