feat(init): 添加完整的认证和业务路由模块
- 实现微信登录、手机号绑定、token验证和登出功能 - 添加打卡系统,支持正常、迟到、请假等状态记录 - 集成课程投票功能,支持多课程选择和评分评论 - 开发反馈系统,支持图文反馈提交和管理员处理 - 构建首页页面模块,动态展示学习资源和个人信息 - 设计计划管理系统,支持周/月/年计划制定和跟踪 - 实现小组计划统计和历史记录查询功能 - 完善用户认证会话管理和数据库交互逻辑
This commit is contained in:
55
wechat.py
Normal file
55
wechat.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import httpx
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
class WeChatError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def code_to_openid(code):
|
||||
resp = httpx.get(
|
||||
"https://api.weixin.qq.com/sns/jscode2session",
|
||||
params={
|
||||
"appid": config.WECHAT_APPID,
|
||||
"secret": config.WECHAT_SECRET,
|
||||
"js_code": code,
|
||||
"grant_type": "authorization_code",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
data = resp.json()
|
||||
if data.get("errcode") or "openid" not in data:
|
||||
raise WeChatError(data.get("errmsg", "无效的登录 code"))
|
||||
return data["openid"]
|
||||
|
||||
|
||||
def _get_access_token():
|
||||
resp = httpx.get(
|
||||
"https://api.weixin.qq.com/cgi-bin/token",
|
||||
params={
|
||||
"grant_type": "client_credential",
|
||||
"appid": config.WECHAT_APPID,
|
||||
"secret": config.WECHAT_SECRET,
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
data = resp.json()
|
||||
if not data.get("access_token"):
|
||||
raise WeChatError(data.get("errmsg", "获取微信 access_token 失败"))
|
||||
return data["access_token"]
|
||||
|
||||
|
||||
def get_phone_number(phone_code):
|
||||
access_token = _get_access_token()
|
||||
resp = httpx.post(
|
||||
"https://api.weixin.qq.com/wxa/business/getuserphonenumber",
|
||||
params={"access_token": access_token},
|
||||
json={"code": phone_code},
|
||||
timeout=10,
|
||||
)
|
||||
data = resp.json()
|
||||
if data.get("errcode") != 0:
|
||||
raise WeChatError(data.get("errmsg", "获取手机号失败"))
|
||||
phone_info = data.get("phone_info") or {}
|
||||
return phone_info.get("purePhoneNumber") or phone_info.get("phoneNumber")
|
||||
Reference in New Issue
Block a user