1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
authorizer.go 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-09-17 19:33 . 基于服务商企业微信 1.0.1
package authorizer
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"
"gitee.com/h79/goutils/common/logger"
)
// 1
//https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/pre_auth_code.html
// 2 然后去授权,授权成功后,会调用回调函数通知相关相信
//https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$this->component_appid}&pre_auth_code={$this->pre_auth_code}&redirect_uri={$redirect_uri}
// GetPreAuthCode 预授权码(pre_auth_code)是第三方平台方实现授权托管的必备信息,每个预授权码有效期为 10 分钟。需要先获取令牌才能调用。
func GetPreAuthCode(api *token.Api) (*PreAuthCode, error) {
url := fmt.Sprintf("%s/cgi-bin/component/api_create_preauthcode?", consts.ApiPrefixUrl)
var in = struct {
AppId string `json:"component_appid"`
}{
AppId: api.AppId(),
}
buf, err := json.Marshal(&in)
if err != nil {
return nil, err
}
type Result struct {
response.Response
PreAuthCode
}
res := Result{}
err = api.Request("POST", url, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
if err != nil {
return nil, err
}
if res.ErrCode != 0 {
return nil, res
}
return &res.PreAuthCode, nil
}
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/api_get_authorizer_info.html
// 获取授权方的帐号基本信息
func GetAuthorizerInfo(api *token.Api, authorizerAppId string) (*Info, *AuthorizationInfo, error) {
url := fmt.Sprintf("%s/cgi-bin/component/api_get_authorizer_info?", consts.ApiPrefixUrl)
type info struct {
AppId string `json:"component_appid"`
AuthorizerAppId string `json:"authorizer_appid"`
}
in := info{
AppId: api.AppId(),
AuthorizerAppId: authorizerAppId,
}
buf, err := json.Marshal(&in)
if err != nil {
return nil, nil, err
}
type Result struct {
response.Response
Info
AuthorizationInfo
}
res := Result{}
err = api.Request("POST", url, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
if err != nil {
return nil, nil, err
}
logger.Debug("App3 GetAuthorizerInfo: %v", res)
if res.ErrCode != 0 {
return nil, nil, res
}
return &res.Info, &res.AuthorizationInfo, nil
}
func GetAuthorizerList(api *token.Api, offset, count int) (*ListAuthInfo, error) {
url := fmt.Sprintf("%s/cgi-bin/component/api_get_authorizer_list?", consts.ApiPrefixUrl)
type info struct {
AppId string `json:"component_appid"`
Offset int `json:"offset"`
Count int `json:"count"`
}
in := info{
AppId: api.AppId(),
Offset: offset,
Count: count,
}
buf, err := json.Marshal(&in)
if err != nil {
return nil, err
}
type Result struct {
response.Response
ListAuthInfo
}
res := Result{}
err = api.Request("POST", url, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
if err != nil {
return nil, err
}
logger.Debug("App3 GetAuthorizerList: %+v", res)
if res.ErrCode != 0 {
return nil, res
}
return &res.ListAuthInfo, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.0.33

搜索帮助

344bd9b3 5694891 D2dac590 5694891