From ba81bea1990eee355b73489392e5bf8b519a14ab Mon Sep 17 00:00:00 2001 From: lan Date: Fri, 23 Dec 2022 12:32:18 +0800 Subject: [PATCH] =?UTF-8?q?add:=E4=B8=8A=E4=BC=A0=E5=89=8D=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 7 +++--- templates/index.html | 56 +++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 14e4c42..546bd8a 100644 --- a/main.py +++ b/main.py @@ -41,8 +41,8 @@ index_html = open('templates/index.html', 'r', encoding='utf-8').read() \ .replace('{{title}}', settings.TITLE) \ .replace('{{description}}', settings.DESCRIPTION) \ .replace('{{keywords}}', settings.KEYWORDS) \ - .replace("'{{fileSizeLimit}}'", str(settings.FILE_SIZE_LIMIT)) -# 管理页面 + .replace("'{{fileSizeLimit}}'", str(settings.FILE_SIZE_LIMIT)) \ + # 管理页面 admin_html = open('templates/admin.html', 'r', encoding='utf-8').read() \ .replace('{{title}}', settings.TITLE) \ .replace('{{description}}', settings.DESCRIPTION) \ @@ -107,7 +107,7 @@ async def index(): @app.get('/banner') -async def banner(s: AsyncSession = Depends(get_session)): +async def banner(request: Request, s: AsyncSession = Depends(get_session)): # 数据库查询config config = (await s.execute(select(Values).filter(Values.key == 'config'))).scalar_one_or_none() # 如果存在config,就返回config的value @@ -119,6 +119,7 @@ async def banner(s: AsyncSession = Depends(get_session)): # 如果不存在config,就返回默认的banner return { 'detail': 'banner', + 'enable': request.headers.get('pwd', '') == settings.ADMIN_PASSWORD, 'data': [{ 'text': 'FileCodeBox', 'url': 'https://github.com/vastsa/FileCodeBox', diff --git a/templates/index.html b/templates/index.html index b39ae8f..1e2bcd4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -271,6 +271,7 @@ fileSizeLimit: '{{fileSizeLimit}}', pwd: localStorage.getItem('pwd') || '', banners: [], + enableUpload: false, uploadData: { style: '2', type: '1', @@ -313,7 +314,12 @@ } }); // 获取Banner - axios.get('/banner').then(res => { + axios.get('/banner', { + headers: { + pwd: localStorage.getItem('pwd') + } + }).then(res => { + this.enableUpload = res.data.enable this.banners = res.data.data }) }, @@ -385,6 +391,22 @@ changeInput: function (value) { this.code = value; }, + checkFile: function (file) { + if (!this.enable) { + this.$message({ + message: '上传功能已关闭', + type: 'error' + }); + return false + } + if (file.size > this.fileSizeLimit) { + this.$message({ + message: `文件大小不能超过${this.fileSizeLimit/1024/1024}MB`, + type: 'error' + }); + return false + } + }, toUploadData: function () { if (this.uploadData.text === '' && this.uploadData.file === null) { this.$message({ @@ -392,17 +414,19 @@ type: 'error' }); } else { - this.http('post', '/share', this.uploadData, { - headers: { - 'Content-Type': 'multipart/form-data', - 'pwd': this.pwd - } - }).then(res => { - this.jiFiles.unshift(res.data); - this.jiDrawer = true; - this.uploadData.text = ''; - this.uploadData.file = null; - }); + if (this.checkFile(this.uploadData.file)) { + this.http('post', '/share', this.uploadData, { + headers: { + 'Content-Type': 'multipart/form-data', + 'pwd': this.pwd + } + }).then(res => { + this.jiFiles.unshift(res.data); + this.jiDrawer = true; + this.uploadData.text = ''; + this.uploadData.file = null; + }); + } } }, successUpload(response, file, fileList) { @@ -420,13 +444,7 @@ }); }, beforeUpload: function (file) { - if (file.size > this.fileSizeLimit) { - this.$message({ - message: `文件大小不能超过${this.fileSizeLimit/1024/1024}MB`, - type: 'error' - }); - return false - } + return this.checkFile(file); } } })