feat: 初始化孕育无忧全栈项目基础代码结构
完成项目初始搭建,包含后端FastAPI+MongoDB服务、前端React+Vite项目的完整基础结构,添加各类配置文件与依赖清单,涵盖核心业务初始框架。
This commit is contained in:
26
backend/app/services/wechat.py
Normal file
26
backend/app/services/wechat.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
|
||||
|
||||
async def code_to_session(code: str) -> dict:
|
||||
"""用小程序 code 换取 openid 和 session_key"""
|
||||
url = "https://api.weixin.qq.com/sns/jscode2session"
|
||||
params = {
|
||||
"appid": settings.wechat_app_id,
|
||||
"secret": settings.wechat_app_secret,
|
||||
"js_code": code,
|
||||
"grant_type": "authorization_code",
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
data = resp.json()
|
||||
|
||||
if "errcode" in data and data["errcode"] != 0:
|
||||
raise Exception(f"微信登录失败: {data.get('errmsg', '未知错误')}")
|
||||
|
||||
return {
|
||||
"openid": data["openid"],
|
||||
"session_key": data["session_key"],
|
||||
"unionid": data.get("unionid"),
|
||||
}
|
||||
Reference in New Issue
Block a user