This commit is contained in:
hackrobot
2024-07-18 10:23:18 +08:00
parent d7c4cf1b5f
commit 73b7c3104d
2 changed files with 38 additions and 0 deletions

View File

@@ -1,6 +1,11 @@
from sqlalchemy import Boolean, Column, Integer, String
from app.database import Base
import requests
from sqlalchemy.orm import Session
from sqlalchemy import event
from sqlalchemy.orm.attributes import get_history
class User(Base):
__tablename__ = "users"
@@ -15,3 +20,36 @@ class User(Base):
ebook = Column(Boolean, default=False) # 新增电子书字段
activity_status = Column(String, default="0") # 新增活动报名字段
vip_member = Column(Boolean, default=False) # 新增VIP会员字段
def sengTgmesage(message):
url = "http://192.168.31.38:8521/send_message"
data = {"message":message}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=data, headers=headers)
try:
response_data = response.json()
print(response_data)
except requests.exceptions.JSONDecodeError:
print("Response content is not in JSON format")
print("Response content:", response.text)
def handle_change(mapper, connection, target):
"""监听器函数"""
if target.__class__.__name__ == 'User': # 确保只监听User类的变动
changed_fields = []
for attr in target.__mapper__.column_attrs:
history = get_history(target, attr.key)
if history.has_changes():
changed_fields.append(attr.key)
if changed_fields:
print(f"检测到用户 {target.id} 的字段变化: {changed_fields}")
msg=f"检测到用户 {target.id} 的字段变化: {changed_fields}"
sengTgmesage(msg)
# 发起HTTP请求
# requests.post("http://your-fastapi-endpoint/notify_change", json={"user_id": target.id, "changed_fields": changed_fields})
event.listen(User, "after_update", handle_change)