feat: 编辑企业弹窗增加管理员信息编辑(姓名、手机号、重置密码)
This commit is contained in:
@@ -259,6 +259,53 @@ router.put('/orgs/:id', async (req: AuthRequest, res, next) => {
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 编辑企业管理员(姓名、手机号、可选重置密码)
|
||||
*/
|
||||
router.put('/orgs/:id/admin', async (req: AuthRequest, res, next) => {
|
||||
try {
|
||||
const { adminName, adminPhone, adminPassword } = req.body as {
|
||||
adminName?: string; adminPhone?: string; adminPassword?: string
|
||||
}
|
||||
|
||||
// 找到该企业的 ADMIN 角色用户(第一个管理员)
|
||||
const admin = await prisma.user.findFirst({
|
||||
where: { orgId: req.params.id, role: 'ADMIN' },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
|
||||
if (!admin) {
|
||||
return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '该企业未设置管理员' } })
|
||||
}
|
||||
|
||||
// 如果修改了手机号,检查是否已被其他用户占用
|
||||
if (adminPhone && adminPhone !== admin.phone) {
|
||||
const existing = await prisma.user.findUnique({ where: { phone: adminPhone } })
|
||||
if (existing && existing.id !== admin.id) {
|
||||
return res.status(409).json({ success: false, error: { code: 'DUPLICATE_PHONE', message: '该手机号已被使用' } })
|
||||
}
|
||||
}
|
||||
|
||||
const updateData: any = {}
|
||||
if (adminName) updateData.name = adminName
|
||||
if (adminPhone) updateData.phone = adminPhone
|
||||
if (adminPassword && adminPassword.length >= 8) {
|
||||
const bcrypt = require('bcryptjs')
|
||||
updateData.passwordHash = await bcrypt.hash(adminPassword, 10)
|
||||
}
|
||||
|
||||
const updated = await prisma.user.update({
|
||||
where: { id: admin.id },
|
||||
data: updateData,
|
||||
select: { id: true, name: true, phone: true, role: true },
|
||||
})
|
||||
|
||||
res.json({ success: true, data: updated })
|
||||
} catch (err) {
|
||||
next(err)
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 删除企业(级联删除所有数据)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user