提交 5968b75c 编写于 作者: H hjdhnx

增加了检测升级

上级 bd0d96eb
......@@ -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
......
3.1.3
\ No newline at end of file
3.1.2
\ No newline at end of file
......@@ -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;
}
</style>
<body>
<script>
......@@ -79,6 +90,34 @@
location.href = '/vod?rule='+rule;
});
$("#checkUpdate").click(function(){
console.log('开始检查升级...');
$.get("/admin/get_ver",function(data,status){
console.log("数据: " + data + "\n状态: " + status);
if(data.msg){
alert(data.msg);
return false
}else{
if(data.local_ver && data.online_ver){
if(data.local_ver !== data.online_ver){
msg = `本地版本:${data.local_ver}\n线上版本:${data.online_ver}\n是否立即执行升级?`;
if(confirm(msg)){
$.get("/admin/update_ver",function(data,status){
console.log(data);
alert(data.msg||'未知数据,具体到数据控制台查看');
return false
});
}
}else{
alert('已经是最新版,无需升级!')
}
}else{
alert('已经是最新版,无需升级!')
}
}
});
});
$(".clear").click(function(){
// location.reload();
// let rule = this.innerText.trim();
......@@ -138,7 +177,11 @@
}
</script>
<h2 class="title">欢迎使用drpy管理员界面</h2><h4><a href="/index">返回首页</a></h4>
<h2 class="title">欢迎使用drpy管理员界面<span class="ver_title">当前版本:</span><span class="ver">{{ ver }}</span></h2>
<h4>
<a href="/index" class="funcbtn">返回首页</a>
<a href="javascript:void(0);" class="funcbtn" id="checkUpdate">检测升级</a>
</h4>
<p>你可以在此页面在线上传规则文件到js目录或者删除js目录的文件</p>
<form action = "/upload" method = "POST" enctype = "multipart/form-data">
<!-- <input type = "file" name = "file" class="btn" accept=".js" onchange="getFileSize(this)"/>-->
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册