1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
session.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-05-14 18:03 . 公众号
package session
import (
"encoding/json"
"gitee.com/h79/gothird/weixin/access"
"gitee.com/h79/gothird/weixin/response"
"gitee.com/h79/goutils/common/http"
)
// 官司说明 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
// https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html
type Session struct {
access.Token
Info
AppId string `json:"-"`
}
type Info struct {
Scope string `json:"scope,omitempty"` //用户授权的作用域,使用逗号(,)分隔
Openid string `json:"openid,omitempty"` //每个应用有对应的openid
IsSnapShotUser int `json:"is_snapshotuser"` //是否为快照页模式虚拟账号,只有当用户是快照页模式虚拟账号时返回,值为1
Unionid string `json:"unionid"` //用户统一标识(针对一个微信开放平台账号下的应用,同一用户的 unionid 是唯一的),只有当scope为"snsapi_userinfo"时返回
}
func GetToken(appid string, url string) (*Session, error) {
hp := http.Http{}
body, err := hp.DoBytes("GET", url, nil)
if err != nil {
return &Session{}, err
}
/*
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
*/
type sessionResult struct {
response.Response
Session
}
res := sessionResult{}
res.AppId = appid
if err = json.Unmarshal(body, &res); err != nil {
return &res.Session, err
}
return &res.Session, res.ErrorIf()
}
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.103

搜索帮助