Skip to content

Commit 78c612c

Browse files
committed
feat: 2023主题适配新登录模式
1 parent d35c84c commit 78c612c

27 files changed

+59
-39
lines changed

apps/admin/dependencies.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from core.settings import settings
1212
from apps.admin.services import FileService, ConfigService, LocalFileService
1313

14+
1415
def create_token(data: dict, expires_in: int = 3600 * 24) -> str:
1516
"""
1617
创建JWT token
@@ -22,16 +23,17 @@ def create_token(data: dict, expires_in: int = 3600 * 24) -> str:
2223
**data,
2324
"exp": int(time.time()) + expires_in
2425
}).encode()).decode()
25-
26+
2627
signature = hmac.new(
2728
settings.admin_token.encode(),
2829
f"{header}.{payload}".encode(),
2930
'sha256'
3031
).digest()
3132
signature = base64.b64encode(signature).decode()
32-
33+
3334
return f"{header}.{payload}.{signature}"
3435

36+
3537
def verify_token(token: str) -> dict:
3638
"""
3739
验证JWT token
@@ -40,29 +42,30 @@ def verify_token(token: str) -> dict:
4042
"""
4143
try:
4244
header_b64, payload_b64, signature_b64 = token.split('.')
43-
45+
4446
# 验证签名
4547
expected_signature = hmac.new(
4648
settings.admin_token.encode(),
4749
f"{header_b64}.{payload_b64}".encode(),
4850
'sha256'
4951
).digest()
5052
expected_signature_b64 = base64.b64encode(expected_signature).decode()
51-
53+
5254
if signature_b64 != expected_signature_b64:
5355
raise ValueError("无效的签名")
54-
56+
5557
# 解码payload
5658
payload = json.loads(base64.b64decode(payload_b64))
57-
59+
5860
# 检查是否过期
5961
if payload.get("exp", 0) < time.time():
6062
raise ValueError("token已过期")
61-
63+
6264
return payload
6365
except Exception as e:
6466
raise ValueError(f"token验证失败: {str(e)}")
6567

68+
6669
async def admin_required(authorization: str = Header(default=None), request: Request = None):
6770
"""
6871
验证管理员权限
@@ -71,9 +74,12 @@ async def admin_required(authorization: str = Header(default=None), request: Req
7174
if not authorization or not authorization.startswith("Bearer "):
7275
is_admin = False
7376
else:
74-
token = authorization.split(" ")[1]
75-
payload = verify_token(token)
76-
is_admin = payload.get("is_admin", False)
77+
try:
78+
token = authorization.split(" ")[1]
79+
payload = verify_token(token)
80+
is_admin = payload.get("is_admin", False)
81+
except ValueError as e:
82+
is_admin = False
7783

7884
if request.url.path.startswith('/share/'):
7985
if not settings.openUpload and not is_admin:
@@ -85,6 +91,7 @@ async def admin_required(authorization: str = Header(default=None), request: Req
8591
except ValueError as e:
8692
raise HTTPException(status_code=401, detail=str(e))
8793

94+
8895
async def get_file_service():
8996
return FileService()
9097

apps/admin/services.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ async def update_config(self, data: dict):
9090
class LocalFileService:
9191
async def list_files(self):
9292
files = []
93+
if not os.path.exists(data_root / 'local'):
94+
os.makedirs(data_root / 'local')
9395
for file in os.listdir(data_root / 'local'):
9496
files.append(LocalFileClass(file))
9597
return files

core/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
'onedrive_password': '',
3636
'onedrive_root_path': 'filebox_storage',
3737
'onedrive_proxy': 0,
38+
'webdav_hostname': '',
39+
'webdav_username': '',
40+
'webdav_password': '',
41+
'webdav_root_path': 'filebox_storage',
42+
'webdav_proxy': 0,
3843
'admin_token': 'FileCodeBox2023',
3944
'openUpload': 1,
4045
'uploadSize': 1024 * 1024 * 10,

fcb-fronted/public/logo_small.png

-41.8 KB
Binary file not shown.

fcb-fronted/src/stores/adminData.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {ref} from "vue";
33

44
export const useAdminData = defineStore('adminData', () => {
55
const adminPassword = ref(localStorage.getItem('adminPassword') || '');
6+
const isAdmin = ref(!!localStorage.getItem('adminPassword'));
67
function updateAdminPwd(pwd: string) {
78
adminPassword.value = pwd;
9+
isAdmin.value = true;
810
localStorage.setItem('adminPassword', pwd);
911
}
10-
return { adminPassword,updateAdminPwd };
12+
return { adminPassword,updateAdminPwd,isAdmin };
1113
});

fcb-fronted/src/utils/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const instance = axios.create({
88
instance.interceptors.request.use(
99
(config: any) => {
1010
config.headers= {
11-
'Authorization': localStorage.getItem('adminPassword') || '',
11+
'Authorization': 'Bearer '+ localStorage.getItem('adminPassword') || '',
1212
}
1313
return config;
1414
});
@@ -24,6 +24,7 @@ instance.interceptors.response.use(
2424
return Promise.reject(response.data);
2525
}
2626
}, (error:any) => {
27+
localStorage.clear()
2728
ElMessage.error(error.response.data.detail);
2829
return Promise.reject(error);
2930
});

fcb-fronted/src/views/Admin/AdminView.vue

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
2-
<el-container v-if="isLogin" style="height: 100vh;width: 100vw;position: relative;user-select: none">
2+
<el-container v-if="adminData.isAdmin" style="height: 100vh;width: 100vw;position: relative;user-select: none">
33
<el-header>
44
<el-menu mode="horizontal" router :default-active="route.path">
55
<el-menu-item v-for="menu in menus" :index="menu.path" :key="menu.path">{{menu.name}}</el-menu-item>
66
<el-menu-item style="float: right" @click="toggleDark(!isDark)">{{ t('admin.menu.color') }}</el-menu-item>
7-
<el-menu-item style="float: right" @click="adminData.updateAdminPwd('');isLogin=false">{{ t('admin.menu.signout') }}</el-menu-item>
7+
<el-menu-item style="float: right" @click="logout()">{{ t('admin.menu.signout') }}</el-menu-item>
88
</el-menu>
99
</el-header>
1010
<el-main>
@@ -29,7 +29,6 @@
2929
import { useDark, useToggle } from '@vueuse/core';
3030
import { ref } from "vue";
3131
const isDark = useDark()
32-
const isLogin = ref(false);
3332
const toggleDark = useToggle(isDark)
3433
import { useRoute } from 'vue-router';
3534
import { useAdminData } from "@/stores/adminData";
@@ -71,19 +70,23 @@ const refreshLoginStatus = () => {
7170
request({
7271
url: '/admin/login',
7372
method: 'post',
73+
data: {
74+
password: adminData.adminPassword,
75+
},
7476
}).then((res: any) => {
7577
if (res.code === 200) {
76-
isLogin.value = true;
7778
adminData.updateAdminPwd(res.detail.token);
7879
ElMessage.success(t('admin.login.loginSuccess'));
7980
} else {
81+
localStorage.clear();
8082
ElMessage.error(t('admin.login.loginError'));
8183
}
8284
});
8385
};
84-
if (adminData.adminPassword !== '') {
85-
refreshLoginStatus();
86-
}
86+
const logout = () => {
87+
localStorage.clear();
88+
};
89+
8790
</script>
8891
<style lang="scss" scoped>
8992
</style>

themes/2023/assets/AboutView-BKOAIqPG.js renamed to themes/2023/assets/AboutView-C7hzxtgM.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

themes/2023/assets/AdminView-B-o88PWR.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)