From 48b75f37c964cf662f79fe8ed7dcb6a01f271382 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Tue, 25 Aug 2026 19:06:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(userManagement):=20=E6=88=90=E5=91=98?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=88=A0=E9=99=A4=E4=B8=8E=E8=BF=90=E7=BB=B4?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E6=8E=A5=E5=8F=A3=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- miniprogram/pages/addUser/addUser.ts | 71 +++++++++++++++++-- .../pages/userManagement/userManagement.ts | 45 +++++++++++- .../pages/userManagement/userManagement.wxml | 20 ++++-- 3 files changed, 120 insertions(+), 16 deletions(-) diff --git a/miniprogram/pages/addUser/addUser.ts b/miniprogram/pages/addUser/addUser.ts index e922d4e..e6de8ea 100644 --- a/miniprogram/pages/addUser/addUser.ts +++ b/miniprogram/pages/addUser/addUser.ts @@ -1,6 +1,6 @@ import { get, put } from '../../utils/request/index' import { getUserInfoByIdCard, type UserInfo } from '../../api/index' -import { parseIdCard, isIdCardValid } from '../../utils/idCard/index' +import { parseIdCard, isIdCardValid, calcAge } from '../../utils/idCard/index' /** 表单数据模型 */ interface MemberForm { @@ -22,8 +22,14 @@ const getEmployeeByWorkNo = (sn: string, workNo: string) => const addUser = (data: Record) => put('weighingScale/v6/edit/user', data) +/** 用户详情接口(编辑模式回填) */ +const getUserInfoById = (id: string) => + get>('weighingScale/v3/select/userinfo/byId', { id }) + /** 目标设备 SN(由 userManagement 跳转传入) */ let targetSn = '' +/** 编辑的用户 id(空表示新增) */ +let editUserId = '' Page({ data: { @@ -46,14 +52,62 @@ Page({ employeeNotFound: false, unitDisplay: '', employeeQueryResult: null as Record | null, + rawUser: null as Record | null, + isEdit: false, }, onLoad(options: Record) { targetSn = decodeURIComponent(options.sn || '') + editUserId = decodeURIComponent(options.userId || '') + this.setData({ isEdit: !!editUserId }) + if (editUserId) { + this._loadEditUser(editUserId) + } + }, + + /** 编辑模式:加载用户详情回填表单 */ + _loadEditUser(userId: string) { + getUserInfoById(userId) + .then((res) => { + const u = res.result + if (!u) { + wx.showToast({ title: '加载用户信息失败', icon: 'none' }) + return + } + const isFamily = u.dataType === 1 + const sex = u.sex ?? 0 + const gender = sex === 1 ? '男' : sex === 2 ? '女' : '' + const age = calcAge(u.birthday ?? '') + + this.setData({ + rawUser: u, + userType: isFamily ? 'family' : 'employee', + birthday: u.birthday ?? '', + 'form.workNo': u.workNo ?? '', + 'form.name': u.realname ?? '', + 'form.idCard': u.idCard ?? '', + 'form.gender': gender, + 'form.age': String(age), + 'form.sex': sex, + 'form.height': u.height ? String(u.height) : '', + 'form.weight': u.weight ? String(u.weight) : '', + }) + if (!isFamily) { + this.setData({ + employeeQueried: true, + employeeQueryResult: u, + unitDisplay: [u.secondDepart, u.thirdDepart].filter(Boolean).join(' / '), + }) + } + }) + .catch(() => { + wx.showToast({ title: '加载用户信息失败', icon: 'none' }) + }) }, /** tabs 切换:清空查询结果、重置表单 */ onTabTap(e: WechatMiniprogram.TouchEvent) { + if (this.data.isEdit) return const tab = e.currentTarget.dataset.value as string this.setData({ userType: tab as 'employee' | 'family', @@ -289,10 +343,12 @@ Page({ } const userInfo = wx.getStorageSync('userInfo') as Record | null - // 员工:以查询接口返回为 base;家庭:以身份证查询结果为 base - const baseSpread = userType === 'employee' - ? (this.data.employeeQueryResult ?? {}) - : (this.data.serverResult ?? {}) + // 编辑:以详情接口返回为 base;新增:员工用查询结果、家庭用身份证查询结果 + const baseSpread = editUserId + ? (this.data.rawUser ?? {}) + : (userType === 'employee' + ? (this.data.employeeQueryResult ?? {}) + : (this.data.serverResult ?? {})) const payload: Record = { ...baseSpread, @@ -308,10 +364,13 @@ Page({ weight: isNaN(weightNum) ? 0 : weightNum, mode: getApp().globalData.operationsEngineer ? 'ops' : 'normal', } + if (editUserId) { + payload.id = editUserId + } addUser(payload) .then(() => { - wx.showToast({ title: '添加成功', icon: 'success' }) + wx.showToast({ title: editUserId ? '保存成功' : '添加成功', icon: 'success' }) // 调用上一个页面(userManagement)刷新成员列表 const pages = getCurrentPages() const prevPage = pages[pages.length - 2] as any diff --git a/miniprogram/pages/userManagement/userManagement.ts b/miniprogram/pages/userManagement/userManagement.ts index 0eb2124..a75d5c8 100644 --- a/miniprogram/pages/userManagement/userManagement.ts +++ b/miniprogram/pages/userManagement/userManagement.ts @@ -1,4 +1,4 @@ -import { get, post } from '../../utils/request/index' +import { get, post, del } from '../../utils/request/index' import { leFuService } from '../../lefu/index' import type { SyncUserData } from '../../lefu/index' import { calcAge } from '../../utils/idCard/index' @@ -50,14 +50,23 @@ interface GroupItem { members: MemberItem[] } -/** 获取设备成员列表 */ +/** 获取设备成员列表(运维模式走 ops/getDeviceUsers,普通模式走 v3/selectUserBySn/user) */ const getDeviceMembers = (sn: string, userId: string) => - get('weighingScale/v3/selectUserBySn/user', { sn, userId }) + get( + getApp().globalData.operationsEngineer + ? 'weighingScale/ops/getDeviceUsers' + : 'weighingScale/v3/selectUserBySn/user', + { sn, userId }, + ) /** 标记用户已同步到设备 */ const markSynced = (data: { sn: string; list: string[] }) => post('weighingScale/v6/markSynced', data, { loading: false }) +/** 删除设备成员 */ +const deleteMember = (id: string) => + del('weighingScale/v2/del/user', { id }, { loading: false }) + /** equipment 页传入的目标设备 */ let targetDevice: { sn: string; deviceId: string; name: string; connected: boolean; connectDeviceInfo: string } | null = null @@ -370,6 +379,36 @@ Page({ wx.navigateTo({ url: `/pages/addUser/addUser?sn=${encodeURIComponent(sn)}` }) }, + /** 编辑成员:跳转 addUser 编辑模式 */ + onTapEdit(e: WechatMiniprogram.TouchEvent) { + const item = e.currentTarget.dataset.item as MemberItem + const sn = targetDevice?.sn || '' + wx.navigateTo({ url: `/pages/addUser/addUser?sn=${encodeURIComponent(sn)}&userId=${encodeURIComponent(item.userId)}` }) + }, + + /** 删除成员 */ + onTapDelete(e: WechatMiniprogram.TouchEvent) { + const item = e.currentTarget.dataset.item as MemberItem + wx.showModal({ + title: '删除用户', + content: `是否确认删除「${item.name}」?`, + cancelText: '取消', + confirmText: '删除', + confirmColor: '#F24439', + success: (res) => { + if (!res.confirm) return + deleteMember(item.id) + .then(() => { + wx.showToast({ title: '删除成功', icon: 'success' }) + this._loadMembers() + }) + .catch(() => { + wx.showToast({ title: '删除失败,请重试', icon: 'none' }) + }) + }, + }) + }, + /** 一键添加本人 */ onTapAddSelf() { // 一键添加本人(待接入) diff --git a/miniprogram/pages/userManagement/userManagement.wxml b/miniprogram/pages/userManagement/userManagement.wxml index cfec3c4..bce2c98 100644 --- a/miniprogram/pages/userManagement/userManagement.wxml +++ b/miniprogram/pages/userManagement/userManagement.wxml @@ -36,7 +36,8 @@ - + + @@ -55,9 +56,10 @@ - 删除 + 删除 + @@ -79,8 +81,8 @@ - 编辑 - 删除 + 编辑 + 删除 @@ -96,9 +98,13 @@ - 添加用户(最多10位) - - + + 添加用户(最多10位) + + + 一键添加本人 + 添加新用户 +