# -*- coding: utf-8 -*- """公告与站点设置。 (自 auth_system.py 拆出,行为与原实现一致;DB 访问已迁移为原生异步。) """ import time from typing import Any from .db import ( _q, _x, ) # ============ 公告 ============ async def get_announcement() -> dict[str, Any]: rows = await _q('SELECT * FROM announcements WHERE id = 1') return dict(rows[0]) if rows else {'title': '', 'content': '', 'popup_mode': 'off'} async def save_announcement(title: str, content: str, popup_mode: str) -> None: if popup_mode not in ('off', 'every', 'once'): popup_mode = 'off' # 版本号精确到毫秒,同秒多次保存也能区分(once 已读判断依赖该字段) updated = time.strftime('%Y-%m-%d %H:%M:%S') + f'.{int(time.time() * 1000) % 1000:03d}' await _x('UPDATE announcements SET title = ?, content = ?, popup_mode = ?, updated_at = ? WHERE id = 1', (title, content, popup_mode, updated)) # ============ 设置(键值) ============ async def get_setting(key: str, default: str = '') -> str: rows = await _q('SELECT value FROM settings WHERE key = ?', (key,)) return rows[0]['value'] if rows else default async def set_setting(key: str, value: str) -> None: await _x('INSERT INTO settings (key, value) VALUES (?, ?) ' 'ON CONFLICT(key) DO UPDATE SET value = excluded.value', (key, str(value)))