项目改造
This commit is contained in:
@@ -72,3 +72,54 @@ async def add_new_jm_code(new_code: int) -> bool:
|
||||
JM_CODES.append(new_code)
|
||||
_safe_write_json(JM_CODES, FILE_PATH)
|
||||
return True
|
||||
|
||||
|
||||
async def list_jm_codes() -> list[str]:
|
||||
"""读取全部 JM 码(Web 管理 / 导出用)。"""
|
||||
await load_codes()
|
||||
return list(JM_CODES)
|
||||
|
||||
|
||||
async def remove_jm_code(code: str) -> bool:
|
||||
"""删除一个 JM 码;不存在返回 False。"""
|
||||
await load_codes()
|
||||
code = str(code).strip()
|
||||
async with LOCK:
|
||||
if code not in JM_CODES:
|
||||
return False
|
||||
JM_CODES.remove(code)
|
||||
_safe_write_json(JM_CODES, FILE_PATH)
|
||||
return True
|
||||
|
||||
|
||||
def _ensure_loaded_sync() -> None:
|
||||
"""同步确保数据已载入(Web 配置读取/写入用,不走异步锁)。"""
|
||||
global JM_CODES, _LOADED
|
||||
if _LOADED:
|
||||
return
|
||||
_migrate_legacy()
|
||||
if not FILE_PATH.exists():
|
||||
JM_CODES = []
|
||||
_safe_write_json(JM_CODES, FILE_PATH)
|
||||
else:
|
||||
try:
|
||||
with open(FILE_PATH, "r", encoding="utf-8") as f:
|
||||
JM_CODES = json.load(f)
|
||||
except (json.JSONDecodeError, OSError):
|
||||
JM_CODES = []
|
||||
_LOADED = True
|
||||
|
||||
|
||||
def sync_get_jm_codes() -> list[str]:
|
||||
"""同步读取全部 JM 码(供通用配置项 getter)。"""
|
||||
_ensure_loaded_sync()
|
||||
return list(JM_CODES)
|
||||
|
||||
|
||||
def sync_set_jm_codes(codes) -> None:
|
||||
"""同步写入全部 JM 码(供通用配置项 setter),并落盘到 jm_code.json。"""
|
||||
global JM_CODES
|
||||
_ensure_loaded_sync()
|
||||
cleaned = [str(c).strip() for c in (codes or []) if str(c).strip()]
|
||||
JM_CODES = cleaned
|
||||
_safe_write_json(JM_CODES, FILE_PATH)
|
||||
|
||||
Reference in New Issue
Block a user