# -*- coding: utf-8 -*- from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import uvicorn from pydantic import BaseModel from typing import List import json import threading import asyncio from datetime import datetime from base64 import b64decode from io import BytesIO import pystray from PIL import Image from image import favicon_ico # 图标,将图标已base64格式存入py文件 from pathlib import os # 防止杀毒软件查杀 app = FastAPI() # // 配置允许域名列表、允许方法、请求头、cookie等 app.add_middleware( CORSMiddleware, allow_origins=["*"], # 表示允许任何源 allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) #打包命令 pyinstaller -F -w -i .\favicon.ico .\xxxx.py -p .\image.py class Item(BaseModel): data_dict: list @app.post('/express_bill_params') async def express_bill_params(items: List): temp_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(temp_time) print('items:', items) def run_main(): uvicorn.run(app, host="0.0.0.0", port=6993) def withdraw_window(): def on_quit_clicked(icon): # 自定义回调函数 icon.stop() # 对象停止方法 pid = os.getpid() #获取当前进程ID try:#杀掉后台进程 os.system('taskkill -f -pid %s' % pid) except: pass image = Image.open(BytesIO(b64decode(favicon_ico))) # 打开 ICO 图像文件并创建一个 Image 对象 menu = (pystray.MenuItem(text='退出', action=on_quit_clicked),) # 创建菜单项元组 icon = pystray.Icon("name", image, "插件", menu) # 创建 PyStray Icon 对象,并传入关键参数 # 显示图标 threading.Thread(target=icon.run, daemon=True).start() def run(): threading.Thread(target=withdraw_window, daemon=True).start() run_main() if __name__ == "__main__": run()