feat: add tenant bound restriction for IM dashboard and management pages
1. extract isTenantBound utility from user store logic 2. add disabled state to tenant select components across all IM pages 3. update policy tenant options logic to hide global option for bound tenants 4. add backend scope validation for IM admin operations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Ref } from 'vue';
|
||||
import { List as TenantList } from '@/api/imTenant';
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
import type { FormSchema } from '@/components/Form';
|
||||
|
||||
export interface ImTenantOption {
|
||||
label: string;
|
||||
@@ -26,15 +27,10 @@ export async function loadImTenantOptions(
|
||||
target: Ref<ImTenantOption[]>,
|
||||
params: Record<string, any> = { page: 1, perPage: 100 }
|
||||
) {
|
||||
const userStore = useUserStore();
|
||||
const userInfo = { ...readTokenUserInfo(userStore.getToken), ...(userStore.getUserInfo as any) };
|
||||
const boundTenantId = Number(userInfo?.tenantId || userInfo?.tenantID || 0);
|
||||
const boundTenantId = getBoundTenantId();
|
||||
|
||||
if (!userStore.isCompanyDept) {
|
||||
if (boundTenantId <= 0) {
|
||||
target.value = [];
|
||||
return target.value;
|
||||
}
|
||||
// 登录用户已绑定商户租户:仅返回自己商户选项(与后端 resolveImAdminScope 强制范围一致)
|
||||
if (boundTenantId > 0) {
|
||||
let tenant: any = null;
|
||||
try {
|
||||
const res = await TenantList({ ...params, id: boundTenantId, page: 1, perPage: 1 });
|
||||
@@ -42,20 +38,11 @@ export async function loadImTenantOptions(
|
||||
} catch {
|
||||
tenant = null;
|
||||
}
|
||||
target.value = [
|
||||
tenant
|
||||
? toTenantOption(tenant)
|
||||
: {
|
||||
label: formatImTenantLabel({
|
||||
id: boundTenantId,
|
||||
name: userInfo?.tenantName,
|
||||
code: userInfo?.tenantCode,
|
||||
}),
|
||||
value: boundTenantId,
|
||||
code: userInfo?.tenantCode,
|
||||
name: userInfo?.tenantName,
|
||||
},
|
||||
];
|
||||
if (!tenant) {
|
||||
const info = readTokenUserInfo(useUserStore().getToken);
|
||||
tenant = { id: boundTenantId, name: info?.tenantName, code: info?.tenantCode };
|
||||
}
|
||||
target.value = [toTenantOption(tenant)];
|
||||
return target.value;
|
||||
}
|
||||
|
||||
@@ -64,6 +51,26 @@ export async function loadImTenantOptions(
|
||||
return target.value;
|
||||
}
|
||||
|
||||
// 登录用户绑定的商户租户ID(按绑定租户判定,与后端 resolveImAdminScope 的范围逻辑一致)
|
||||
export function getBoundTenantId(): number {
|
||||
const userStore = useUserStore();
|
||||
const userInfo = { ...readTokenUserInfo(userStore.getToken), ...(userStore.getUserInfo as any) };
|
||||
return Number(userInfo?.tenantId || userInfo?.tenantID || 0);
|
||||
}
|
||||
|
||||
// 登录用户是否为商户绑定账号(true=下拉框只显示并锁定自己商户)
|
||||
export function isTenantBound(): boolean {
|
||||
return getBoundTenantId() > 0;
|
||||
}
|
||||
|
||||
// 商户绑定账号:锁定搜索表单中的商户下拉项(仅一项、不可清除、不可更改)
|
||||
export function lockTenantSchema(schema?: FormSchema) {
|
||||
if (!schema || !isTenantBound()) return;
|
||||
const props: any = (schema.componentProps = schema.componentProps || {});
|
||||
props.disabled = true;
|
||||
props.clearable = false;
|
||||
}
|
||||
|
||||
function toTenantOption(item: any): ImTenantOption {
|
||||
return {
|
||||
label: formatImTenantLabel(item),
|
||||
|
||||
Reference in New Issue
Block a user