feat(auth): 运行环境(生产/开发)+ 小程序码版本(体验版trial/正式版release)可配
- PINEAGENTS_ENV=prod|dev 判定运行环境; /auth/status 回显 env + wxacode_env 以便确认 - PINEAGENTS_WXACODE_ENV=release|trial|develop 决定小程序码指向(默认 prod→release, dev→trial=体验版) - get_wxacode 用该 env_version(支持体验版测试)
This commit is contained in:
@@ -311,6 +311,8 @@ async def auth_status(db: Database = Depends(get_db)):
|
||||
login_modes=modes,
|
||||
wechat_login=config.AUTH_WECHAT_LOGIN,
|
||||
wx_qr=config.AUTH_WECHAT_QR,
|
||||
env=config.APP_ENV,
|
||||
wxacode_env=config.WECHAT_WXACODE_ENV,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -61,6 +61,8 @@ class AuthStatusResponse(BaseModel):
|
||||
login_modes: list[str] = ["password", "phone"] # password/phone/wechat/miniprogram(四种)
|
||||
wechat_login: bool = False # 小程序扫码(code2session)
|
||||
wx_qr: bool = False # 微信开放平台扫码(OAuth)
|
||||
env: str = "prod" # prod | dev —— 运行环境(前端/联调可据此确认)
|
||||
wxacode_env: str = "release" # release | trial | develop —— 小程序码指向的版本(体验版=trial)
|
||||
|
||||
class VerifyResponse(BaseModel):
|
||||
valid: bool
|
||||
|
||||
@@ -146,6 +146,19 @@ AUTH_WECHAT_QR = os.environ.get("PINEAGENTS_WECHAT_QR", "true").strip().lower()
|
||||
# 本期统一登录仅两模式(账号密码/手机号验证码);微信扫码预留后期,未开放(默认 False)。
|
||||
AUTH_WECHAT_LOGIN = os.environ.get("PINEAGENTS_WECHAT_LOGIN", "false").strip().lower() in ("true", "1", "yes")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 运行环境(生产/开发)—— 供业务判断与小程序码版本选择,亦在 /auth/status 回显以便确认
|
||||
# ---------------------------------------------------------------------------
|
||||
# APP_ENV: prod | dev(默认 prod)。dev 时小程序码默认指向「体验版」(trial);prod 指向正式版(release)。
|
||||
APP_ENV = os.environ.get("PINEAGENTS_ENV", "prod").strip().lower()
|
||||
APP_ENV = "prod" if APP_ENV not in ("prod", "dev", "development", "test") else ("prod" if APP_ENV == "prod" else "dev")
|
||||
# 小程序码(wxacode) 的版本类型:release(正式版) | trial(体验版) | develop(开发版)
|
||||
# 默认按 APP_ENV:prod→release,dev→trial;可显式 PINEAGENTS_WXACODE_ENV 覆盖。
|
||||
_WXACODE_DEFAULT = "release" if APP_ENV == "prod" else "trial"
|
||||
WECHAT_WXACODE_ENV = os.environ.get("PINEAGENTS_WXACODE_ENV", "").strip().lower() or _WXACODE_DEFAULT
|
||||
if WECHAT_WXACODE_ENV not in ("release", "trial", "develop"):
|
||||
WECHAT_WXACODE_ENV = _WXACODE_DEFAULT
|
||||
|
||||
|
||||
def resolve_token_expiry(expires_in: int | None) -> int:
|
||||
"""把前端传来的 ``expires_in`` 归一化为秒数。
|
||||
|
||||
@@ -147,17 +147,20 @@ async def _wx_access_token() -> str:
|
||||
return token
|
||||
|
||||
|
||||
async def get_wxacode(scene: str, page: str = "pages/scan-login/index") -> bytes:
|
||||
async def get_wxacode(scene: str, page: str = "pages/scan-login/index", env_version: str | None = None) -> bytes:
|
||||
"""生成小程序码(wxacode)图片,扫码自动打开小程序指定页面并携带 scene。
|
||||
|
||||
env_version:release(正式版) | trial(体验版) | develop(开发版),默认取 config.WECHAT_WXACODE_ENV
|
||||
(随 APP_ENV:prod→release,dev→trial,可显式 PINEAGENTS_WXACODE_ENV 覆盖)。
|
||||
scene 限 32 字符(本服务用短 token)。返回 PNG 字节。
|
||||
"""
|
||||
token = await _wx_access_token()
|
||||
env = env_version or config.WECHAT_WXACODE_ENV
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=15) as client:
|
||||
resp = await client.post(
|
||||
_WX_ACODE_URL.format(token=token),
|
||||
json={"scene": scene, "page": page, "check_path": False, "env_version": "release"},
|
||||
json={"scene": scene, "page": page, "check_path": False, "env_version": env},
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise WechatError(f"wechat getwxacodeunlimit failed: {exc}") from exc
|
||||
|
||||
Reference in New Issue
Block a user