mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 11:49:20 +08:00
feat: 优化开放接口创建
This commit is contained in:
+206
-53
@@ -7,14 +7,15 @@ import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import {
|
||||
Alert,
|
||||
Checkbox,
|
||||
DatePicker,
|
||||
Empty,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
message,
|
||||
Select,
|
||||
Tag,
|
||||
Tree,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
@@ -30,9 +31,18 @@ defineOptions({ name: 'CreateApiKeyModal' });
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
type PermissionTreeNode = {
|
||||
key: number;
|
||||
title: string;
|
||||
code?: string;
|
||||
bindable?: boolean;
|
||||
companyPermissionId?: number;
|
||||
children?: PermissionTreeNode[];
|
||||
};
|
||||
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
draggable: true,
|
||||
class: 'w-[560px]',
|
||||
class: 'w-[820px]',
|
||||
onCancel() {
|
||||
handleCancel();
|
||||
},
|
||||
@@ -43,7 +53,9 @@ const [Modal, modalApi] = useVbenModal({
|
||||
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const permissionOptions = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
|
||||
const permissionTree = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
|
||||
const checkedPermissionIds = ref<number[]>([]);
|
||||
const expandedKeys = ref<number[]>([]);
|
||||
const createdKey = ref<string | null>(null);
|
||||
|
||||
const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
||||
@@ -54,10 +66,104 @@ const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
||||
ipWhitelist: [],
|
||||
});
|
||||
|
||||
const modalTitle = computed(() => (createdKey.value ? 'API Key 已创建' : '创建 API Key'));
|
||||
const modalTitle = computed(() =>
|
||||
createdKey.value ? 'API Key 已创建' : '创建 API Key',
|
||||
);
|
||||
|
||||
const treeData = computed(() => mapToTreeData(permissionTree.value));
|
||||
|
||||
const hasBindablePermission = computed(
|
||||
() => collectBindableCompanyPermissionIds(permissionTree.value).length > 0,
|
||||
);
|
||||
|
||||
const selectedBindableCount = computed(
|
||||
() => formData.value.companyPermissionIds.length,
|
||||
);
|
||||
|
||||
async function loadPermissions() {
|
||||
permissionOptions.value = await getOpenApiKeyAvailablePermissionsApi();
|
||||
permissionTree.value = await getOpenApiKeyAvailablePermissionsApi();
|
||||
expandedKeys.value = collectExpandableKeys(permissionTree.value);
|
||||
}
|
||||
|
||||
function mapToTreeData(
|
||||
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||
): PermissionTreeNode[] {
|
||||
return nodes.map((node) => ({
|
||||
key: node.permissionId,
|
||||
title: node.name,
|
||||
code: node.code,
|
||||
bindable: node.bindable === true,
|
||||
companyPermissionId: node.companyPermissionId,
|
||||
children:
|
||||
node.children && node.children.length > 0
|
||||
? mapToTreeData(node.children)
|
||||
: undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
function collectExpandableKeys(
|
||||
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||
): number[] {
|
||||
const keys: number[] = [];
|
||||
for (const node of nodes) {
|
||||
if (node.children && node.children.length > 0) {
|
||||
keys.push(node.permissionId);
|
||||
keys.push(...collectExpandableKeys(node.children));
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** 树中全部可绑定叶子的 companyPermissionId */
|
||||
function collectBindableCompanyPermissionIds(
|
||||
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||
): number[] {
|
||||
const ids: number[] = [];
|
||||
for (const node of nodes) {
|
||||
if (node.bindable && node.companyPermissionId != null) {
|
||||
ids.push(node.companyPermissionId);
|
||||
}
|
||||
if (node.children?.length) {
|
||||
ids.push(...collectBindableCompanyPermissionIds(node.children));
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** 根据勾选的 permissionId,收集可绑定叶子的 companyPermissionId */
|
||||
function resolveCompanyPermissionIds(
|
||||
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||
checkedPermissionIdSet: Set<number>,
|
||||
): number[] {
|
||||
const ids: number[] = [];
|
||||
for (const node of nodes) {
|
||||
if (
|
||||
node.bindable &&
|
||||
node.companyPermissionId != null &&
|
||||
checkedPermissionIdSet.has(node.permissionId)
|
||||
) {
|
||||
ids.push(node.companyPermissionId);
|
||||
}
|
||||
if (node.children?.length) {
|
||||
ids.push(
|
||||
...resolveCompanyPermissionIds(node.children, checkedPermissionIdSet),
|
||||
);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
function handleTreeCheck(
|
||||
checked:
|
||||
| (number | string)[]
|
||||
| { checked: (number | string)[]; halfChecked: (number | string)[] },
|
||||
) {
|
||||
const keys = Array.isArray(checked) ? checked : checked.checked;
|
||||
checkedPermissionIds.value = keys.map((k) => Number(k));
|
||||
formData.value.companyPermissionIds = resolveCompanyPermissionIds(
|
||||
permissionTree.value,
|
||||
new Set(checkedPermissionIds.value),
|
||||
);
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
@@ -68,6 +174,7 @@ function resetForm() {
|
||||
rateLimit: 60,
|
||||
ipWhitelist: [],
|
||||
};
|
||||
checkedPermissionIds.value = [];
|
||||
createdKey.value = null;
|
||||
}
|
||||
|
||||
@@ -82,7 +189,7 @@ async function handleSubmit() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (permissionOptions.value.length === 0) {
|
||||
if (!hasBindablePermission.value) {
|
||||
message.warning('当前公司尚未分配开放接口权限,请联系平台管理员');
|
||||
return;
|
||||
}
|
||||
@@ -158,63 +265,109 @@ defineExpose({
|
||||
<Tag color="blue" class="cursor-pointer" @click="copyKey">点击复制 Key</Tag>
|
||||
</div>
|
||||
|
||||
<Form v-else ref="formRef" :model="formData" layout="vertical">
|
||||
<Form v-else ref="formRef" :model="formData" layout="vertical" class="mb-0">
|
||||
<Alert
|
||||
v-if="permissionOptions.length === 0"
|
||||
v-if="!hasBindablePermission"
|
||||
class="mb-4"
|
||||
message="当前公司尚未分配开放接口权限(open:* 叶子 api),请先在「公司权限分配」中配置。"
|
||||
message="当前公司尚未分配可绑定的开放接口权限,请先在「公司权限分配」中配置 open 域权限。"
|
||||
show-icon
|
||||
type="info"
|
||||
/>
|
||||
|
||||
<Form.Item
|
||||
label="Key 名称"
|
||||
name="name"
|
||||
:rules="[{ required: true, message: '请输入 Key 名称' }]"
|
||||
>
|
||||
<Input v-model:value="formData.name" placeholder="如:Agent-订单导入" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="开放权限"
|
||||
name="companyPermissionIds"
|
||||
:rules="[{ required: true, type: 'array', min: 1, message: '请选择至少一个权限' }]"
|
||||
>
|
||||
<Checkbox.Group v-model:value="formData.companyPermissionIds" class="flex flex-col gap-2">
|
||||
<Checkbox
|
||||
v-for="item in permissionOptions"
|
||||
:key="item.companyPermissionId"
|
||||
:value="item.companyPermissionId"
|
||||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
<!-- 左:基本信息 -->
|
||||
<div class="min-w-0 space-y-1">
|
||||
<div class="mb-2 text-sm font-medium text-foreground">基本信息</div>
|
||||
<Form.Item
|
||||
label="Key 名称"
|
||||
name="name"
|
||||
:rules="[{ required: true, message: '请输入 Key 名称' }]"
|
||||
>
|
||||
{{ item.name }} ({{ item.code }})
|
||||
</Checkbox>
|
||||
</Checkbox.Group>
|
||||
</Form.Item>
|
||||
<Input v-model:value="formData.name" placeholder="如:Agent-订单导入" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="每分钟请求上限" name="rateLimit">
|
||||
<InputNumber v-model:value="formData.rateLimit" :min="1" :max="10000" class="w-full" />
|
||||
</Form.Item>
|
||||
<Form.Item label="每分钟请求上限" name="rateLimit">
|
||||
<InputNumber
|
||||
v-model:value="formData.rateLimit"
|
||||
:min="1"
|
||||
:max="10000"
|
||||
class="w-full"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="过期时间(可选)" name="expireAt">
|
||||
<DatePicker
|
||||
v-model:value="formData.expireAt"
|
||||
show-time
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
placeholder="留空表示永不过期"
|
||||
class="w-full"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="过期时间(可选)" name="expireAt">
|
||||
<DatePicker
|
||||
v-model:value="formData.expireAt"
|
||||
show-time
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
placeholder="留空表示永不过期"
|
||||
class="w-full"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="IP 白名单(可选)" name="ipWhitelist">
|
||||
<Select
|
||||
v-model:value="formData.ipWhitelist"
|
||||
mode="tags"
|
||||
placeholder="留空表示不限制,输入 IP 后回车"
|
||||
:token-separators="[',', ' ']"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="IP 白名单(可选)" name="ipWhitelist">
|
||||
<Select
|
||||
v-model:value="formData.ipWhitelist"
|
||||
mode="tags"
|
||||
placeholder="留空表示不限制,输入 IP 后回车"
|
||||
:token-separators="[',', ' ']"
|
||||
/>
|
||||
</Form.Item>
|
||||
</div>
|
||||
|
||||
<!-- 右:开放权限树 -->
|
||||
<div class="min-w-0">
|
||||
<div class="mb-2 flex items-center justify-between gap-2">
|
||||
<span class="text-sm font-medium text-foreground">开放权限</span>
|
||||
<span class="text-xs text-muted-foreground">
|
||||
已选 {{ selectedBindableCount }} 项 scope
|
||||
</span>
|
||||
</div>
|
||||
<Form.Item
|
||||
name="companyPermissionIds"
|
||||
class="mb-0"
|
||||
:rules="[
|
||||
{
|
||||
required: true,
|
||||
type: 'array',
|
||||
min: 1,
|
||||
message: '请选择至少一个开放权限',
|
||||
},
|
||||
]"
|
||||
>
|
||||
<div
|
||||
class="max-h-[360px] min-h-[280px] overflow-y-auto rounded-md border border-border px-2 py-2"
|
||||
>
|
||||
<Empty
|
||||
v-if="!hasBindablePermission"
|
||||
class="py-10"
|
||||
description="暂无可选开放权限"
|
||||
/>
|
||||
<Tree
|
||||
v-else
|
||||
v-model:expanded-keys="expandedKeys"
|
||||
checkable
|
||||
:tree-data="treeData"
|
||||
:checked-keys="checkedPermissionIds"
|
||||
:selectable="false"
|
||||
@check="handleTreeCheck"
|
||||
>
|
||||
<template #title="node">
|
||||
<span>{{ node.title }}</span>
|
||||
<span
|
||||
v-if="node.code"
|
||||
class="ml-1 text-xs text-muted-foreground"
|
||||
>
|
||||
{{ node.code }}
|
||||
</span>
|
||||
</template>
|
||||
</Tree>
|
||||
</div>
|
||||
</Form.Item>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
|
||||
<template #confirmText>{{ createdKey ? '我已保存,关闭' : '创建' }}</template>
|
||||
|
||||
@@ -25,12 +25,6 @@ const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
const loading = ref(false);
|
||||
const createModalRef = ref<InstanceType<typeof CreateApiKeyModal>>();
|
||||
|
||||
const permissionLabelMap: Record<string, string> = {
|
||||
'open:product:adfee': '商品广告费',
|
||||
'open:shop:pdd:read': '拼多多店铺',
|
||||
'open:extension:feature:read': '扩展功能',
|
||||
};
|
||||
|
||||
function formatExpireAt(cellValue?: null | string) {
|
||||
return cellValue ? String(cellValue) : '永不过期';
|
||||
}
|
||||
@@ -127,10 +121,7 @@ function getPermissionTooltip(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
||||
if (row.permissions?.length) {
|
||||
return row.permissions.map((item) => `${item.name} (${item.code})`);
|
||||
}
|
||||
return (row.permissionCodes ?? []).map((code) => {
|
||||
const label = permissionLabelMap[code] || code;
|
||||
return label === code ? code : `${label} (${code})`;
|
||||
});
|
||||
return row.permissionCodes ?? [];
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user