feat: 主体停用后下拉置灰不可切换、数据只读、定时任务跳过
- principalCheck 中间件加载主体状态:停用主体只放行读请求(GET/HEAD/OPTIONS)与导出任务提交, 其余写操作(新建/编辑/推送/删除)一律 403「主体已停用,仅可查看与导出」 - 可选主体列表包含停用主体并按「启用优先」排序;/api/auth/profile 返回 status/disabled - 顶栏主体下拉显示「xxx(已停用)」且不可选择;当前主体停用时在顶栏下方显示只读提示条 - 定时任务跳过停用主体:自动拉单、订单推送 WMS、发货推送、出库推送、发货结果回传平台、一键拉单任务 - 主体自身的 GET/PUT 未加主体状态校验,运维仍可通过接口重新启用 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
739506da58
commit
e49c25cd19
@ -215,6 +215,9 @@ class AuthController extends Controller {
|
|||||||
return {
|
return {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
name: item.name,
|
name: item.name,
|
||||||
|
// 主体停用时前端置灰不可选,并提示只能查看
|
||||||
|
status: item.status,
|
||||||
|
disabled: Number(item.status) !== 1,
|
||||||
account_name: (relation && relation.name) || null,
|
account_name: (relation && relation.name) || null,
|
||||||
account_avatar: (relation && relation.avatar) || null,
|
account_avatar: (relation && relation.avatar) || null,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
* 适用于“需要传入/关联主体ID(principal_id)”的请求:
|
* 适用于“需要传入/关联主体ID(principal_id)”的请求:
|
||||||
* - 取不到可校验的主体ID -> 400
|
* - 取不到可校验的主体ID -> 400
|
||||||
* - 当前账号未与该主体绑定(principal_users 无记录)-> 403
|
* - 当前账号未与该主体绑定(principal_users 无记录)-> 403
|
||||||
|
* - 主体已停用(principals.status=0)-> 只允许读(GET/HEAD/OPTIONS)与导出,其余写操作 403
|
||||||
* 校验通过后把 principalId 写入 ctx.state.principalId,供控制器做数据隔离(列表过滤、创建归属)。
|
* 校验通过后把 principalId 写入 ctx.state.principalId,供控制器做数据隔离(列表过滤、创建归属)。
|
||||||
*
|
*
|
||||||
* 解析与鉴权规则:
|
* 解析与鉴权规则:
|
||||||
@ -66,6 +67,38 @@ module.exports = (options = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.state.principalId = principalId;
|
ctx.state.principalId = principalId;
|
||||||
|
|
||||||
|
// 主体停用:数据只读(可查看 / 导出),写操作一律拒绝
|
||||||
|
const principal = await ctx.service.cache.getPrincipal(principalId);
|
||||||
|
if (!principal) {
|
||||||
|
ctx.status = 404;
|
||||||
|
ctx.body = { code: 404, message: '主体不存在' };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const disabled = Number(principal.status) !== 1;
|
||||||
|
ctx.state.principalDisabled = disabled;
|
||||||
|
if (disabled && !isReadOnlyRequest(ctx)) {
|
||||||
|
ctx.status = 403;
|
||||||
|
ctx.body = { code: 403, message: '主体已停用,仅可查看与导出,不能新建、修改、推送或删除数据' };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** 只读方法(不改变数据) */
|
||||||
|
const READ_METHODS = new Set([ 'GET', 'HEAD', 'OPTIONS' ]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停用主体下仍允许的“读”请求
|
||||||
|
* - 常规 GET/HEAD/OPTIONS
|
||||||
|
* - 导出任务提交(POST /api/export-tasks):属于查看数据,按主体停用策略放行
|
||||||
|
* @param {Object} ctx koa 上下文
|
||||||
|
* @return {boolean} 是否放行
|
||||||
|
*/
|
||||||
|
function isReadOnlyRequest(ctx) {
|
||||||
|
if (READ_METHODS.has(ctx.method)) return true;
|
||||||
|
if (ctx.method === 'POST' && ctx.path === '/api/export-tasks') return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@ -39,11 +39,15 @@ module.exports = {
|
|||||||
running = true;
|
running = true;
|
||||||
try {
|
try {
|
||||||
const Op = ctx.app.Sequelize.Op;
|
const Op = ctx.app.Sequelize.Op;
|
||||||
|
// 主体停用后不再自动拉单
|
||||||
|
const enabledPrincipalIds = await ctx.service.cache.getEnabledPrincipalIds();
|
||||||
|
if (!enabledPrincipalIds.length) return { shops: 0 };
|
||||||
const shops = await ctx.model.Shop.findAll({
|
const shops = await ctx.model.Shop.findAll({
|
||||||
where: {
|
where: {
|
||||||
type: 2,
|
type: 2,
|
||||||
status: 1,
|
status: 1,
|
||||||
auto_fetch_order: 1,
|
auto_fetch_order: 1,
|
||||||
|
principal_id: { [Op.in]: enabledPrincipalIds },
|
||||||
external_print_account_id: { [Op.ne]: null },
|
external_print_account_id: { [Op.ne]: null },
|
||||||
},
|
},
|
||||||
order: [[ 'id', 'ASC' ]],
|
order: [[ 'id', 'ASC' ]],
|
||||||
|
|||||||
@ -148,7 +148,8 @@ class CacheService extends Service {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户可选主体列表(头部主体切换/下拉选择用,含该主体下的角色与默认标识)
|
* 用户可选主体列表(头部主体切换/下拉选择用,含该主体下的角色与默认标识)
|
||||||
* 只返回启用状态的关联:员工在某个主体下被停用后,登录后不再展示该主体
|
* 只返回启用状态的关联:员工在某个主体下被停用后,登录后不再展示该主体;
|
||||||
|
* 主体本身停用时仍返回(前端置灰不可选、标注「已停用」),并按「启用优先」排序
|
||||||
* @param {Number} userId 用户ID
|
* @param {Number} userId 用户ID
|
||||||
* @return {Promise<Array<Object>>} 主体选项列表
|
* @return {Promise<Array<Object>>} 主体选项列表
|
||||||
*/
|
*/
|
||||||
@ -181,10 +182,31 @@ class CacheService extends Service {
|
|||||||
is_default: item.is_default,
|
is_default: item.is_default,
|
||||||
relation_status: item.status,
|
relation_status: item.status,
|
||||||
};
|
};
|
||||||
}).filter(Boolean);
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
// 启用主体排在前面,保证切换/默认选择不会落在已停用主体上
|
||||||
|
.sort((a, b) => {
|
||||||
|
const enabledDiff = (Number(b.status) === 1 ? 1 : 0) - (Number(a.status) === 1 ? 1 : 0);
|
||||||
|
if (enabledDiff) return enabledDiff;
|
||||||
|
return Number(b.is_default) - Number(a.is_default);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 已启用的主体ID列表(停用的主体不参与定时任务、推送与自动拉单)
|
||||||
|
* 不做缓存:任务频率低,且主体状态变更需要立即生效
|
||||||
|
* @return {Promise<Array<Number>>} 主体ID数组
|
||||||
|
*/
|
||||||
|
async getEnabledPrincipalIds() {
|
||||||
|
const rows = await this.ctx.model.Principal.findAll({
|
||||||
|
where: { status: 1 },
|
||||||
|
attributes: [ 'id' ],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
return rows.map(row => Number(row.id));
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- 主体信息 ----------
|
// ---------- 主体信息 ----------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -258,10 +258,14 @@ class ExternalSendSyncService extends Service {
|
|||||||
async syncPending({ limit = 50 } = {}) {
|
async syncPending({ limit = 50 } = {}) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const Op = ctx.app.Sequelize.Op;
|
const Op = ctx.app.Sequelize.Op;
|
||||||
|
// 主体停用后不再回传平台
|
||||||
|
const enabledPrincipalIds = await ctx.service.cache.getEnabledPrincipalIds();
|
||||||
|
if (!enabledPrincipalIds.length) return { total: 0, success: 0, failed: 0, exception: 0 };
|
||||||
const rows = await ctx.model.OrderStatusDelivery.findAll({
|
const rows = await ctx.model.OrderStatusDelivery.findAll({
|
||||||
where: {
|
where: {
|
||||||
external_send_status: { [Op.in]: [ SEND_STATUS.PENDING, SEND_STATUS.RETRY ] },
|
external_send_status: { [Op.in]: [ SEND_STATUS.PENDING, SEND_STATUS.RETRY ] },
|
||||||
external_send_times: { [Op.lt]: MAX_SEND_TIMES },
|
external_send_times: { [Op.lt]: MAX_SEND_TIMES },
|
||||||
|
principal_id: { [Op.in]: enabledPrincipalIds },
|
||||||
},
|
},
|
||||||
order: [[ 'external_send_at', 'ASC' ]],
|
order: [[ 'external_send_at', 'ASC' ]],
|
||||||
limit,
|
limit,
|
||||||
|
|||||||
@ -318,6 +318,7 @@ class OrderDeliveryPushService extends Service {
|
|||||||
where: {
|
where: {
|
||||||
status: 2,
|
status: 2,
|
||||||
warehouse_id: { [Op.ne]: null },
|
warehouse_id: { [Op.ne]: null },
|
||||||
|
principal_id: { [Op.in]: await ctx.service.cache.getEnabledPrincipalIds() },
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{ pushed_at: null },
|
{ pushed_at: null },
|
||||||
{ pushed_at: { [Op.lt]: throttleBefore } },
|
{ pushed_at: { [Op.lt]: throttleBefore } },
|
||||||
|
|||||||
@ -327,6 +327,16 @@ class OrderSyncTaskService extends Service {
|
|||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const Model = ctx.model.OrderSyncTask;
|
const Model = ctx.model.OrderSyncTask;
|
||||||
|
|
||||||
|
// 主体停用:不再拉单,任务直接标记失败并写明原因,避免排队卡死
|
||||||
|
const principal = await ctx.service.cache.getPrincipal(task.principal_id);
|
||||||
|
if (!principal || Number(principal.status) !== 1) {
|
||||||
|
await Model.update(
|
||||||
|
{ status: STATUS.FAILED, error_message: '主体已停用,不再拉取订单', finished_at: new Date(), progress: 100 },
|
||||||
|
{ where: { id: task.id } }
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let shops = [];
|
let shops = [];
|
||||||
try {
|
try {
|
||||||
const access = await this.resolveAccess(task.created_by, task.principal_id);
|
const access = await this.resolveAccess(task.created_by, task.principal_id);
|
||||||
|
|||||||
@ -432,6 +432,7 @@ class OrderWmsService extends Service {
|
|||||||
const orders = await ctx.model.OrderStatusQueued.findAll({
|
const orders = await ctx.model.OrderStatusQueued.findAll({
|
||||||
where: {
|
where: {
|
||||||
status: ORDER_STATUS.QUEUED,
|
status: ORDER_STATUS.QUEUED,
|
||||||
|
principal_id: { [Op.in]: await ctx.service.cache.getEnabledPrincipalIds() },
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{ pushed_at: null },
|
{ pushed_at: null },
|
||||||
{ pushed_at: { [Op.lt]: throttleBefore } },
|
{ pushed_at: { [Op.lt]: throttleBefore } },
|
||||||
|
|||||||
@ -446,6 +446,7 @@ class OutboundOrderPushService extends Service {
|
|||||||
where: {
|
where: {
|
||||||
status: 2,
|
status: 2,
|
||||||
warehouse_id: { [Op.ne]: null },
|
warehouse_id: { [Op.ne]: null },
|
||||||
|
principal_id: { [Op.in]: await ctx.service.cache.getEnabledPrincipalIds() },
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{ pushed_at: null },
|
{ pushed_at: null },
|
||||||
{ pushed_at: { [Op.lt]: throttleBefore } },
|
{ pushed_at: { [Op.lt]: throttleBefore } },
|
||||||
|
|||||||
@ -17,7 +17,16 @@ export interface UserInfo {
|
|||||||
last_login_at: string | null
|
last_login_at: string | null
|
||||||
created_at: string
|
created_at: string
|
||||||
updated_at: string
|
updated_at: string
|
||||||
principals?: { id: number; name: string; account_name?: string | null; account_avatar?: string | null }[]
|
principals?: {
|
||||||
|
id: number
|
||||||
|
name: string
|
||||||
|
/** 主体状态:1启用 0停用 */
|
||||||
|
status?: number
|
||||||
|
/** 主体是否已停用(停用后下拉置灰不可选,数据只读) */
|
||||||
|
disabled?: boolean
|
||||||
|
account_name?: string | null
|
||||||
|
account_avatar?: string | null
|
||||||
|
}[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RegisterPayload {
|
export interface RegisterPayload {
|
||||||
|
|||||||
@ -107,7 +107,7 @@ const accountProfile = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
// 当前主体若已不在所属列表内(如切换账号),重置为第一个
|
// 当前主体若已不在所属列表内(如切换账号),重置为第一个(后端已按「启用优先」排序)
|
||||||
const ids = (auth.user?.principals || []).map((p) => p.id)
|
const ids = (auth.user?.principals || []).map((p) => p.id)
|
||||||
principalStore.ensureValid(ids)
|
principalStore.ensureValid(ids)
|
||||||
// 计算标签条可见数量,处理溢出折叠
|
// 计算标签条可见数量,处理溢出折叠
|
||||||
@ -150,6 +150,13 @@ function goExport() {
|
|||||||
router.push('/export-tasks')
|
router.push('/export-tasks')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 当前主体是否已停用(停用后只读:后端拒绝写操作) */
|
||||||
|
const currentPrincipalDisabled = computed(() => {
|
||||||
|
const currentId = principalStore.currentId
|
||||||
|
const item = (auth.user?.principals || []).find(p => Number(p.id) === Number(currentId))
|
||||||
|
return Boolean(item?.disabled)
|
||||||
|
})
|
||||||
|
|
||||||
// 切换当前主体:持久化到 localStorage 并刷新页面以按新主体重新拉取数据
|
// 切换当前主体:持久化到 localStorage 并刷新页面以按新主体重新拉取数据
|
||||||
function onPrincipalChange(id: number) {
|
function onPrincipalChange(id: number) {
|
||||||
principalStore.setCurrentId(id)
|
principalStore.setCurrentId(id)
|
||||||
@ -214,8 +221,9 @@ function onPrincipalChange(id: number) {
|
|||||||
<el-option
|
<el-option
|
||||||
v-for="p in auth.user.principals"
|
v-for="p in auth.user.principals"
|
||||||
:key="p.id"
|
:key="p.id"
|
||||||
:label="`${p.name}`"
|
:label="p.disabled ? `${p.name}(已停用)` : p.name"
|
||||||
:value="p.id"
|
:value="p.id"
|
||||||
|
:disabled="p.disabled"
|
||||||
/>
|
/>
|
||||||
</el-select>
|
</el-select>
|
||||||
<el-dropdown v-if="auth.user">
|
<el-dropdown v-if="auth.user">
|
||||||
@ -235,6 +243,14 @@ function onPrincipalChange(id: number) {
|
|||||||
<el-button v-else text type="primary" @click="goLogin">登录</el-button>
|
<el-button v-else text type="primary" @click="goLogin">登录</el-button>
|
||||||
</div>
|
</div>
|
||||||
</el-header>
|
</el-header>
|
||||||
|
<el-alert
|
||||||
|
v-if="currentPrincipalDisabled"
|
||||||
|
class="principal-disabled-tip"
|
||||||
|
type="warning"
|
||||||
|
:closable="false"
|
||||||
|
show-icon
|
||||||
|
title="当前主体已停用:数据仅可查看与导出,新建、编辑、推送、删除等操作不可用;如需恢复请联系平台管理员"
|
||||||
|
/>
|
||||||
<el-main class="app-main">
|
<el-main class="app-main">
|
||||||
<RouterView v-slot="{ Component }">
|
<RouterView v-slot="{ Component }">
|
||||||
<KeepAlive :max="20">
|
<KeepAlive :max="20">
|
||||||
@ -298,6 +314,11 @@ function onPrincipalChange(id: number) {
|
|||||||
z-index: 0;
|
z-index: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 主体停用提示:紧贴顶栏下方,不占页面主体高度 */
|
||||||
|
.principal-disabled-tip {
|
||||||
|
border-radius: 0;
|
||||||
|
}
|
||||||
|
|
||||||
.user-area {
|
.user-area {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@ -18,7 +18,10 @@ export const usePrincipalStore = defineStore('principal', () => {
|
|||||||
else localStorage.setItem(CURRENT_PRINCIPAL_KEY, String(id))
|
else localStorage.setItem(CURRENT_PRINCIPAL_KEY, String(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 若当前主体不在可选列表内,重置为列表第一个(或不选) */
|
/**
|
||||||
|
* 若当前主体不在可选列表内,重置为列表第一个(或不选)
|
||||||
|
* @param principalIds 可选主体ID(后端已按「启用优先」排序)
|
||||||
|
*/
|
||||||
function ensureValid(principalIds: number[]) {
|
function ensureValid(principalIds: number[]) {
|
||||||
if (currentId.value == null) return
|
if (currentId.value == null) return
|
||||||
if (!principalIds.includes(currentId.value)) {
|
if (!principalIds.includes(currentId.value)) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user