feat(userManagement): 成员编辑删除与运维模式接口区分
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
2afe598881
commit
48b75f37c9
@@ -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<string, any>) =>
|
||||
put('weighingScale/v6/edit/user', data)
|
||||
|
||||
/** 用户详情接口(编辑模式回填) */
|
||||
const getUserInfoById = (id: string) =>
|
||||
get<Record<string, any>>('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<string, any> | null,
|
||||
rawUser: null as Record<string, any> | null,
|
||||
isEdit: false,
|
||||
},
|
||||
|
||||
onLoad(options: Record<string, string>) {
|
||||
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<string, any> | 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<string, any> = {
|
||||
...baseSpread,
|
||||
@@ -308,10 +364,13 @@ Page({
|
||||
weight: isNaN(weightNum) ? 0 : weightNum,
|
||||
mode: getApp<IAppOption>().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
|
||||
|
||||
Reference in New Issue
Block a user