From 9dbd5b4c8746b12e6a27f732fac6771d675f2425 Mon Sep 17 00:00:00 2001 From: selfrelease Date: Wed, 29 Jul 2026 12:25:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BC=96=E8=BE=91=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BC=B9=E7=AA=97=E5=A2=9E=E5=8A=A0=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=BC=96=E8=BE=91=EF=BC=88=E5=A7=93=E5=90=8D?= =?UTF-8?q?=E3=80=81=E6=89=8B=E6=9C=BA=E5=8F=B7=E3=80=81=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=AF=86=E7=A0=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/routes/platform.routes.ts | 47 ++++++++++++++++++++ frontend/src/pages/platform/PlatformOrgs.tsx | 47 +++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/platform.routes.ts b/backend/src/routes/platform.routes.ts index 0f1bd3f..31ef391 100644 --- a/backend/src/routes/platform.routes.ts +++ b/backend/src/routes/platform.routes.ts @@ -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) + } +}) + /** * 删除企业(级联删除所有数据) */ diff --git a/frontend/src/pages/platform/PlatformOrgs.tsx b/frontend/src/pages/platform/PlatformOrgs.tsx index 4e47f7a..af8ece2 100644 --- a/frontend/src/pages/platform/PlatformOrgs.tsx +++ b/frontend/src/pages/platform/PlatformOrgs.tsx @@ -35,6 +35,8 @@ export default function PlatformOrgs() { adminName: '管理员', adminPhone: '', adminPassword: '12345678', }) const [creating, setCreating] = useState(false) + const [editAdmin, setEditAdmin] = useState({ name: '', phone: '', password: '' }) + const [savingAdmin, setSavingAdmin] = useState(false) const fetchOrgs = async () => { setLoading(true) @@ -55,6 +57,21 @@ export default function PlatformOrgs() { const totalPages = Math.ceil(total / pageSize) + const handleEditOrg = async (org: Org) => { + setEditOrg(org) + setEditAdmin({ name: '', phone: '', password: '' }) + // 加载企业管理员信息 + try { + const res = await api.get(`/platform/orgs/${org.id}`) as any + const admin = res.data.users?.find((u: any) => u.role === 'ADMIN') + if (admin) { + setEditAdmin({ name: admin.name || '', phone: admin.phone || '', password: '' }) + } + } catch { + // 忽略 + } + } + const handleSaveEdit = async () => { if (!editOrg) return try { @@ -66,6 +83,14 @@ export default function PlatformOrgs() { contactName: editOrg.contactName, contactPhone: editOrg.contactPhone, }) + // 如果管理员信息有改动,同步保存 + if (editAdmin.name || editAdmin.phone || editAdmin.password) { + await api.put(`/platform/orgs/${editOrg.id}/admin`, { + adminName: editAdmin.name || undefined, + adminPhone: editAdmin.phone || undefined, + adminPassword: editAdmin.password || undefined, + }) + } setEditOrg(null) fetchOrgs() } catch (err: any) { @@ -183,7 +208,7 @@ export default function PlatformOrgs() {
+ + {/* 管理员账号 */} +
+

企业管理员

+
+
+ + setEditAdmin({ ...editAdmin, name: e.target.value })} /> +
+
+ + setEditAdmin({ ...editAdmin, phone: e.target.value })} /> +
+
+ + setEditAdmin({ ...editAdmin, password: e.target.value })} /> +
+
+
+