add keys
This commit is contained in:
@@ -82,10 +82,16 @@ export const keysApi = {
|
|||||||
error: string;
|
error: string;
|
||||||
}[]
|
}[]
|
||||||
> {
|
> {
|
||||||
const res = await http.post("/keys/test-multiple", {
|
const res = await http.post(
|
||||||
|
"/keys/test-multiple",
|
||||||
|
{
|
||||||
group_id,
|
group_id,
|
||||||
keys_text,
|
keys_text,
|
||||||
});
|
},
|
||||||
|
{
|
||||||
|
hideMessage: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
return res.data;
|
return res.data;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -97,6 +103,14 @@ export const keysApi = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 测试密钥
|
||||||
|
restoreKeys(group_id: number, keys_text: string): Promise<null> {
|
||||||
|
return http.post("/keys/restore-multiple", {
|
||||||
|
group_id,
|
||||||
|
keys_text,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// 恢复所有无效密钥
|
// 恢复所有无效密钥
|
||||||
restoreAllInvalidKeys(group_id: number): Promise<void> {
|
restoreAllInvalidKeys(group_id: number): Promise<void> {
|
||||||
return http.post("/keys/restore-all-invalid", { group_id });
|
return http.post("/keys/restore-all-invalid", { group_id });
|
||||||
|
@@ -75,7 +75,7 @@ function handleGroupEdited(newGroup: Group) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleDelete() {
|
async function handleDelete() {
|
||||||
if (!props.group) {
|
if (!props.group) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
125
web/src/components/keys/KeyCreateDialog.vue
Normal file
125
web/src/components/keys/KeyCreateDialog.vue
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { keysApi } from "@/api/keys";
|
||||||
|
import { Close } from "@vicons/ionicons5";
|
||||||
|
import { NButton, NCard, NInput, NModal } from "naive-ui";
|
||||||
|
import { ref, watch } from "vue";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
show: boolean;
|
||||||
|
groupId: number;
|
||||||
|
groupName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Emits {
|
||||||
|
(e: "update:show", value: boolean): void;
|
||||||
|
(e: "success"): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps<Props>();
|
||||||
|
|
||||||
|
const emit = defineEmits<Emits>();
|
||||||
|
|
||||||
|
const loading = ref(false);
|
||||||
|
const keysText = ref("");
|
||||||
|
|
||||||
|
// 监听弹窗显示状态
|
||||||
|
watch(
|
||||||
|
() => props.show,
|
||||||
|
show => {
|
||||||
|
if (show) {
|
||||||
|
resetForm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 重置表单
|
||||||
|
function resetForm() {
|
||||||
|
keysText.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭弹窗
|
||||||
|
function handleClose() {
|
||||||
|
emit("update:show", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提交表单
|
||||||
|
async function handleSubmit() {
|
||||||
|
if (loading.value || !keysText.value.trim()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
loading.value = true;
|
||||||
|
|
||||||
|
await keysApi.addMultipleKeys(props.groupId, keysText.value);
|
||||||
|
|
||||||
|
emit("success");
|
||||||
|
handleClose();
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<n-modal :show="show" @update:show="handleClose" class="form-modal">
|
||||||
|
<n-card
|
||||||
|
style="width: 800px"
|
||||||
|
:title="`为 ${groupName || '当前分组'} 添加密钥`"
|
||||||
|
:bordered="false"
|
||||||
|
size="huge"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
>
|
||||||
|
<template #header-extra>
|
||||||
|
<n-button quaternary circle @click="handleClose">
|
||||||
|
<template #icon>
|
||||||
|
<n-icon :component="Close" />
|
||||||
|
</template>
|
||||||
|
</n-button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<n-input
|
||||||
|
v-model:value="keysText"
|
||||||
|
type="textarea"
|
||||||
|
placeholder="输入密钥,每行一个"
|
||||||
|
:rows="8"
|
||||||
|
style="margin-top: 20px"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<template #footer>
|
||||||
|
<div style="display: flex; justify-content: flex-end; gap: 12px">
|
||||||
|
<n-button @click="handleClose">取消</n-button>
|
||||||
|
<n-button type="primary" @click="handleSubmit" :loading="loading" :disabled="!keysText">
|
||||||
|
创建
|
||||||
|
</n-button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</n-card>
|
||||||
|
</n-modal>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.form-modal {
|
||||||
|
--n-color: rgba(255, 255, 255, 0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.n-input) {
|
||||||
|
--n-border-radius: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.n-card-header) {
|
||||||
|
border-bottom: 1px solid rgba(239, 239, 245, 0.8);
|
||||||
|
padding: 10px 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.n-card__content) {
|
||||||
|
max-height: calc(100vh - 68px - 61px - 50px);
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.n-card__footer) {
|
||||||
|
border-top: 1px solid rgba(239, 239, 245, 0.8);
|
||||||
|
padding: 10px 15px;
|
||||||
|
}
|
||||||
|
</style>
|
@@ -1,6 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { keysApi } from "@/api/keys";
|
import { keysApi } from "@/api/keys";
|
||||||
import type { APIKey, Group, KeyStatus } from "@/types/models";
|
import type { APIKey, Group, KeyStatus } from "@/types/models";
|
||||||
|
import { getGroupDisplayName } from "@/utils/display";
|
||||||
import {
|
import {
|
||||||
AddCircleOutline,
|
AddCircleOutline,
|
||||||
CopyOutline,
|
CopyOutline,
|
||||||
@@ -8,8 +9,19 @@ import {
|
|||||||
EyeOutline,
|
EyeOutline,
|
||||||
RemoveCircleOutline,
|
RemoveCircleOutline,
|
||||||
} from "@vicons/ionicons5";
|
} from "@vicons/ionicons5";
|
||||||
import { NButton, NDropdown, NEmpty, NIcon, NInput, NSelect, NSpace, NSpin } from "naive-ui";
|
import {
|
||||||
|
NButton,
|
||||||
|
NDropdown,
|
||||||
|
NEmpty,
|
||||||
|
NIcon,
|
||||||
|
NInput,
|
||||||
|
NSelect,
|
||||||
|
NSpace,
|
||||||
|
NSpin,
|
||||||
|
useDialog,
|
||||||
|
} from "naive-ui";
|
||||||
import { ref, watch } from "vue";
|
import { ref, watch } from "vue";
|
||||||
|
import KeyCreateDialog from "./KeyCreateDialog.vue";
|
||||||
|
|
||||||
interface KeyRow extends APIKey {
|
interface KeyRow extends APIKey {
|
||||||
is_visible: boolean;
|
is_visible: boolean;
|
||||||
@@ -29,6 +41,7 @@ const currentPage = ref(1);
|
|||||||
const pageSize = ref(9);
|
const pageSize = ref(9);
|
||||||
const total = ref(0);
|
const total = ref(0);
|
||||||
const totalPages = ref(0);
|
const totalPages = ref(0);
|
||||||
|
const dialog = useDialog();
|
||||||
|
|
||||||
// 状态过滤选项
|
// 状态过滤选项
|
||||||
const statusOptions = [
|
const statusOptions = [
|
||||||
@@ -51,6 +64,11 @@ const moreOptions = [
|
|||||||
|
|
||||||
// 防抖定时器
|
// 防抖定时器
|
||||||
let searchTimer: number | undefined = undefined;
|
let searchTimer: number | undefined = undefined;
|
||||||
|
let testingMsg: any = null;
|
||||||
|
let restoreMsg: any = null;
|
||||||
|
let deleteMsg: any = null;
|
||||||
|
|
||||||
|
const createDialogShow = ref(false);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => props.selectedGroup,
|
() => props.selectedGroup,
|
||||||
@@ -147,11 +165,11 @@ function copyKey(key: KeyRow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function testKey(_key: KeyRow) {
|
async function testKey(_key: KeyRow) {
|
||||||
if (!props.selectedGroup?.id || !_key.key_value) {
|
if (!props.selectedGroup?.id || !_key.key_value || testingMsg) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadingMsg = window.$message.info("正在测试密钥...", {
|
testingMsg = window.$message.info("正在测试密钥...", {
|
||||||
duration: 0,
|
duration: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -166,7 +184,8 @@ async function testKey(_key: KeyRow) {
|
|||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
console.error("测试失败");
|
console.error("测试失败");
|
||||||
} finally {
|
} finally {
|
||||||
loadingMsg.destroy();
|
testingMsg?.destroy();
|
||||||
|
testingMsg = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,35 +194,59 @@ function toggleKeyVisibility(key: KeyRow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function restoreKey(key: KeyRow) {
|
async function restoreKey(key: KeyRow) {
|
||||||
// eslint-disable-next-line no-alert
|
if (!props.selectedGroup?.id || !key.key_value || restoreMsg) {
|
||||||
const confirmed = window.confirm(`确定要恢复密钥"${maskKey(key.key_value)}"吗?`);
|
|
||||||
if (!confirmed) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dialog.warning({
|
||||||
|
title: "恢复密钥",
|
||||||
|
content: `确定要恢复密钥"${maskKey(key.key_value)}"吗?`,
|
||||||
|
positiveText: "确定",
|
||||||
|
negativeText: "取消",
|
||||||
|
onPositiveClick: async () => {
|
||||||
|
restoreMsg = window.$message.info("正在恢复密钥...", {
|
||||||
|
duration: 0,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await keysApi.toggleKeyStatus(key.id.toString(), 1);
|
await keysApi.restoreKeys(props.selectedGroup.id, key.key_value);
|
||||||
window.$message.success("密钥已恢复");
|
|
||||||
await loadKeys();
|
await loadKeys();
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
window.$message.error("恢复失败");
|
console.error("恢复失败");
|
||||||
|
} finally {
|
||||||
|
restoreMsg?.destroy();
|
||||||
|
restoreMsg = null;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteKey(key: KeyRow) {
|
async function deleteKey(key: KeyRow) {
|
||||||
// eslint-disable-next-line no-alert
|
if (!props.selectedGroup?.id || !key.key_value || deleteMsg) {
|
||||||
const confirmed = window.confirm(`确定要删除密钥"${maskKey(key.key_value)}"吗?`);
|
|
||||||
if (!confirmed) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dialog.warning({
|
||||||
|
title: "删除密钥",
|
||||||
|
content: `确定要删除密钥"${maskKey(key.key_value)}"吗?`,
|
||||||
|
positiveText: "确定",
|
||||||
|
negativeText: "取消",
|
||||||
|
onPositiveClick: async () => {
|
||||||
|
deleteMsg = window.$message.info("正在删除密钥...", {
|
||||||
|
duration: 0,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await keysApi.deleteKeyById(key.id.toString());
|
await keysApi.deleteKeys(props.selectedGroup.id, key.key_value);
|
||||||
window.$message.success("密钥已删除");
|
|
||||||
await loadKeys();
|
await loadKeys();
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
window.$message.error("删除失败");
|
console.error("删除失败");
|
||||||
|
} finally {
|
||||||
|
deleteMsg?.destroy();
|
||||||
|
deleteMsg = null;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatRelativeTime(date: string) {
|
function formatRelativeTime(date: string) {
|
||||||
@@ -234,7 +277,7 @@ function getStatusClass(status: KeyStatus): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addKey() {
|
function addKey() {
|
||||||
window.$message.info("添加密钥功能开发中");
|
createDialogShow.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function copyAllKeys() {
|
async function copyAllKeys() {
|
||||||
@@ -308,12 +351,12 @@ async function restoreAllInvalid() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-alert
|
dialog.warning({
|
||||||
const confirmed = window.confirm("确定要恢复所有无效密钥吗?");
|
// title: "恢复密钥",
|
||||||
if (!confirmed) {
|
content: "确定要恢复所有无效密钥吗?",
|
||||||
return;
|
positiveText: "确定",
|
||||||
}
|
negativeText: "取消",
|
||||||
|
onPositiveClick: async () => {
|
||||||
try {
|
try {
|
||||||
window.$message.success("所有无效密钥已恢复");
|
window.$message.success("所有无效密钥已恢复");
|
||||||
await loadKeys();
|
await loadKeys();
|
||||||
@@ -321,6 +364,8 @@ async function restoreAllInvalid() {
|
|||||||
// 错误已记录
|
// 错误已记录
|
||||||
window.$message.error("恢复失败");
|
window.$message.error("恢复失败");
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateAllKeys() {
|
async function validateAllKeys() {
|
||||||
@@ -535,6 +580,14 @@ function changePageSize(size: number) {
|
|||||||
</n-button>
|
</n-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<key-create-dialog
|
||||||
|
v-if="selectedGroup?.id"
|
||||||
|
v-model:show="createDialogShow"
|
||||||
|
:group-id="selectedGroup.id"
|
||||||
|
:group-name="getGroupDisplayName(selectedGroup!)"
|
||||||
|
@success="loadKeys"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@@ -2,6 +2,12 @@ import { useAuthService } from "@/services/auth";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { appState } from "./app-state";
|
import { appState } from "./app-state";
|
||||||
|
|
||||||
|
declare module "axios" {
|
||||||
|
interface AxiosRequestConfig {
|
||||||
|
hideMessage?: boolean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const http = axios.create({
|
const http = axios.create({
|
||||||
baseURL: "/api",
|
baseURL: "/api",
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
@@ -22,7 +28,7 @@ http.interceptors.request.use(config => {
|
|||||||
http.interceptors.response.use(
|
http.interceptors.response.use(
|
||||||
response => {
|
response => {
|
||||||
appState.loading = false;
|
appState.loading = false;
|
||||||
if (response.config.method !== "get") {
|
if (response.config.method !== "get" && !response.config.hideMessage) {
|
||||||
window.$message.success("操作成功");
|
window.$message.success("操作成功");
|
||||||
}
|
}
|
||||||
return response.data;
|
return response.data;
|
||||||
|
Reference in New Issue
Block a user