feat(principal): 主体启用/停用改为仅数据库维护,页面与接口不可操作

- PUT /api/principals/{id} 不再接受 status 变更(变更时返回 403「主体的启用/停用只能由平台运维在数据库中维护」)
- POST /api/principals 状态固定为启用,创建时传 status=0 直接 403;其它非白名单字段忽略
- 集团设置页面状态改为只读展示并提示由平台运维维护(去掉启用/禁用开关)
- 前端 updatePrincipal 类型移除 status,接口契约说明同步更新

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-06 00:21:52 +08:00
parent 463b7cd1bc
commit 137a3ed1f3
4 changed files with 39 additions and 11 deletions

View File

@ -277,7 +277,7 @@ module.exports = {
type: { type: 'integer', description: '主体类型:1自营 2加盟 3外部,默认1' },
contact_name: { type: 'string', description: '联系人' },
contact_phone: { type: 'string', description: '联系电话' },
status: { type: 'integer', description: '状态:1启用 0禁用,默认1' },
status: { type: 'integer', description: '主体状态固定为启用:启用/停用只能由平台运维在数据库中维护' },
remark: { type: 'string', description: '备注' },
},
updatePrincipalRequest: {
@ -285,7 +285,7 @@ module.exports = {
type: { type: 'integer', description: '主体类型:1自营 2加盟 3外部' },
contact_name: { type: 'string', description: '联系人' },
contact_phone: { type: 'string', description: '联系电话' },
status: { type: 'integer', description: '状态:1启用 0禁用' },
status: { type: 'integer', description: '主体状态不接受接口修改:启用/停用只能由平台运维在数据库中维护' },
remark: { type: 'string', description: '备注' },
},

View File

@ -3,8 +3,11 @@
const { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/** 允许更新的主体字段,避免请求体覆盖 id 等主键信息 */
const WRITABLE_FIELDS = [ 'name', 'type', 'contact_name', 'contact_phone', 'status', 'remark' ];
/** 允许更新的主体字段,避免请求体覆盖 id 等主键信息
* 注意:不含 status —— 主体的启用/停用只能由平台运维直接在数据库中维护,
* 页面与接口都不允许普通用户自行启用或停用集团(否则会把自己锁死或被绕过)。
*/
const WRITABLE_FIELDS = [ 'name', 'type', 'contact_name', 'contact_phone', 'remark' ];
/**
* @controller PrincipalController 主体管理
@ -94,7 +97,8 @@ class PrincipalController extends Controller {
/**
* @summary 新增主体
* @description 主体名称 name 必填;创建者自动关联为该主体 owner(多租户隔离)
* @description 主体名称 name 必填;创建者自动关联为该主体 owner(多租户隔离);
* 主体状态固定为启用(启用/停用只能由平台运维在数据库中维护)
* @router post /api/principals
* @apikey
* @request body createPrincipalRequest *body
@ -109,8 +113,18 @@ class PrincipalController extends Controller {
ctx.body = { code: 400, message: 'name 为必填项' };
return;
}
// 状态不接受入参:主体启用/停用只能由平台运维在数据库中维护
if (ctx.request.body.status !== undefined && Number(ctx.request.body.status) !== 1) {
ctx.status = 403;
ctx.body = { code: 403, message: '主体的启用/停用只能由平台运维在数据库中维护' };
return;
}
const principal = await ctx.model.Principal.create(ctx.request.body);
const payload = { status: 1 };
WRITABLE_FIELDS.forEach(field => {
if (ctx.request.body[field] !== undefined) payload[field] = ctx.request.body[field];
});
const principal = await ctx.model.Principal.create(payload);
// 将创建者关联为该主体的默认用户(owner),建立多租户归属关系
if (ctx.userId) {
@ -131,7 +145,7 @@ class PrincipalController extends Controller {
/**
* @summary 更新主体
* @description 未传入的字段保持原值
* @description 未传入的字段保持原值;不接受状态变更(主体启用/停用只能由平台运维在数据库中维护)
* @router put /api/principals/{id}
* @apikey
* @request path integer *id 主体主键ID
@ -155,6 +169,13 @@ class PrincipalController extends Controller {
return;
}
// 状态不通过接口维护:避免普通用户自行启用/停用集团(停用后自身也会被锁在外面)
if (ctx.request.body.status !== undefined && Number(ctx.request.body.status) !== Number(principal.status)) {
ctx.status = 403;
ctx.body = { code: 403, message: '主体的启用/停用只能由平台运维在数据库中维护' };
return;
}
const data = {};
WRITABLE_FIELDS.forEach(field => {
if (ctx.request.body[field] !== undefined) data[field] = ctx.request.body[field];

View File

@ -29,8 +29,8 @@ export function getPrincipal(id: number) {
return get<PrincipalItem & { remark?: string | null; updated_at?: string }>(`/principals/${id}`)
}
/** 更新主体(组织架构 → 集团设置保存当前主体) */
export function updatePrincipal(id: number, data: Partial<PrincipalPayload> & { status?: number }) {
/** 更新主体(集团设置保存当前主体);主体状态不接受接口修改,只能由平台运维在数据库中维护 */
export function updatePrincipal(id: number, data: Partial<PrincipalPayload>) {
return put<{ id: number }>(`/principals/${id}`, data)
}

View File

@ -77,7 +77,6 @@ async function handleSave() {
type: form.type,
contact_name: form.contact_name.trim(),
contact_phone: form.contact_phone.trim(),
status: form.status,
remark: form.remark.trim(),
})
ElMessage.success('集团设置已保存')
@ -126,7 +125,10 @@ onMounted(fetchGroup)
<el-input v-model="form.contact_phone" maxlength="20" placeholder="选填" />
</el-form-item>
<el-form-item label="状态">
<el-switch v-model="form.status" :active-value="1" :inactive-value="0" active-text="启用" inactive-text="禁用" />
<el-tag :type="form.status === 1 ? 'success' : 'info'">
{{ form.status === 1 ? '启用' : '已停用' }}
</el-tag>
<span class="status-hint">主体启用/停用由平台运维在后台维护,页面不可自行切换</span>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="form.remark" type="textarea" :rows="3" maxlength="255" show-word-limit placeholder="选填" />
@ -161,4 +163,9 @@ onMounted(fetchGroup)
max-width: 620px;
margin-top: 8px;
}
.status-hint {
margin-left: 10px;
font-size: 12px;
color: var(--el-text-color-secondary);
}
</style>