mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 00:29:19 +08:00
wip: 可能基本完成的开放模块
This commit is contained in:
+222
@@ -0,0 +1,222 @@
|
||||
<script lang="ts" setup>
|
||||
import type { OpenApiKeyDTO, OpenApiKeyVO } from '#/api/open/api-key';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import {
|
||||
Alert,
|
||||
Checkbox,
|
||||
DatePicker,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
message,
|
||||
Select,
|
||||
Tag,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createOpenApiKeyApi,
|
||||
getOpenApiKeyAvailablePermissionsApi,
|
||||
} from '#/api/open/api-key';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success', result: OpenApiKeyVO.CreateOpenApiKeyResultVO): void;
|
||||
}
|
||||
|
||||
defineOptions({ name: 'CreateApiKeyModal' });
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
draggable: true,
|
||||
class: 'w-[560px]',
|
||||
onCancel() {
|
||||
handleCancel();
|
||||
},
|
||||
onConfirm() {
|
||||
handleSubmit();
|
||||
},
|
||||
});
|
||||
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const permissionOptions = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
|
||||
const createdKey = ref<string | null>(null);
|
||||
|
||||
const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
||||
name: '',
|
||||
companyPermissionIds: [],
|
||||
expireAt: undefined,
|
||||
rateLimit: 60,
|
||||
ipWhitelist: [],
|
||||
});
|
||||
|
||||
const modalTitle = computed(() => (createdKey.value ? 'API Key 已创建' : '创建 API Key'));
|
||||
|
||||
async function loadPermissions() {
|
||||
permissionOptions.value = await getOpenApiKeyAvailablePermissionsApi();
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
formData.value = {
|
||||
name: '',
|
||||
companyPermissionIds: [],
|
||||
expireAt: undefined,
|
||||
rateLimit: 60,
|
||||
ipWhitelist: [],
|
||||
};
|
||||
createdKey.value = null;
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
resetForm();
|
||||
modalApi.close();
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (createdKey.value) {
|
||||
handleCancel();
|
||||
return;
|
||||
}
|
||||
|
||||
if (permissionOptions.value.length === 0) {
|
||||
message.warning('当前公司尚未分配开放接口权限,请联系平台管理员');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
const result = await createOpenApiKeyApi(formData.value);
|
||||
createdKey.value = result.plainKey;
|
||||
message.success('API Key 创建成功,请立即保存明文 Key');
|
||||
emit('success', result);
|
||||
} catch {
|
||||
// 校验或请求失败
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function copyText(text: string) {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return;
|
||||
}
|
||||
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = text;
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.left = '-9999px';
|
||||
document.body.appendChild(textarea);
|
||||
textarea.focus();
|
||||
textarea.select();
|
||||
try {
|
||||
const ok = document.execCommand('copy');
|
||||
if (!ok) {
|
||||
throw new Error('copy failed');
|
||||
}
|
||||
} finally {
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyKey() {
|
||||
if (!createdKey.value) return;
|
||||
try {
|
||||
await copyText(createdKey.value);
|
||||
message.success('已复制到剪贴板');
|
||||
} catch {
|
||||
message.error('复制失败,请手动选择文本复制');
|
||||
}
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
open: async () => {
|
||||
resetForm();
|
||||
await loadPermissions();
|
||||
modalApi.open();
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal :title="modalTitle" :confirm-loading="loading" :show-cancel-button="!createdKey">
|
||||
<Alert
|
||||
v-if="createdKey"
|
||||
class="mb-4"
|
||||
message="明文 Key 仅显示一次,关闭后将无法再次查看,请立即复制保存。"
|
||||
show-icon
|
||||
type="warning"
|
||||
/>
|
||||
|
||||
<div v-if="createdKey" class="space-y-3">
|
||||
<Input.TextArea :rows="3" :value="createdKey" readonly />
|
||||
<Tag color="blue" class="cursor-pointer" @click="copyKey">点击复制 Key</Tag>
|
||||
</div>
|
||||
|
||||
<Form v-else ref="formRef" :model="formData" layout="vertical">
|
||||
<Alert
|
||||
v-if="permissionOptions.length === 0"
|
||||
class="mb-4"
|
||||
message="当前公司尚未分配 open:scope:* 开放权限,请先在「公司权限分配」中配置。"
|
||||
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"
|
||||
>
|
||||
{{ item.name }} ({{ item.code }})
|
||||
</Checkbox>
|
||||
</Checkbox.Group>
|
||||
</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="IP 白名单(可选)" name="ipWhitelist">
|
||||
<Select
|
||||
v-model:value="formData.ipWhitelist"
|
||||
mode="tags"
|
||||
placeholder="留空表示不限制,输入 IP 后回车"
|
||||
:token-separators="[',', ' ']"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
|
||||
<template #confirmText>{{ createdKey ? '我已保存,关闭' : '创建' }}</template>
|
||||
</Modal>
|
||||
</template>
|
||||
@@ -0,0 +1,193 @@
|
||||
<script lang="ts" setup>
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { OpenApiKeyVO } from '#/api/open/api-key';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Button, message, Popconfirm, Space, Tag, Tooltip } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import {
|
||||
deleteOpenApiKeyApi,
|
||||
disableOpenApiKeyApi,
|
||||
enableOpenApiKeyApi,
|
||||
getOpenApiKeyListApi,
|
||||
} from '#/api/open/api-key';
|
||||
|
||||
import CreateApiKeyModal from './components/CreateApiKeyModal.vue';
|
||||
|
||||
defineOptions({ name: 'OpenApiKeyManagement' });
|
||||
|
||||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
const loading = ref(false);
|
||||
const createModalRef = ref<InstanceType<typeof CreateApiKeyModal>>();
|
||||
|
||||
const permissionLabelMap: Record<string, string> = {
|
||||
'open:scope:order:import': '订单导入',
|
||||
'open:scope:order:read': '订单字段',
|
||||
'open:scope:promotion_fee:import': '推广费导入',
|
||||
'open:scope:promotion_fee:read': '推广费字段',
|
||||
'open:scope:import_task:read': '导入任务',
|
||||
'open:scope:meta:read': '元数据',
|
||||
};
|
||||
|
||||
function formatExpireAt(cellValue?: null | string) {
|
||||
return cellValue ? String(cellValue) : '永不过期';
|
||||
}
|
||||
|
||||
const gridOptions: VxeGridProps<OpenApiKeyVO.OpenApiKeyInfoVO> = {
|
||||
columns: [
|
||||
{ type: 'seq', title: '序号', minWidth: 60 },
|
||||
{ field: 'name', title: '名称', minWidth: 140 },
|
||||
{ field: 'keyPrefix', title: 'Key 前缀', minWidth: 180 },
|
||||
{
|
||||
field: 'permissionCodes',
|
||||
title: '开放权限',
|
||||
minWidth: 100,
|
||||
slots: { default: 'permissions' },
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 90,
|
||||
slots: { default: 'status' },
|
||||
},
|
||||
{ field: 'rateLimit', title: '限流/分钟', minWidth: 100 },
|
||||
{
|
||||
field: 'expireAt',
|
||||
title: '过期时间',
|
||||
minWidth: 160,
|
||||
showOverflow: 'tooltip',
|
||||
formatter: ({ cellValue }: { cellValue?: null | string }) =>
|
||||
formatExpireAt(cellValue),
|
||||
},
|
||||
{ field: 'createTime', title: '创建时间', minWidth: 160 },
|
||||
{
|
||||
title: '操作',
|
||||
width: 200,
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
rowConfig: { isHover: true },
|
||||
showOverflow: true,
|
||||
size: 'small',
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
refresh: true,
|
||||
zoom: true,
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const list = await getOpenApiKeyListApi();
|
||||
return { items: list, total: list.length };
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
|
||||
|
||||
async function handleDisable(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
||||
await disableOpenApiKeyApi(row.id);
|
||||
message.success('已停用');
|
||||
gridApi.reload();
|
||||
}
|
||||
|
||||
async function handleEnable(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
||||
await enableOpenApiKeyApi(row.id);
|
||||
message.success('已启用');
|
||||
gridApi.reload();
|
||||
}
|
||||
|
||||
async function handleDelete(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
||||
await deleteOpenApiKeyApi(row.id);
|
||||
message.success('已删除');
|
||||
gridApi.reload();
|
||||
}
|
||||
|
||||
function openCreateModal() {
|
||||
createModalRef.value?.open();
|
||||
}
|
||||
|
||||
function getPermissionCount(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
||||
return row.permissionCodes?.length ?? row.permissions?.length ?? 0;
|
||||
}
|
||||
|
||||
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})`;
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page auto-content-height>
|
||||
<Grid :loading="loading">
|
||||
<template #toolbar-tools>
|
||||
<Button type="primary" @click="openCreateModal">
|
||||
<PlusIcon class="size-4" />
|
||||
创建 API Key
|
||||
</Button>
|
||||
</template>
|
||||
|
||||
<template #permissions="{ row }">
|
||||
<Tooltip v-if="getPermissionCount(row) > 0">
|
||||
<template #title>
|
||||
<div v-for="(line, index) in getPermissionTooltip(row)" :key="index">
|
||||
{{ line }}
|
||||
</div>
|
||||
</template>
|
||||
<span class="cursor-default">{{ getPermissionCount(row) }} 项</span>
|
||||
</Tooltip>
|
||||
<span v-else class="text-gray-400">0 项</span>
|
||||
</template>
|
||||
|
||||
<template #status="{ row }">
|
||||
<Tag :color="row.status === 1 ? 'success' : 'default'">
|
||||
{{ row.status === 1 ? '正常' : '停用' }}
|
||||
</Tag>
|
||||
</template>
|
||||
|
||||
<template #action="{ row }">
|
||||
<Space>
|
||||
<Popconfirm
|
||||
v-if="row.status === 1"
|
||||
title="确认停用该 API Key?"
|
||||
@confirm="handleDisable(row)"
|
||||
>
|
||||
<Button type="link" size="small">停用</Button>
|
||||
</Popconfirm>
|
||||
<Popconfirm
|
||||
v-else
|
||||
title="确认启用该 API Key?"
|
||||
@confirm="handleEnable(row)"
|
||||
>
|
||||
<Button type="link" size="small">启用</Button>
|
||||
</Popconfirm>
|
||||
<Popconfirm title="确认删除该 API Key?" @confirm="handleDelete(row)">
|
||||
<Button type="link" danger size="small">删除</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
<CreateApiKeyModal ref="createModalRef" @success="() => gridApi.reload()" />
|
||||
</Page>
|
||||
</template>
|
||||
Reference in New Issue
Block a user