From 5968b75c0d3cf5fff65861f86901479aaa3ea351 Mon Sep 17 00:00:00 2001 From: hjdhnx Date: Tue, 6 Sep 2022 01:44:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 27 ++++++++++++++++++++- js/version.txt | 2 +- templates/admin.html | 45 +++++++++++++++++++++++++++++++++- utils/update.py | 58 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 127 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 21dc423..c9788d8 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,7 @@ from werkzeug.utils import secure_filename from js.rules import getRuleLists from utils import error,parser from utils.web import * +from utils.update import checkUpdate,getOnlineVer,getLocalVer,download_new_version import sys import codecs from classes.cms import CMS,logger @@ -115,7 +116,7 @@ def admin_home(): # 管理员界面 if not verfy_token(token): return render_template('login.html') # return jsonify(error.success('登录成功')) - return render_template('admin.html',rules=getRules('js')) + return render_template('admin.html',rules=getRules('js'),ver=getLocalVer()) @app.route('/api/login',methods=['GET','POST']) def login_api(): @@ -160,6 +161,30 @@ def admin_clear_rule(name): os.remove(file_path) return jsonify(error.success('成功删除文件:'+file_path)) +@app.route('/admin/get_ver') +def admin_get_ver(): + cookies = request.cookies + # print(cookies) + token = cookies.get('token', '') + # print(f'mytoken:{token}') + if not verfy_token(token): + # return render_template('login.html') + return jsonify(error.failed('请登录后再试')) + + return jsonify({'local_ver':getLocalVer(),'online_ver':getOnlineVer()}) + +@app.route('/admin/update_ver') +def admin_update_ver(): + cookies = request.cookies + # print(cookies) + token = cookies.get('token', '') + # print(f'mytoken:{token}') + if not verfy_token(token): + # return render_template('login.html') + return jsonify(error.failed('请登录后再试')) + msg = download_new_version() + return jsonify(error.success(msg)) + @app.route('/upload', methods=['GET', 'POST']) def upload_file(): cookies = request.cookies diff --git a/js/version.txt b/js/version.txt index 711ee4f..6ebad14 100644 --- a/js/version.txt +++ b/js/version.txt @@ -1 +1 @@ -3.1.3 \ No newline at end of file +3.1.2 \ No newline at end of file diff --git a/templates/admin.html b/templates/admin.html index 14bc4d1..bc4b8ad 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -63,6 +63,17 @@ /*li a {*/ /* display: block !important;*/ /*}*/ + .ver_title{ + font-size: 15px;margin-left: 20px + } + + .ver{ + font-size: 16px;margin-left: 5px;color: #ea7d7d; + } + + a.funcbtn{ + margin-left: 10px; + } -

欢迎使用drpy管理员界面

返回首页

+

欢迎使用drpy管理员界面当前版本:{{ ver }}

+

+返回首页 +检测升级 +

你可以在此页面在线上传规则文件到js目录或者删除js目录的文件

diff --git a/utils/update.py b/utils/update.py index d3149cb..b4af08f 100644 --- a/utils/update.py +++ b/utils/update.py @@ -6,6 +6,7 @@ import requests import os +import zipfile def getLocalVer(): base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录 @@ -22,8 +23,61 @@ def getLocalVer(): def getOnlineVer(): ver = '1.0.1' try: - r = requests.get('',timeout=(2,2)) + r = requests.get('https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/version.txt',timeout=(2,2)) ver = r.text except Exception as e: print(f'{e}') - return ver \ No newline at end of file + return ver + +def checkUpdate(): + local_ver = getLocalVer() + online_ver = getOnlineVer() + if local_ver != online_ver: + return True + return False + + +def del_file(filepath): + """ + 删除execl目录下的所有文件或文件夹 + :param filepath: 路径 + :return: + """ + del_list = os.listdir(filepath) + for f in del_list: + file_path = os.path.join(filepath, f) + if os.path.isfile(file_path): + os.remove(file_path) + +def download_new_version(): + base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录 + tmp_path = os.path.join(base_path, f'tmp') + os.makedirs(tmp_path,exist_ok=True) + url = 'https://gitcode.net/qq_32394351/dr_py/-/archive/master/dr_py-master.zip' + # tmp_files = os.listdir(tmp_path) + # for tp in tmp_files: + # print(f'清除缓存文件:{tp}') + # os.remove(os.path.join(tmp_path, tp)) + del_file(tmp_path) + headers = { + 'Referer': 'https://gitcode.net/', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36', + } + msg = '' + try: + print(f'开始下载:{url}') + r = requests.get(url,headers=headers,timeout=(20,20)) + rb = r.content + download_path = os.path.join(tmp_path, 'dr_py.zip') + with open(download_path,mode='wb+') as f: + f.write(rb) + print(f'开始解压文件:{download_path}') + f = zipfile.ZipFile(download_path, 'r') # 压缩文件位置 + for file in f.namelist(): + f.extract(file, tmp_path) # 解压位置 + f.close() + print('解压完毕') + msg = '下载成功' + except Exception as e: + msg = f'下载失败:{e}' + return msg \ No newline at end of file -- GitLab