From 1bdd3dbc19789ce7464e2ceb9ddfc73fc4973557 Mon Sep 17 00:00:00 2001 From: hackrobot Date: Sun, 21 Jul 2024 14:31:44 +0800 Subject: [PATCH] update --- app/main.py | 64 ++++++++++++++++++++++++++++++++++++---------- app/models.py | 2 ++ app/schemas.py | 2 ++ app/tentcentSMS.py | 4 +-- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/app/main.py b/app/main.py index 7a1db17..3fe7d53 100644 --- a/app/main.py +++ b/app/main.py @@ -15,6 +15,8 @@ from fastapi.staticfiles import StaticFiles # from app import tgMessage # type: ignore #发送tg消息 from datetime import datetime import requests +from tentcentSMS import sendSms +import random # 初始化数据库 models.Base.metadata.create_all(bind=engine) @@ -50,9 +52,10 @@ def get_db(): # def root(): # return {"message": "Hello World"} + def sengTgmesage(message): url = "http://192.168.31.38:8521/send_message" - data = {"message":message} + data = {"message": message} headers = {"Content-Type": "application/json"} response = requests.post(url, json=data, headers=headers) try: @@ -61,13 +64,13 @@ def sengTgmesage(message): except requests.exceptions.JSONDecodeError: print("Response content is not in JSON format") print("Response content:", response.text) - - + + @app.get("/hello") def hello(): # response = f"你好,\n..........................\n一名开发者\n副业:跨境电商\n#公众号:异度世界\n..........................\n回复关键词,了解更多:\n1. 微信机器人\n2. 数字游民\n3. 加群\n..........................\n[该消息是接受好友自动回复]" response = f"你好,\n..........................\n一名开发者\n副业:跨境电商\n#公众号:异度世界\n..........................\n回复关键词,了解更多:\n1. 数字游民\n..........................\n[该消息是接受好友自动回复]" - + return PlainTextResponse(content=response) @@ -98,7 +101,7 @@ def tgMessagefun(openid: Optional[str] = None, db: Session = Depends(get_db)): message = f'openid: {openid}-未关联,在{current_time}访问网页' print(message) sengTgmesage(message) - + # tgMessage.sendTg(message) @@ -137,7 +140,6 @@ def generateqr(url="http://nomad.hackrobot.cn", # url="http://192.168.31.219:10086" if wxid: url += f"?wxid={wxid}" - # 创建二维码 qr = qrcode.QRCode(version=1, box_size=5, border=2) # 调整二维码大小 @@ -204,7 +206,7 @@ def add_user(nickname: Optional[Any] = None, response = f"{nickname}你好,\n..........................\n一名开发者\n副业:跨境电商\n#公众号:异度世界\n..........................\n回复关键词,了解更多:\n1. 数字游民\n..........................\n[该消息是接受好友自动回复]" # 关键词回复 - if keyword == '加群' or keyword =="数字游民" or keyword =="3" or keyword =="1": + if keyword == '加群' or keyword == "数字游民" or keyword == "3" or keyword == "1": print("keyword==加群") combined_image_path = generateqr(url="http://nomad.hackrobot.cn", wxid=wxid, @@ -220,15 +222,17 @@ def add_user(nickname: Optional[Any] = None, elif keyword == '活动': print("keyword==活动") response = f"活动正在筹备中" - combined_image_path = generateqr(url="http://nomad.hackrobot.cn", - wxid=wxid, - group_image_path="images/meetupqr.jpg") + combined_image_path = generateqr( + url="http://nomad.hackrobot.cn", + wxid=wxid, + group_image_path="images/meetupqr.jpg") return FileResponse(combined_image_path) elif keyword == 'qrcode': print("keyword==qrcode") - combined_image_path = generateqr(url="http://nomad.hackrobot.cn", - # wxid='qrcode', - group_image_path="images/nomadqr.jpg") + combined_image_path = generateqr( + url="http://nomad.hackrobot.cn", + # wxid='qrcode', + group_image_path="images/nomadqr.jpg") return FileResponse(combined_image_path) else: print('其他keyword') @@ -276,3 +280,37 @@ def update_user(identifier: str, } +# 获取手机号=>生成验证码 +@app.post("/get_verification_code/") +def get_verification_code(wxid: str, + phone_number: str, + db: Session = Depends(get_db)): + verification_code = '{:04d}'.format(random.randint(0, 9999)) + user = db.query(models.User).filter(models.User.wxid == wxid).first() + if not user: + raise HTTPException(status_code=404, detail="User not found") + + user.phone_number = phone_number + user.verification_code = verification_code + db.commit() + + return { + "message": "Verification code sent", + "verification_code": verification_code + } + + +# 验证码校验 +@app.post("/verify_code/") +def verify_code(phone_number: str, code: str, db: Session = Depends(get_db)): + user = db.query( + models.User).filter(models.User.phone_number == phone_number).first() + + if not user: + raise HTTPException(status_code=404, detail="User not found") + + if user.verification_code == code: + return {"message": "Verification successful"} + else: + raise HTTPException(status_code=400, + detail="Invalid verification code") diff --git a/app/models.py b/app/models.py index 4b278c3..0ab7936 100644 --- a/app/models.py +++ b/app/models.py @@ -20,6 +20,8 @@ class User(Base): ebook = Column(Boolean, default=False) # 新增电子书字段 activity_status = Column(String, default="0") # 新增活动报名字段 vip_member = Column(Boolean, default=False) # 新增VIP会员字段 + verification_code = Column(String, index=True) # 新增验证码字段 + phone_number = Column(String, index=True, unique=True) # 新增手机号字段 def sengTgmesage(message): diff --git a/app/schemas.py b/app/schemas.py index d24d4b6..f724299 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -12,6 +12,8 @@ class UserBase(BaseModel): ebook: Optional[bool] = None # 新增电子书字段 activity_status: Optional[str] = None # 新增活动报名字段 vip_member: Optional[bool] = None # 新增VIP会员字段 + verification_code: Optional[bool] = None #新增验证码字段 + class UserCreate(UserBase): pass diff --git a/app/tentcentSMS.py b/app/tentcentSMS.py index 614ea58..b09d205 100644 --- a/app/tentcentSMS.py +++ b/app/tentcentSMS.py @@ -16,7 +16,7 @@ import random # phoneNum="+8613243889782" -def sendSms(phoneNum): +def sendSms(phoneNum,verification_code=None): try: # 必要步骤: # 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。 @@ -77,7 +77,7 @@ def sendSms(phoneNum): # 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空 # 随机4位数字 - verification_code = '{:04d}'.format(random.randint(0, 9999)) + # verification_code = '{:04d}'.format(random.randint(0, 9999)) req.TemplateParamSet = [verification_code] # 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] # 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号