This commit is contained in:
aaron 2025-03-27 17:25:51 +08:00
parent e7829d642f
commit 7eef4942d8
4 changed files with 16 additions and 4 deletions

View File

@ -258,6 +258,9 @@ async def get_url_link(
if not activity:
return error_response(code=404, message="活动不存在")
if activity.qr_code:
return success_response(data=activity.qr_code)
# 获取URL链接
wechat_client = WeChatClient()
image_data = await wechat_client.get_wx_code(path=f"pages/my/promation/activities/index", query=f"id={activity_id}")
@ -266,6 +269,9 @@ async def get_url_link(
url = await qcloud_manager.upload_file_bytes(image_data, key)
activity.qr_code = url
db.commit()
return success_response(data=url)

View File

@ -447,18 +447,20 @@ class WeChatClient:
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()}"
url =f"https://api.weixin.qq.com/wxa/getwxacode?access_token={await self.get_access_token()}"
print(f"path: {path}")
print(f"scene: {query}") # 添加调试日志
params = {
"page": path,
"scene": query,
"path": path + "?" + query,
"env_version": "release" if not settings.DEBUG else "develop"
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, data=json.dumps(params)) as response:
# 使用json参数而不是data参数
async with session.post(url, json=params) as response:
if response.status != 200:
result = await response.json()
print(f"获取小程序码失败: {result}") # 添加错误日志
raise Exception(f"获取小程序码失败: {result.get('errmsg')}")
image_data = await response.read()

View File

@ -19,6 +19,7 @@ class CouponActivityDB(Base):
user_limit = Column(Integer, nullable=False, default=1) # 用户可领取次数
is_active = Column(Boolean, nullable=False, default=True) # 是否激活
coupon_config = Column(JSON, nullable=False) # 可领取的优惠券配置 {coupon_id: count}
qr_code = Column(String(200), nullable=True) # 活动小程序码URL
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
@ -34,6 +35,7 @@ class CouponActivityCreate(BaseModel):
user_limit: int = Field(..., gt=0)
coupon_config: Dict[int, int] # {coupon_id: count}
is_active: bool = Field(default=True)
qr_code: Optional[str] = Field(None, max_length=200) # 活动小程序码URL
class CouponActivityUpdate(BaseModel):
name: Optional[str] = Field(None, max_length=100)
@ -46,6 +48,7 @@ class CouponActivityUpdate(BaseModel):
user_limit: Optional[int] = Field(None, gt=0)
coupon_config: Optional[Dict[int, int]] = None
is_active: Optional[bool] = None
qr_code: Optional[str] = Field(None, max_length=200) # 活动小程序码URL
class CouponActivityInfo(BaseModel):
id: int
@ -59,6 +62,7 @@ class CouponActivityInfo(BaseModel):
user_limit: int
coupon_config: Dict[int, int]
is_active: bool
qr_code: Optional[str] # 活动小程序码URL
create_time: datetime
update_time: Optional[datetime]

Binary file not shown.