import re import json import asyncio import uvicorn from playwright.async_api import async_playwright from fastapi import FastAPI import aiomysql from pymysql.converters import escape_string app = FastAPI() HOST = '0.0.0.0' PORT = 8000 # store = 20182 # storeIdentifier = 'ECI_METRO' # auth = 'eci' store = 20153 storeIdentifier = 'LEI_SHING_HONG' auth = 'lsh' code_rule = r"^([0-9]{1,2}[A-Z])\-*([0-9]{4})|([0-9]{1,3})\-*([0-9]{4})$" mysql_config = { 'host': 'localhost', 'port': 3306, 'database': 'workdata', 'user': 'root', 'password': 'rxxt', 'charset': 'utf8mb4', } class AsyncMysql: def __init__(self): self.host = mysql_config.get('host') self.port = int(mysql_config.get('port')) self.user = mysql_config.get('user') self.password = mysql_config.get('password') self.database = mysql_config.get('database') self.pool = None async def create_pool(self): """ 创建 MySQL 连接池 """ try: self.pool = await aiomysql.create_pool( host=self.host, port=self.port, user=self.user, password=self.password, db=self.database, autocommit=True, minsize=2, maxsize=12 ) except Exception as e: print(f"Failed to create MySQL connection pool: {str(e)}") async def execute_query(self, query): """ 执行增删改操作 """ try: async with self.pool.acquire() as conn: async with conn.cursor(aiomysql.DictCursor) as cursor: await cursor.execute(query) return True except Exception as e: print(f"Failed to execute query: {query}") print(f"Error: {str(e)}") return False async def execute_select(self, query): """ 执行查询操作 """ try: async with self.pool.acquire() as conn: async with conn.cursor(aiomysql.DictCursor) as cursor: await cursor.execute(query) results = await cursor.fetchall() return results except Exception as e: print(f"Failed to execute select query: {query}") print(f"Error: {str(e)}") return None async def close_pool(self): """ 关闭 MySQL 连接池 """ if self.pool: self.pool.close() await self.pool.wait_closed() print("MySQL connection pool closed.") @app.get('/hi') def _(): return { 'version': 1.5, 'host': HOST, 'port': PORT } def getUniqueId(s: str): p = re.match(r'^.+