mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49: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>
|
||||
Reference in New Issue
Block a user