From 15f63a12a2bd1b9a09b2350376a1615546049f09 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Tue, 18 Mar 2025 19:34:25 +0800 Subject: [PATCH] update --- app/api/endpoints/user.py | 26 ++++++++++++++++++++++++++ app/core/qcloud.py | 19 +++++++++++++++++++ app/core/wechat.py | 19 +++++++++++++++++++ app/models/user.py | 3 +++ jobs.sqlite | Bin 24576 -> 24576 bytes 5 files changed, 67 insertions(+) diff --git a/app/api/endpoints/user.py b/app/api/endpoints/user.py index 4f70349..6b54acc 100644 --- a/app/api/endpoints/user.py +++ b/app/api/endpoints/user.py @@ -30,6 +30,8 @@ import logging from fastapi import Request from app.core.wecombot import WecomBot from fastapi import BackgroundTasks +from app.core.wechat import WeChatClient +from app.core.qcloud import qcloud_manager router = APIRouter() @@ -65,6 +67,30 @@ async def send_verify_code(request: VerifyCodeRequest): return success_response(message="验证码已发送") +@router.get("/qr_code", response_model=ResponseModel) +async def get_qr_code( + db: Session = Depends(get_db), + current_user: UserDB = Depends(get_current_user) +): + if current_user.qr_code_url: + return success_response(message="获取用户二维码成功",data={ + "url": current_user.qr_code_url + }) + + wechat_client = WeChatClient() + image_data = await wechat_client.get_wx_code(path=f"pages/help/index/index", query=f"shared_user_code={current_user.user_code}") + random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + key = f"qr_code/{current_user.user_code}_{random_str}.png" + + url = await qcloud_manager.upload_file_bytes(image_data, key) + current_user.qr_code_url = url + db.commit() + + """获取用户二维码""" + return success_response(message="获取用户二维码成功",data={ + "url": current_user.qr_code_url + }) + @router.get("/info", response_model=ResponseModel) async def get_user_info( db: Session = Depends(get_db), diff --git a/app/core/qcloud.py b/app/core/qcloud.py index 627b703..b56e3d5 100644 --- a/app/core/qcloud.py +++ b/app/core/qcloud.py @@ -177,6 +177,25 @@ class QCloudManager: except TencentCloudSDKException as e: raise Exception(f"身份证实名认证失败: {str(e)}") + + async def upload_file_bytes(self, file_bytes: bytes, key: str) -> str: + """ + 上传文件字节流到 COS + """ + try: + self._init_cos_client() + + # 上传文件 + self.cos_client.put_object( + Bucket=settings.COS_BUCKET, + Body=file_bytes, + Key=key, + ) + + return f"https://{settings.COS_BASE_URL}/{key}" + + except Exception as e: + raise Exception(f"文件上传失败: {str(e)}") async def upload_file(self, file: UploadFile, folder: str = None) -> str: """ diff --git a/app/core/wechat.py b/app/core/wechat.py index 8d38244..7ccc087 100644 --- a/app/core/wechat.py +++ b/app/core/wechat.py @@ -446,6 +446,25 @@ class WeChatClient: print(f"申请退款异常: {str(e)}") raise Exception(f"申请退款失败: {str(e)}") + async def get_wx_code(self, path: str, query: str = None) -> bytes: + url =f"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={await self.get_access_token()}" + print(f"path: {path}") + params = { + "page": path, + "scene": query + } + try: + async with aiohttp.ClientSession() as session: + async with session.post(url, data=json.dumps(params)) as response: + if response.status != 200: + result = await response.json() + raise Exception(f"获取小程序码失败: {result.get('errmsg')}") + + image_data = await response.read() + return image_data + except Exception as e: + print(f"获取小程序码失败: {str(e)}") + raise Exception(f"获取小程序码失败: {str(e)}") async def get_url_link(self, path: str, query: str) -> str: diff --git a/app/models/user.py b/app/models/user.py index 3ffc902..4ff71f0 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -53,6 +53,8 @@ class UserDB(Base): community_id = Column(Integer, ForeignKey("communities.id"), nullable=True) # 归属小区 is_auth = Column(Boolean, nullable=False, default=False) is_delivering = Column(Boolean, nullable=False, default=False) # 是否在配送中 + + qr_code_url = Column(String(200), nullable=True) # 二维码URL地址 # 用户所属小区关系 community = relationship("CommunityDB", foreign_keys=[community_id], backref=backref("residents", lazy="dynamic")) @@ -89,6 +91,7 @@ class UserInfo(BaseModel): community_name: Optional[str] = None is_auth: bool = False is_delivering: bool = False + qr_code_url: Optional[str] = None class Config: from_attributes = True diff --git a/jobs.sqlite b/jobs.sqlite index c6fdf1194acf6b98044da934056ca4239486631e..7278e9ea4386647518bb2488a2cca1407bf2cb90 100644 GIT binary patch delta 21 bcmZoTz}Rqrae@>ROYlS)Cm^{oVL?0qO)UoA delta 21 bcmZoTz}Rqrae@>R^Y4i=PC#;F!h(1JSDOed