This commit is contained in:
aaron 2025-03-18 19:34:25 +08:00
parent f155324e2c
commit 15f63a12a2
5 changed files with 67 additions and 0 deletions

View File

@ -30,6 +30,8 @@ import logging
from fastapi import Request from fastapi import Request
from app.core.wecombot import WecomBot from app.core.wecombot import WecomBot
from fastapi import BackgroundTasks from fastapi import BackgroundTasks
from app.core.wechat import WeChatClient
from app.core.qcloud import qcloud_manager
router = APIRouter() router = APIRouter()
@ -65,6 +67,30 @@ async def send_verify_code(request: VerifyCodeRequest):
return success_response(message="验证码已发送") 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) @router.get("/info", response_model=ResponseModel)
async def get_user_info( async def get_user_info(
db: Session = Depends(get_db), db: Session = Depends(get_db),

View File

@ -177,6 +177,25 @@ class QCloudManager:
except TencentCloudSDKException as e: except TencentCloudSDKException as e:
raise Exception(f"身份证实名认证失败: {str(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: async def upload_file(self, file: UploadFile, folder: str = None) -> str:
""" """

View File

@ -446,6 +446,25 @@ class WeChatClient:
print(f"申请退款异常: {str(e)}") print(f"申请退款异常: {str(e)}")
raise Exception(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: async def get_url_link(self, path: str, query: str) -> str:

View File

@ -53,6 +53,8 @@ class UserDB(Base):
community_id = Column(Integer, ForeignKey("communities.id"), nullable=True) # 归属小区 community_id = Column(Integer, ForeignKey("communities.id"), nullable=True) # 归属小区
is_auth = Column(Boolean, nullable=False, default=False) is_auth = Column(Boolean, nullable=False, default=False)
is_delivering = 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")) community = relationship("CommunityDB", foreign_keys=[community_id], backref=backref("residents", lazy="dynamic"))
@ -89,6 +91,7 @@ class UserInfo(BaseModel):
community_name: Optional[str] = None community_name: Optional[str] = None
is_auth: bool = False is_auth: bool = False
is_delivering: bool = False is_delivering: bool = False
qr_code_url: Optional[str] = None
class Config: class Config:
from_attributes = True from_attributes = True

Binary file not shown.