Skip to content

Commit 4691114

Browse files
committed
Refactor axios.js and Vue components to conditionally handle namespace settings based on server provider. Update request headers in axios.js to include site key and namespace password only for specific providers. Enhance NamespaceAccess and NamespaceSettingsCard components to show/hide based on provider, and streamline password management functionality.
1 parent 31cff8a commit 4691114

File tree

5 files changed

+153
-24
lines changed

5 files changed

+153
-24
lines changed

src/axios/axios.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ const axiosInstance = axios.create({
1313
// 请求拦截器
1414
axiosInstance.interceptors.request.use(
1515
(requestConfig) => {
16-
// 确保每次请求时都获取最新的 siteKey
17-
const siteKey = getSetting("server.siteKey");
18-
if (siteKey) {
19-
requestConfig.headers["x-site-key"] = Base64.encode(siteKey);
20-
}
16+
const provider = getSetting("server.provider");
17+
18+
// 只有在 kv-server 或 classworkscloud 模式下才添加请求头
19+
if (provider === "kv-server" || provider === "classworkscloud") {
20+
// 确保每次请求时都获取最新的 siteKey
21+
const siteKey = getSetting("server.siteKey");
22+
if (siteKey) {
23+
requestConfig.headers["x-site-key"] = Base64.encode(siteKey);
24+
}
2125

22-
// 自动添加命名空间密码
23-
const namespacePassword = getSetting("namespace.password");
24-
if (namespacePassword) {
25-
requestConfig.headers["x-namespace-password"] =
26-
Base64.encode(namespacePassword);
26+
// 自动添加命名空间密码
27+
const namespacePassword = getSetting("namespace.password");
28+
if (namespacePassword) {
29+
requestConfig.headers["x-namespace-password"] = Base64.encode(namespacePassword);
30+
}
2731
}
2832

2933
return requestConfig;

src/components/NamespaceAccess.vue

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="namespace-access">
2+
<div class="namespace-access" v-if="shouldShowAccess">
33
<!-- 只读状态显示 -->
44
<v-chip
55
v-if="isReadOnly"
@@ -13,7 +13,7 @@
1313
<v-btn
1414
v-if="isReadOnly"
1515
color="primary"
16-
size="small"
16+
class="rounded-xl"
1717
prepend-icon="mdi-lock-open-variant"
1818
@click="openPasswordDialog"
1919
:disabled="loading"
@@ -23,7 +23,7 @@
2323

2424
<!-- 密码输入对话框 -->
2525
<v-dialog v-model="dialog" max-width="400" persistent>
26-
<v-card>
26+
<v-card class="rounded-xl" border hover>
2727
<v-card-title class="text-h6">输入访问密码</v-card-title>
2828
<v-card-text>
2929
<div v-if="passwordHint" class="text-body-2 mb-4">
@@ -37,7 +37,6 @@
3737
:error="!!error"
3838
:error-messages="error"
3939
@keyup.enter="checkPassword"
40-
:append-inner-icon="showPassword ? 'mdi-eye-off' : 'mdi-eye'"
4140
@click:append-inner="showPassword = !showPassword"
4241
:disabled="loading"
4342
autofocus
@@ -85,11 +84,23 @@ export default {
8584
passwordHint: null, // 密码提示
8685
};
8786
},
87+
computed: {
88+
shouldShowAccess() {
89+
const provider = getSetting("server.provider");
90+
return provider === "kv-server" || provider === "classworkscloud";
91+
}
92+
},
8893
async created() {
89-
await this.checkAccess();
94+
if (this.shouldShowAccess) {
95+
await this.checkAccess();
96+
}
9097
},
9198
methods: {
9299
async checkAccess() {
100+
if (!this.shouldShowAccess) {
101+
return;
102+
}
103+
93104
try {
94105
// 获取命名空间访问类型
95106
const response = await axios.get(
@@ -105,7 +116,6 @@ export default {
105116
// 保存密码提示
106117
this.passwordHint = response.data.passwordHint || null;
107118
} else {
108-
//this.$router.push("/settings");
109119
return;
110120
}
111121

src/components/settings/cards/NamespaceSettingsCard.vue

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<template>
22
<settings-card
3+
v-if="shouldShowCard"
34
title="命名空间设置"
45
icon="mdi-database-lock"
56
:loading="loading"
67
>
8+
<namespace-access ref="namespaceAccess" />
79
<!-- 命名空间标识符 -->
810
<v-card variant="tonal" class="rounded-lg mb-4">
911
<v-card-item>
@@ -178,12 +180,23 @@
178180
variant="tonal"
179181
:loading="hintLoading"
180182
@click="openHintDialog"
183+
class="mr-2"
181184
>
182185
设置密码提示
183186
<template #prepend>
184187
<v-icon icon="mdi-lightbulb-outline" />
185188
</template>
186189
</v-btn>
190+
<v-btn
191+
color="primary"
192+
variant="tonal"
193+
@click="modifyLocalPassword"
194+
>
195+
修改本地密码
196+
<template #prepend>
197+
<v-icon icon="mdi-key-variant" />
198+
</template>
199+
</v-btn>
187200
</div>
188201

189202
<v-btn
@@ -352,6 +365,7 @@
352365

353366
<script>
354367
import SettingsCard from "@/components/SettingsCard.vue";
368+
import NamespaceAccess from "@/components/NamespaceAccess.vue";
355369
import { kvServerProvider } from "@/utils/providers/kvServerProvider";
356370
import { getSetting } from "@/utils/settings";
357371
import axios from "@/axios/axios";
@@ -374,7 +388,10 @@ const getHeaders = () => {
374388
375389
export default {
376390
name: "NamespaceSettingsCard",
377-
components: { SettingsCard },
391+
components: {
392+
SettingsCard,
393+
NamespaceAccess
394+
},
378395
379396
data() {
380397
return {
@@ -438,6 +455,10 @@ export default {
438455
},
439456
440457
computed: {
458+
shouldShowCard() {
459+
const provider = getSetting("server.provider");
460+
return provider === "kv-server" || provider === "classworkscloud";
461+
},
441462
deviceUuid() {
442463
return this.namespaceInfo.uuid;
443464
},
@@ -460,8 +481,10 @@ export default {
460481
},
461482
462483
async created() {
463-
await this.loadNamespaceInfo();
464-
await this.loadPasswordHint();
484+
if (this.shouldShowCard) {
485+
await this.loadNamespaceInfo();
486+
await this.loadPasswordHint();
487+
}
465488
},
466489
467490
methods: {
@@ -693,6 +716,14 @@ export default {
693716
this.snackbarText = message;
694717
this.showSnackbar = true;
695718
},
719+
720+
modifyLocalPassword() {
721+
// 获取NamespaceAccess组件实例并调用方法
722+
const namespaceAccess = this.$refs.namespaceAccess;
723+
if (namespaceAccess) {
724+
namespaceAccess.openPasswordDialog();
725+
}
726+
},
696727
},
697728
};
698729
</script>

src/pages/settings.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ export default {
254254
return { isMobile: mobile };
255255
},
256256
data() {
257+
const provider = getSetting("server.provider");
258+
const showNamespaceSettings = provider === "kv-server" || provider === "classworkscloud";
259+
257260
const settings = {
258261
server: {
259262
domain: getSetting("server.domain"),
@@ -337,11 +340,13 @@ export default {
337340
icon: "mdi-server",
338341
value: "server",
339342
},
340-
{
341-
title: "命名空间",
342-
icon: "mdi-database-lock",
343-
value: "namespace",
344-
},
343+
...(showNamespaceSettings ? [
344+
{
345+
title: "命名空间",
346+
icon: "mdi-database-lock",
347+
value: "namespace",
348+
}
349+
] : []),
345350
{
346351
title: "分享设置",
347352
icon: "mdi-share",

src/utils/api.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import axios from "@/axios/axios";
2+
import { getSetting } from "@/utils/settings";
3+
4+
// Helper function to check if provider is valid for API calls
5+
const isValidProvider = () => {
6+
const provider = getSetting("server.provider");
7+
return provider === "kv-server" || provider === "classworkscloud";
8+
};
9+
10+
// Helper function to get request headers with site key and namespace password
11+
const getHeaders = () => {
12+
const headers = { Accept: "application/json" };
13+
const siteKey = getSetting("server.siteKey");
14+
const namespacePassword = getSetting("namespace.password");
15+
16+
if (siteKey) {
17+
headers["x-site-key"] = siteKey;
18+
}
19+
if (namespacePassword) {
20+
headers["x-namespace-password"] = namespacePassword;
21+
}
22+
23+
return headers;
24+
};
25+
26+
/**
27+
* Get namespace info from the server
28+
* @returns {Promise<Object>} Response data containing namespace info
29+
*/
30+
export const getNamespaceInfo = async () => {
31+
if (!isValidProvider()) {
32+
throw new Error("当前数据提供者不支持此操作");
33+
}
34+
35+
const serverUrl = getSetting("server.domain");
36+
const machineId = getSetting("device.uuid");
37+
38+
try {
39+
const response = await axios.get(`${serverUrl}/${machineId}/_info`, {
40+
headers: getHeaders(),
41+
});
42+
43+
return response.data;
44+
} catch (error) {
45+
throw new Error(error.response?.data?.message || "获取命名空间信息失败");
46+
}
47+
};
48+
49+
/**
50+
* Update namespace password
51+
* @param {string} oldPassword - Current password (if exists)
52+
* @param {string} newPassword - New password to set
53+
* @returns {Promise<Object>} Response data
54+
*/
55+
export const updateNamespacePassword = async (oldPassword, newPassword) => {
56+
if (!isValidProvider()) {
57+
throw new Error("当前数据提供者不支持此操作");
58+
}
59+
60+
const serverUrl = getSetting("server.domain");
61+
const machineId = getSetting("device.uuid");
62+
63+
try {
64+
const response = await axios.put(
65+
`${serverUrl}/${machineId}/_infopassword`,
66+
{
67+
oldPassword,
68+
newPassword,
69+
},
70+
{
71+
headers: getHeaders(),
72+
}
73+
);
74+
75+
return response.data;
76+
} catch (error) {
77+
throw new Error(error.response?.data?.message || "更新命名空间密码失败");
78+
}
79+
};

0 commit comments

Comments
 (0)