pull/632/head
Mr Chen 6 months ago
parent f1a74e6275
commit 020c8da426
  1. 1
      web/admin/setting/__init__.py
  2. 85
      web/admin/setting/notify_tgbot.py
  3. 4
      web/admin/setup/option.py
  4. 29
      web/core/mw.py
  5. 27
      web/static/app/config.js
  6. 4
      web/templates/default/setting.html
  7. 1
      web/utils/config.py

@ -13,6 +13,7 @@ from .temp_login import *
from .timezone import * from .timezone import *
from .secondary_verifiy import * from .secondary_verifiy import *
from .notify_email import * from .notify_email import *
from .notify_tgbot import *

@ -0,0 +1,85 @@
# coding:utf-8
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
import re
import json
import os
import time
from flask import Blueprint, render_template
from flask import request
from admin import session
from admin.user_login_check import panel_login_required
import core.mw as mw
import utils.config as utils_config
from .setting import blueprint
import thisdb
# 获取邮件信息
@blueprint.route('/get_notify_tgbot', endpoint='get_notify_tgbot', methods=['POST'])
@panel_login_required
def get_notify_tgbot():
notify_tgbot = thisdb.getOptionByJson('notify_tgbot', default={'open':False}, type='notify')
if 'cfg' in notify_tgbot:
decrypt_data = mw.deDoubleCrypt('tgbot', notify_tgbot['cfg'])
notify_tgbot['tgbot'] = json.loads(decrypt_data)
else:
notify_tgbot['tgbot'] = []
return mw.returnData(True,'ok',notify_tgbot)
# 设置邮件信息
@blueprint.route('/set_notify_tgbot', endpoint='set_notify_tgbot', methods=['POST'])
@panel_login_required
def set_notify_tgbot():
data = request.form.get('data', '').strip()
crypt_data = mw.enDoubleCrypt('tgbot', data)
notify_tgbot = thisdb.getOptionByJson('notify_tgbot', default={'open':False}, type='notify')
notify_tgbot['cfg'] = crypt_data
thisdb.setOption('notify_tgbot', json.dumps(notify_tgbot), type='notify')
return mw.returnData(True,'设置成功')
# 设置邮件测试
@blueprint.route('/set_notify_tgbot_test', endpoint='set_notify_tgbot_test', methods=['POST'])
@panel_login_required
def set_notify_tgbot_test():
tag_data = request.form.get('data', '').strip()
tmp = json.loads(tag_data)
test_pass = mw.tgbotNotifyTest(tmp['app_token'], tmp['chat_id'])
if test_pass == True:
return mw.returnData(True, '验证成功')
return mw.returnData(False, '验证失败:'+test_pass)
# 切换邮件开关
@blueprint.route('/set_notify_tgbot_enable', endpoint='set_notify_tgbot_enable', methods=['POST'])
@panel_login_required
def set_notify_tgbot_enable():
tag = request.form.get('tag', '').strip()
data = request.form.get('data', '').strip()
notify_tgbot = thisdb.getOptionByJson('notify_tgbot', default={'open':False}, type='notify')
if notify_tgbot['open']:
op_action = '关闭'
notify_tgbot['open'] = False
else:
op_action = '开启'
notify_tgbot['open'] = True
thisdb.setOption('notify_tgbot', json.dumps(notify_tgbot), type='notify')
return mw.returnData(True, op_action+'成功')

@ -59,7 +59,9 @@ def init_option():
thisdb.setOption('site_path', mw.getFatherDir()+'/wwwroot') thisdb.setOption('site_path', mw.getFatherDir()+'/wwwroot')
# 邮件通知 # 异步邮件通知
thisdb.setOption('notify_email', json.dumps({'open':False}), type='notify') thisdb.setOption('notify_email', json.dumps({'open':False}), type='notify')
# 异步Telegram Bot 通知
thisdb.setOption('notify_tgbot', json.dumps({'open':False}), type='notify')
return True return True

@ -721,6 +721,34 @@ def writeLog(stype, msg, args=()):
# writeFileLog(getTracebackInfo()) # writeFileLog(getTracebackInfo())
return writeDbLog(stype, msg, args, uid) return writeDbLog(stype, msg, args, uid)
def writeFileLog(msg, path=None, limit_size=50 * 1024 * 1024, save_limit=3):
log_file = getPanelDir() + '/logs/debug.log'
if path != None:
log_file = path
if os.path.exists(log_file):
size = os.path.getsize(log_file)
if size > limit_size:
log_file_rename = log_file + "_" + \
time.strftime("%Y-%m-%d_%H%M%S") + '.log'
os.rename(log_file, log_file_rename)
logs = sorted(glob.glob(log_file + "_*"))
count = len(logs)
save_limit = count - save_limit
for i in range(count):
if i > save_limit:
break
os.remove(logs[i])
# print('|---多余日志[' + logs[i] + ']已删除!')
f = open(log_file, 'ab+')
msg += "\n"
if __name__ == '__main__':
print(msg)
f.write(msg.encode('utf-8'))
f.close()
return True
def writeDbLog(stype, msg, args=(), uid=1): def writeDbLog(stype, msg, args=(), uid=1):
try: try:
import thisdb import thisdb
@ -1220,6 +1248,7 @@ def tgbotNotifyHttpPost(app_token, chat_id, msg):
return True return True
except Exception as e: except Exception as e:
writeFileLog(str(e)) writeFileLog(str(e))
return str(e)
return False return False

@ -540,10 +540,19 @@ function setPanelSSL(){
}); });
} }
function setNotifyApi(tag, obj){ function setNotifyTgbot(obj){
var enable = $(obj).prop("checked"); var enable = $(obj).prop("checked");
// console.log(tag,obj,enable); $.post('/setting/set_notify_tgbot_enable', {'enable':enable},function(rdata){
$.post('/setting/set_notify_email_enable', {'tag':tag, 'enable':enable},function(rdata){ showMsg(rdata.msg, function(){
if (rdata.status){}
} ,{icon:rdata.status?1:2}, 1000);
},'json');
}
function setNotifyEmail(obj){
var enable = $(obj).prop("checked");
$.post('/setting/set_notify_email_enable', {'enable':enable},function(rdata){
showMsg(rdata.msg, function(){ showMsg(rdata.msg, function(){
if (rdata.status){} if (rdata.status){}
} ,{icon:rdata.status?1:2}, 1000); } ,{icon:rdata.status?1:2}, 1000);
@ -552,16 +561,16 @@ function setNotifyApi(tag, obj){
function getTgbot(){ function getTgbot(){
var loadT = layer.msg('正在获取TgBot信息...',{icon:16,time:0,shade: [0.3, '#000']}); var loadT = layer.msg('正在获取TgBot信息...',{icon:16,time:0,shade: [0.3, '#000']});
$.post('/setting/get_notify',{},function(data){ $.post('/setting/get_notify_tgbot',{},function(data){
layer.close(loadT); layer.close(loadT);
var app_token = ''; var app_token = '';
var chat_id = ''; var chat_id = '';
if (data.status){ if (data.status){
if (typeof(data['data']['tgbot']) !='undefined'){ if (data['data']['tgbot'].length != 0){
app_token = data['data']['tgbot']['data']['app_token']; app_token = data['data']['tgbot']['app_token'];
chat_id = data['data']['tgbot']['data']['chat_id']; chat_id = data['data']['tgbot']['chat_id'];
} }
} }
@ -598,7 +607,7 @@ function getTgbot(){
return false; return false;
} }
$.post('/setting/set_notify',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){ $.post('/setting/set_notify_tgbot',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){
showMsg(rdata.msg, function(){ showMsg(rdata.msg, function(){
if (rdata.status){ if (rdata.status){
layer.close(index); layer.close(index);
@ -622,7 +631,7 @@ function getTgbot(){
return false; return false;
} }
$.post('/setting/set_notify_test',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){ $.post('/setting/set_notify_tgbot_test',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){
showMsg(rdata.msg, function(){ showMsg(rdata.msg, function(){
if (rdata.status){ if (rdata.status){
layer.close(index); layer.close(index);

@ -164,7 +164,7 @@
<p class="mtb15"> <p class="mtb15">
<span class="set-tit text-right" title="TelegramBot" style="float: left;">TG机器人通知</span> <span class="set-tit text-right" title="TelegramBot" style="float: left;">TG机器人通知</span>
<input id="cfg_tgbot" name="cfg_tgbot" onclick="setNotifyApi('tgbot',this)" class="btswitch btswitch-ios" type="checkbox" {{data['notify_tgbot_enable']}}> <input id="cfg_tgbot" name="cfg_tgbot" onclick="setNotifyTgbot(this)" class="btswitch btswitch-ios" type="checkbox" {% if data['notify_tgbot']['open'] %}checked{% endif %}>
<label class="btswitch-btn ml5" for="cfg_tgbot" style="float: left;margin-top:4px;"></label> <label class="btswitch-btn ml5" for="cfg_tgbot" style="float: left;margin-top:4px;"></label>
<button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getTgbot();">TelegramBot配置</button> <button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getTgbot();">TelegramBot配置</button>
<span class="set-info c7">Telegram Bot机器人通知【<b style="color:red;">国内可能无法使用</b></span> <span class="set-info c7">Telegram Bot机器人通知【<b style="color:red;">国内可能无法使用</b></span>
@ -172,7 +172,7 @@
<p class="mtb15"> <p class="mtb15">
<span class="set-tit text-right" title="绑定账号" style="float: left;">邮件通知</span> <span class="set-tit text-right" title="绑定账号" style="float: left;">邮件通知</span>
<input id="cfg_email" name="cfg_email" onclick="setNotifyApi('email',this)" class="btswitch btswitch-ios" type="checkbox" {% if data['notify_email']['open'] %}checked{% endif %}> <input id="cfg_email" name="cfg_email" onclick="setNotifyEmail(this)" class="btswitch btswitch-ios" type="checkbox" {% if data['notify_email']['open'] %}checked{% endif %}>
<label class="btswitch-btn ml5" for="cfg_email" style="float: left;margin-top:4px;"></label> <label class="btswitch-btn ml5" for="cfg_email" style="float: left;margin-top:4px;"></label>
<button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getEmailCfg();">邮件配置</button> <button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getEmailCfg();">邮件配置</button>
<span class="set-info c7">邮件通知</span> <span class="set-info c7">邮件通知</span>

@ -82,5 +82,6 @@ def getGlobalVar():
# 邮件通知设置 # 邮件通知设置
data['notify_email'] = thisdb.getOptionByJson('notify_email', default={'open':False}, type='notify') data['notify_email'] = thisdb.getOptionByJson('notify_email', default={'open':False}, type='notify')
data['notify_tgbot'] = thisdb.getOptionByJson('notify_tgbot', default={'open':False}, type='notify')
return data return data
Loading…
Cancel
Save