增加新的使用方法,加快访问速度,合并相似的功能
This commit is contained in:
@@ -31,7 +31,7 @@ def ReJson(code: int, body: [dict, list] = None, msg: str = None, error: str = N
|
||||
}
|
||||
rjson = situation.get(code, {'code': 9999, 'body': None, 'msg': "code错误", "extra": {}})
|
||||
if code != 0:
|
||||
logging.warning((code, rjson['body'], msg if msg else None))
|
||||
logging.warning(f"\n{code} \n{rjson['body']}\n{msg if msg else None}")
|
||||
if body:
|
||||
rjson['body'] = body
|
||||
if msg:
|
||||
|
||||
+54
-12
@@ -9,29 +9,68 @@ import base64
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import traceback
|
||||
from .rjson import ReJson
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def read_session(session_file, arg):
|
||||
with open(session_file, 'r') as f:
|
||||
session = json.load(f)
|
||||
return session.get(arg, "")
|
||||
|
||||
|
||||
def save_session(session_file, arg, value):
|
||||
def read_session(session_file, wxid, arg):
|
||||
try:
|
||||
with open(session_file, 'r') as f:
|
||||
session = json.load(f)
|
||||
except:
|
||||
except FileNotFoundError:
|
||||
logging.error(f"Session file not found: {session_file}")
|
||||
return None
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error(f"Error decoding JSON file: {e}")
|
||||
return None
|
||||
return session.get(wxid, {}).get(arg, None)
|
||||
|
||||
|
||||
def get_session_wxids(session_file):
|
||||
try:
|
||||
with open(session_file, 'r') as f:
|
||||
session = json.load(f)
|
||||
except FileNotFoundError:
|
||||
logging.error(f"Session file not found: {session_file}")
|
||||
return None
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error(f"Error decoding JSON file: {e}")
|
||||
return None
|
||||
return list(session.keys())
|
||||
|
||||
|
||||
def save_session(session_file, wxid, arg, value):
|
||||
try:
|
||||
with open(session_file, 'r') as f:
|
||||
session = json.load(f)
|
||||
except FileNotFoundError:
|
||||
session = {}
|
||||
session[arg] = value
|
||||
with open(session_file, 'w') as f:
|
||||
json.dump(session, f, indent=4)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error(f"Error decoding JSON file: {e}")
|
||||
return False
|
||||
if wxid not in session:
|
||||
session[wxid] = {}
|
||||
session[wxid][arg] = value
|
||||
try:
|
||||
with open(session_file, 'w') as f:
|
||||
json.dump(session, f, indent=4)
|
||||
except Exception as e:
|
||||
logging.error(f"Error writing to file: {e}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def validate_title(title):
|
||||
"""
|
||||
校验文件名是否合法
|
||||
"""
|
||||
rstr = r"[\/\\\:\*\?\"\<\>\|\.]" # '/ \ : * ? " < > |'
|
||||
new_title = re.sub(rstr, "_", title) # 替换为下划线
|
||||
return new_title
|
||||
|
||||
|
||||
def error9999(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
@@ -40,9 +79,12 @@ def error9999(func):
|
||||
except Exception as e:
|
||||
traceback_data = traceback.format_exc()
|
||||
rdata = f"{traceback_data}"
|
||||
# logging.error(rdata)
|
||||
return ReJson(9999, body=rdata)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def gen_base64(path):
|
||||
# 获取文件名后缀
|
||||
extension = os.path.splitext(path)[1]
|
||||
@@ -61,4 +103,4 @@ def gen_base64(path):
|
||||
js_code = file.read()
|
||||
|
||||
base64_encoded_js = base64.b64encode(js_code).decode('utf-8')
|
||||
return start_str + base64_encoded_js
|
||||
return start_str + base64_encoded_js
|
||||
|
||||
Reference in New Issue
Block a user