Skip to content

Commit d2da7e2

Browse files
authored
pref: Improve the API interface process (#7272)
1 parent 873bba9 commit d2da7e2

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

frontend/src/views/setting/panel/api-interface/index.vue

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:close-on-click-modal="false"
77
:close-on-press-escape="false"
88
@close="handleClose"
9-
size="35%"
9+
size="40%"
1010
>
1111
<template #header>
1212
<DrawerHeader :header="$t('setting.apiInterface')" :back="handleClose" />
@@ -89,6 +89,7 @@ import i18n from '@/lang';
8989
import { MsgSuccess } from '@/utils/message';
9090
import { ElMessageBox, FormInstance } from 'element-plus';
9191
import DrawerHeader from '@/components/drawer-header/index.vue';
92+
import { checkCidr, checkIp } from '@/utils/util';
9293
9394
const loading = ref();
9495
const drawerVisible = ref();
@@ -105,7 +106,7 @@ const form = reactive({
105106
});
106107
107108
const rules = reactive({
108-
ipWhiteList: [Rules.requiredInput],
109+
ipWhiteList: [Rules.requiredInput, { validator: checkIPs, trigger: 'blur' }],
109110
apiKey: [Rules.requiredInput],
110111
});
111112
@@ -114,7 +115,28 @@ interface DialogProps {
114115
apiKey: string;
115116
ipWhiteList: string;
116117
}
118+
119+
function checkIPs(rule: any, value: any, callback: any) {
120+
if (form.ipWhiteList !== '') {
121+
let addr = form.ipWhiteList.split('\n');
122+
for (const item of addr) {
123+
if (item === '') {
124+
continue;
125+
}
126+
if (item.indexOf('/') !== -1) {
127+
if (checkCidr(item)) {
128+
return callback(new Error(i18n.global.t('firewall.addressFormatError')));
129+
}
130+
} else if (checkIp(item)) {
131+
return callback(new Error(i18n.global.t('firewall.addressFormatError')));
132+
}
133+
}
134+
}
135+
callback();
136+
}
137+
117138
const emit = defineEmits<{ (e: 'search'): void }>();
139+
118140
const acceptParams = async (params: DialogProps): Promise<void> => {
119141
form.apiInterfaceStatus = params.apiInterfaceStatus;
120142
form.apiKey = params.apiKey;
@@ -128,12 +150,12 @@ const acceptParams = async (params: DialogProps): Promise<void> => {
128150
};
129151
130152
const resetApiKey = async () => {
131-
loading.value = true;
132153
ElMessageBox.confirm(i18n.global.t('setting.apiKeyResetHelper'), i18n.global.t('setting.apiKeyReset'), {
133154
confirmButtonText: i18n.global.t('commons.button.confirm'),
134155
cancelButtonText: i18n.global.t('commons.button.cancel'),
135156
})
136157
.then(async () => {
158+
loading.value = true;
137159
await generateApiKey()
138160
.then((res) => {
139161
loading.value = false;

frontend/src/views/setting/panel/index.vue

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@
141141
inactive-value="disable"
142142
/>
143143
<span class="input-help">{{ $t('setting.apiInterfaceHelper') }}</span>
144+
<div v-if="form.apiInterfaceStatus === 'enable'">
145+
<div>
146+
<el-button link type="primary" @click="onChangeApiInterfaceStatus">
147+
{{ $t('commons.button.view') }}
148+
</el-button>
149+
</div>
150+
</div>
144151
</el-form-item>
145152

146153
<el-form-item :label="$t('setting.developerMode')" prop="developerMode">
@@ -382,13 +389,21 @@ const onChangeProxy = () => {
382389
});
383390
};
384391
385-
const onChangeApiInterfaceStatus = () => {
392+
const onChangeApiInterfaceStatus = async () => {
386393
if (form.apiInterfaceStatus === 'enable') {
387-
apiInterfaceRef.value.acceptParams({
388-
apiInterfaceStatus: form.apiInterfaceStatus,
389-
apiKey: form.apiKey,
390-
ipWhiteList: form.ipWhiteList,
391-
});
394+
loading.value = true;
395+
await updateSetting({ key: 'ApiInterfaceStatus', value: form.apiInterfaceStatus })
396+
.then(() => {
397+
loading.value = false;
398+
apiInterfaceRef.value.acceptParams({
399+
apiInterfaceStatus: form.apiInterfaceStatus,
400+
apiKey: form.apiKey,
401+
ipWhiteList: form.ipWhiteList,
402+
});
403+
})
404+
.catch(() => {
405+
loading.value = false;
406+
});
392407
return;
393408
}
394409
ElMessageBox.confirm(i18n.t('setting.apiInterfaceClose'), i18n.t('setting.apiInterface'), {
@@ -397,6 +412,7 @@ const onChangeApiInterfaceStatus = () => {
397412
})
398413
.then(async () => {
399414
loading.value = true;
415+
form.apiInterfaceStatus = 'disable';
400416
await updateSetting({ key: 'ApiInterfaceStatus', value: 'disable' })
401417
.then(() => {
402418
loading.value = false;
@@ -408,12 +424,7 @@ const onChangeApiInterfaceStatus = () => {
408424
});
409425
})
410426
.catch(() => {
411-
apiInterfaceRef.value.acceptParams({
412-
apiInterfaceStatus: 'enable',
413-
apiKey: form.apiKey,
414-
ipWhiteList: form.ipWhiteList,
415-
});
416-
return;
427+
form.apiInterfaceStatus = 'enable';
417428
});
418429
};
419430
const onChangeNetwork = () => {

0 commit comments

Comments
 (0)