1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
user.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-07-28 16:16 . 还是把 token 放在这里
package oauth2
import (
"encoding/json"
"fmt"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/weixin/consts"
"gitee.com/h79/gothird/weixin/response"
"gitee.com/h79/goutils/common/http"
)
/**
* 网站应用
*
*第一步:请求CODE
* 第三方使用网站应用授权登录前请注意已获取相应网页授权作用域(scope=snsapi_login),则可以通过在PC端打开以下链接:
* https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
*
* https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html
*/
// GetUser GET https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s
// 通过access_token和openid获取用户的基础信息,包括头像、昵称、性别、地区
func GetUser(api *token.Api, openid string) (*User, error) {
if openid == "" {
return nil, token.Error(-1, "openid is empty")
}
uri := fmt.Sprintf("%s/sns/userinfo?openid=%s&", consts.ApiPrefixUrl, openid)
type userResult struct {
response.Response
User
}
res := userResult{}
if er := api.Request("GET", uri, nil, func(hp *http.Http, body []byte) error {
if err := json.Unmarshal(body, &res); err != nil {
return err
}
return res.ReturnIf(api)
}); er != nil {
return nil, er
}
res.AppId = api.AppId()
return &res.User, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.28

搜索帮助

344bd9b3 5694891 D2dac590 5694891