sequenceDiagram
participant 用户 as 用户端H5
participant 后端 as 后端服务器
participant 微信 as 微信服务器
Note over 用户,后端: 初始化配置
用户->>用户: public.js加载公众号APPID
Note over 用户,微信: 步骤1:触发登录
微信-->>用户: 返回302重定向响应<br>Location: https://open.weixin.qq.com/connect/oauth2/authorize?<br>appid=APPID&<br>redirect_uri=ENCODED_URL&<br>response_type=code&<br>scope=snsapi_userinfo&<br>state=RANDOM_STATE
Note over 用户,微信: 步骤3:用户授权
用户->>微信: 访问授权页面
微信-->>用户: 显示授权确认界面
note over 用户: 点击同意授权
微信-->>用户: 302重定向到redirect_uri?code=AUTH_CODE&state=RANDOM_STATE
Note over 用户,后端: 步骤4:提交授权码
用户->>后端: GET /callback?code=AUTH_CODE&state=RANDOM_STATE
Note over 后端,微信: 步骤5:换取凭证
后端->>微信: POST https://api.weixin.qq.com/sns/oauth2/access_token<br>?appid=APPID&secret=APPSECRET&code=AUTH_CODE&grant_type=authorization_code
微信-->>后端: 返回access_token/openid
Note over 后端: 步骤6:处理登录态
后端->>后端: 1. 验证state防CSRF<br>2. 关联openid与用户体系<br>3. 生成本地session/JWT
Note over 后端,用户: 步骤7:完成登录
后端-->>用户: 302重定向回课程页<br>Set-Cookie: 登录凭证
用户->>用户: 加载课程页(携带登录凭证)