Integrate WeChat Official Account support and update configuration
- Added WeChat Official Account configuration options to .env.example and config.go for OAuth2 integration. - Updated main.go to initialize WeChat OAuth handler and register routes for handling OAuth requests. - Enhanced documentation to include references for WeChat Official Account functionality. - Updated route registration to accommodate the new OAuth handler for improved API structure.
This commit is contained in:
@@ -26,3 +26,7 @@
|
||||
## 上传(七牛直传)
|
||||
|
||||
- `docs/common/upload_qiniu.md`
|
||||
|
||||
## 微信公众号
|
||||
|
||||
- `docs/common/wechat_official.md`
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# 微信公众号:code 换取 openid/用户信息
|
||||
|
||||
本接口用于微信公众号网页授权(OAuth2):前端拿到微信回调的 `code` 后,调用后端换取 `openid`,并可选获取用户信息(昵称/头像/unionid 等)。
|
||||
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx65d83a301fc84068&redirect_uri=https%3A%2F%2Fwww.baidu.com&response_type=code&scope=&state=1#wechat_redirect
|
||||
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx65d83a301fc84068&redirect_uri=https%3A%2F%2Fwww.baidu.com&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
|
||||
## 配置
|
||||
|
||||
在 `.env` 中配置(示例见:`.env.example`):
|
||||
|
||||
- `WECHAT_OA_APP_ID`
|
||||
- `WECHAT_OA_APP_SECRET`
|
||||
- `WECHAT_OA_TIMEOUT_SECONDS`(可选)
|
||||
|
||||
## 接口
|
||||
|
||||
`POST /api/v1/wechat/official/oauth/code2user`
|
||||
|
||||
说明:
|
||||
- 不需要登录(无需 Bearer Token),因为该 `code` 本身来自微信网页授权流程。
|
||||
|
||||
请求体:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": "微信回调参数 code",
|
||||
"with_userinfo": true
|
||||
}
|
||||
```
|
||||
|
||||
- `with_userinfo` 可选,默认 `true`。
|
||||
- 若网页授权 scope 仅为 `snsapi_base`,通常只能拿到 `openid`,获取 `userinfo` 可能失败(接口会返回 `userinfo_error` 提示)。
|
||||
|
||||
curl 示例:
|
||||
|
||||
```bash
|
||||
curl -X POST 'http://127.0.0.1:8080/api/v1/wechat/official/oauth/code2user' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"code":"YOUR_CODE","with_userinfo":true}'
|
||||
```
|
||||
|
||||
成功响应示例(节选):
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"message": "success",
|
||||
"data": {
|
||||
"openid": "oXXXX",
|
||||
"unionid": "oXXXX",
|
||||
"scope": "snsapi_userinfo",
|
||||
"expires_in": 7200,
|
||||
"userinfo": {
|
||||
"openid": "oXXXX",
|
||||
"nickname": "昵称",
|
||||
"headimgurl": "https://...",
|
||||
"sex": 1,
|
||||
"province": "Guangdong",
|
||||
"city": "Shenzhen",
|
||||
"country": "CN",
|
||||
"unionid": "oXXXX"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user