Documentation ¶
Overview ¶
公众号第三方平台接口
Index ¶
- Constants
- Variables
- func AuthCodeURL(componentAppId, preAuthCode, redirectURI, state string) string
- func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv Server, ...)
- type AccessTokenServer
- type AuthorizationInfo
- type AuthorizerAccessTokenInfo
- type AuthorizerAccessTokenServer
- type AuthorizerInfo
- type AuthorizerInfoEx
- type Client
- func (clt *Client) CreatePreAuthCode() (code *PreAuthCode, err error)
- func (clt *Client) GetAuthorizerInfo(authorizerAppId string) (info *AuthorizerInfoEx, err error)
- func (clt *Client) GetAuthorizerOption(authorizerAppId, optionName string) (optionValue string, err error)
- func (clt *Client) GetJSON(incompleteURL string, response interface{}) (err error)
- func (clt *Client) PostJSON(incompleteURL string, request interface{}, response interface{}) (err error)
- func (clt *Client) QueryAuth(authCode string) (info *AuthorizationInfo, err error)
- func (clt *Client) SetAuthorizerOption(authorizerAppId, optionName, optionValue string) (err error)
- type DefaultAccessTokenServer
- type DefaultServer
- func (srv *DefaultServer) AppId() string
- func (srv *DefaultServer) CurrentAESKey() (key [32]byte)
- func (srv *DefaultServer) LastAESKey() (key [32]byte, valid bool)
- func (srv *DefaultServer) MessageHandler() MessageHandler
- func (srv *DefaultServer) Token() string
- func (srv *DefaultServer) UpdateAESKey(aesKey []byte) (err error)
- type Function
- type MessageHandler
- type MessageHandlerFunc
- type MessageServeMux
- func (mux *MessageServeMux) DefaultMessageHandle(handler MessageHandler)
- func (mux *MessageServeMux) DefaultMessageHandleFunc(handler func(http.ResponseWriter, *Request))
- func (mux *MessageServeMux) MessageHandle(msgType string, handler MessageHandler)
- func (mux *MessageServeMux) MessageHandleFunc(msgType string, handler func(http.ResponseWriter, *Request))
- func (mux *MessageServeMux) ServeMessage(w http.ResponseWriter, r *Request)
- type MixedMessage
- type PreAuthCode
- type Request
- type RequestHttpBody
- type Server
- type ServerFrontend
- type UnauthorizedMessage
- type VerifyTicketCache
- type VerifyTicketCache2
- type VerifyTicketGetter
- type VerifyTicketMessage
Constants ¶
const ( // 微信服务器推送过来的消息类型 MsgTypeVerifyTicket = "component_verify_ticket" // 推送 component_verify_ticket 协议 )
Variables ¶
var ErrNotFound = errors.New("item not found")
Functions ¶
Types ¶
type AccessTokenServer ¶
type AccessTokenServer interface { // 从中控服务器获取被缓存的 component_access_token. Token() (string, error) // 请求中控服务器到微信服务器刷新 component_access_token. // // 高并发场景下某个时间点可能有很多请求(比如缓存的 component_access_token 刚好过期时), 但是我们 // 不期望也没有必要让这些请求都去微信服务器获取 component_access_token(有可能导致api超过调用限制), // 实际上这些请求只需要一个新的 component_access_token 即可, 所以建议 AccessTokenServer 从微信服务器 // 获取一次 component_access_token 之后的至多5秒内(收敛时间, 视情况而定, 理论上至多5个http或tcp周期) // 再次调用该函数不再去微信服务器获取, 而是直接返回之前的结果. TokenRefresh() (string, error) // 没有实际意义, 接口标识 Tag7B36CB9FFE9911E48469A4DB30FED8E1() }
component_access_token 中控服务器接口.
type AuthorizationInfo ¶
type AuthorizationInfo struct { AuthorizerAccessTokenInfo AuthorizerAppId string `json:"authorizer_appid"` FunctionInfo []Function `json:"func_info"` }
type AuthorizerAccessTokenServer ¶
type AuthorizerAccessTokenServer struct {
// contains filtered or unexported fields
}
authorizer_access_token 中控服务器, mp.AccessTokenServer 的简单实现.
NOTE: 1. 用于单进程环境. 2. 因为 AuthorizerAccessTokenServer 同时也是一个简单的中控服务器, 而不是仅仅实现 mp.AccessTokenServer 接口, 所以整个系统只能存在一个 AuthorizerAccessTokenServer 实例!
func NewAuthorizerAccessTokenServer ¶
func NewAuthorizerAccessTokenServer(clt *Client, authorizerAppId, authorizerRefreshToken string) (srv *AuthorizerAccessTokenServer)
创建一个新的 AuthorizerAccessTokenServer.
func (*AuthorizerAccessTokenServer) TagCE90001AFE9C11E48611A4DB30FED8E1 ¶
func (srv *AuthorizerAccessTokenServer) TagCE90001AFE9C11E48611A4DB30FED8E1()
func (*AuthorizerAccessTokenServer) Token ¶
func (srv *AuthorizerAccessTokenServer) Token() (token string, err error)
獲取 authorizer_access_token
func (*AuthorizerAccessTokenServer) TokenRefresh ¶
func (srv *AuthorizerAccessTokenServer) TokenRefresh() (token string, err error)
刷新 authorizer_access_token
type AuthorizerInfo ¶
type AuthorizerInfo struct { NickName string `json:"nick_name"` HeadImage string `json:"head_img"` ServiceTypeInfo struct { Id int64 `json:"id"` } `json:"service_type_info"` VerifyTypeInfo struct { Id int64 `json:"id"` } `json:"verify_type_info"` UserName string `json:"user_name"` Alias string `json:"alias"` }
type AuthorizerInfoEx ¶
type AuthorizerInfoEx struct { AuthorizerInfo AuthorizerInfo `json:"authorizer_info"` QrCodeURL string `json:"qrcode_url"` AuthorizationInfo AuthorizationInfo `json:"authorization_info"` }
type Client ¶
type Client struct { AccessTokenServer AppId string HttpClient *http.Client }
func NewClient ¶
func NewClient(appId string, srv AccessTokenServer, clt *http.Client) *Client
创建一个新的 Client.
如果 clt == nil 则默认用 http.DefaultClient
func (*Client) CreatePreAuthCode ¶
func (clt *Client) CreatePreAuthCode() (code *PreAuthCode, err error)
获取预授权码.
func (*Client) GetAuthorizerInfo ¶
func (clt *Client) GetAuthorizerInfo(authorizerAppId string) (info *AuthorizerInfoEx, err error)
获取授权方的账户信息.
func (*Client) GetAuthorizerOption ¶
func (clt *Client) GetAuthorizerOption(authorizerAppId, optionName string) (optionValue string, err error)
获取授权方的选项设置信息.
func (*Client) GetJSON ¶
GET 微信资源, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.
NOTE: 1. 一般不用调用这个方法, 请直接调用高层次的封装方法; 2. 最终的 URL == incompleteURL + component_access_token; 3. response 格式有要求, 要么是 *mp.Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field): struct { mp.Error ... }
func (*Client) PostJSON ¶
func (clt *Client) PostJSON(incompleteURL string, request interface{}, response interface{}) (err error)
用 encoding/json 把 request marshal 为 JSON, 放入 http 请求的 body 中, POST 到微信服务器, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.
NOTE: 1. 一般不用调用这个方法, 请直接调用高层次的封装方法; 2. 最终的 URL == incompleteURL + component_access_token; 3. response 格式有要求, 要么是 *mp.Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field): struct { mp.Error ... }
func (*Client) QueryAuth ¶
func (clt *Client) QueryAuth(authCode string) (info *AuthorizationInfo, err error)
使用授权码换取公众号的授权信息.
func (*Client) SetAuthorizerOption ¶
设置授权方的选项信息.
type DefaultAccessTokenServer ¶
type DefaultAccessTokenServer struct {
// contains filtered or unexported fields
}
AccessTokenServer 的简单实现.
NOTE: 1. 用于单进程环境. 2. 因为 DefaultAccessTokenServer 同时也是一个简单的中控服务器, 而不是仅仅实现 AccessTokenServer 接口, 所以整个系统只能存在一个 DefaultAccessTokenServer 实例!
func NewDefaultAccessTokenServer ¶
func NewDefaultAccessTokenServer(appId, appSecret string, ticketGetter VerifyTicketGetter, clt *http.Client) (srv *DefaultAccessTokenServer)
创建一个新的 DefaultAccessTokenServer.
如果 clt == nil 则默认使用 http.DefaultClient.
func (*DefaultAccessTokenServer) Tag7B36CB9FFE9911E48469A4DB30FED8E1 ¶
func (srv *DefaultAccessTokenServer) Tag7B36CB9FFE9911E48469A4DB30FED8E1()
func (*DefaultAccessTokenServer) Token ¶
func (srv *DefaultAccessTokenServer) Token() (token string, err error)
func (*DefaultAccessTokenServer) TokenRefresh ¶
func (srv *DefaultAccessTokenServer) TokenRefresh() (token string, err error)
type DefaultServer ¶
type DefaultServer struct {
// contains filtered or unexported fields
}
func NewDefaultServer ¶
func NewDefaultServer(appId, token string, AESKey []byte, handler MessageHandler) (srv *DefaultServer)
func (*DefaultServer) AppId ¶
func (srv *DefaultServer) AppId() string
func (*DefaultServer) CurrentAESKey ¶
func (srv *DefaultServer) CurrentAESKey() (key [32]byte)
func (*DefaultServer) LastAESKey ¶
func (srv *DefaultServer) LastAESKey() (key [32]byte, valid bool)
func (*DefaultServer) MessageHandler ¶
func (srv *DefaultServer) MessageHandler() MessageHandler
func (*DefaultServer) Token ¶
func (srv *DefaultServer) Token() string
func (*DefaultServer) UpdateAESKey ¶
func (srv *DefaultServer) UpdateAESKey(aesKey []byte) (err error)
type Function ¶
type Function struct { ScopeCategory struct { Id int64 `json:"id"` } `json:"funcscope_category"` }
權限集
type MessageHandler ¶
type MessageHandler interface {
ServeMessage(http.ResponseWriter, *Request)
}
微信服务器推送过来的消息(事件)处理接口
type MessageHandlerFunc ¶
type MessageHandlerFunc func(http.ResponseWriter, *Request)
func (MessageHandlerFunc) ServeMessage ¶
func (fn MessageHandlerFunc) ServeMessage(w http.ResponseWriter, r *Request)
type MessageServeMux ¶
type MessageServeMux struct {
// contains filtered or unexported fields
}
MessageServeMux 实现了一个简单的消息(事件)路由器, 同时也是一个 MessageHandler 的实现.
func NewMessageServeMux ¶
func NewMessageServeMux() *MessageServeMux
func (*MessageServeMux) DefaultMessageHandle ¶
func (mux *MessageServeMux) DefaultMessageHandle(handler MessageHandler)
注册消息的默认 MessageHandler.
func (*MessageServeMux) DefaultMessageHandleFunc ¶
func (mux *MessageServeMux) DefaultMessageHandleFunc(handler func(http.ResponseWriter, *Request))
注册消息的默认 MessageHandler.
func (*MessageServeMux) MessageHandle ¶
func (mux *MessageServeMux) MessageHandle(msgType string, handler MessageHandler)
注册特定类型消息的 MessageHandler.
func (*MessageServeMux) MessageHandleFunc ¶
func (mux *MessageServeMux) MessageHandleFunc(msgType string, handler func(http.ResponseWriter, *Request))
注册特定类型消息的 MessageHandler.
func (*MessageServeMux) ServeMessage ¶
func (mux *MessageServeMux) ServeMessage(w http.ResponseWriter, r *Request)
MessageServeMux 实现了 MessageHandler 接口.
type MixedMessage ¶
type MixedMessage struct { XMLName struct{} `xml:"xml" json:"-"` AppId string `xml:"AppId" json:"AppId"` CreateTime int64 `xml:"CreateTime" json:"CreateTime"` InfoType string `xml:"InfoType" json:"InfoType"` VerifyTicket string `xml:"ComponentVerifyTicket" json:"ComponentVerifyTicket"` AuthorizerAppId string `xml:"AuthorizerAppid" json:"AuthorizerAppid"` }
微信服务器推送过来的消息(事件)的合集.
type PreAuthCode ¶
type Request ¶
type Request struct { Token string // 请求消息所属第三方平台的 Token HttpRequest *http.Request // 可以为 nil, 因为某些 http 框架没有提供此参数 QueryValues url.Values // 回调请求 URL 中的查询参数集合 MsgSignature string // 回调请求 URL 中的消息体签名: msg_signature EncryptType string // 回调请求 URL 中的加密方式: encrypt_type Timestamp int64 // 回调请求 URL 中的时间戳: timestamp Nonce string // 回调请求 URL 中的随机数: nonce RawMsgXML []byte // 消息的"明文"XML 文本 MixedMsg *MixedMessage // RawMsgXML 解析后的消息 AESKey [32]byte // 当前消息 AES 加密的 key Random []byte // 当前消息加密时所用的 random, 16 bytes AppId string // 当前消息的 AppId }
消息(事件)请求信息
type RequestHttpBody ¶
type RequestHttpBody struct { XMLName struct{} `xml:"xml" json:"-"` AppId string `xml:"AppId"` EncryptedMsg string `xml:"Encrypt"` }
微信服务器请求 http body
type ServerFrontend ¶
type ServerFrontend struct {
// contains filtered or unexported fields
}
func NewServerFrontend ¶
func NewServerFrontend(server Server, handler mp.ErrorHandler, interceptor mp.Interceptor) *ServerFrontend
handler, interceptor 均可以为 nil
func (*ServerFrontend) ServeHTTP ¶
func (frontend *ServerFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)
实现 http.Handler.
type UnauthorizedMessage ¶
type UnauthorizedMessage struct {}
func GetUnauthorizedMessage ¶
func GetUnauthorizedMessage(msg *MixedMessage) *UnauthorizedMessage
type VerifyTicketCache ¶
type VerifyTicketCache struct {
// contains filtered or unexported fields
}
func (*VerifyTicketCache) GetComponentVerifyTicket ¶
func (cache *VerifyTicketCache) GetComponentVerifyTicket(appId string) (ticket string, err error)
func (*VerifyTicketCache) SetComponentVerifyTicket ¶
func (cache *VerifyTicketCache) SetComponentVerifyTicket(appId string, ticket string) (err error)
func (*VerifyTicketCache) Tag9AEACC95FE9911E4B5A4A4DB30FED8E1 ¶
func (cache *VerifyTicketCache) Tag9AEACC95FE9911E4B5A4A4DB30FED8E1()
type VerifyTicketCache2 ¶
type VerifyTicketCache2 struct {
// contains filtered or unexported fields
}
func NewVerifyTicketCache2 ¶
func NewVerifyTicketCache2() *VerifyTicketCache2
func (*VerifyTicketCache2) GetComponentVerifyTicket ¶
func (cache *VerifyTicketCache2) GetComponentVerifyTicket(appId string) (ticket string, err error)
func (*VerifyTicketCache2) SetComponentVerifyTicket ¶
func (cache *VerifyTicketCache2) SetComponentVerifyTicket(appId string, ticket string) (err error)
func (*VerifyTicketCache2) Tag9AEACC95FE9911E4B5A4A4DB30FED8E1 ¶
func (cache *VerifyTicketCache2) Tag9AEACC95FE9911E4B5A4A4DB30FED8E1()
type VerifyTicketGetter ¶
type VerifyTicketGetter interface { // 根据 component_appid 获取第三方平台当前的 component_verify_ticket, 如果没有找到返回 ErrNotFound GetComponentVerifyTicket(appId string) (ticket string, err error) // 没有实际意义, 接口标识 Tag9AEACC95FE9911E4B5A4A4DB30FED8E1() }
component_verify_ticket 獲取接口
type VerifyTicketMessage ¶
type VerifyTicketMessage struct { XMLName struct{} `xml:"xml" json:"-"` AppId string `xml:"AppId" json:"AppId"` CreateTime int64 `xml:"CreateTime" json:"CreateTime"` InfoType string `xml:"InfoType" json:"InfoType"` VerifyTicket string `xml:"ComponentVerifyTicket" json:"ComponentVerifyTicket"` }
func GetVerifyTicketMessage ¶
func GetVerifyTicketMessage(msg *MixedMessage) *VerifyTicketMessage
Source Files ¶
- access_token_server.go
- auth.go
- authorizer_access_token_server.go
- buffer_pool.go
- client.go
- create_preauthcode.go
- doc.go
- get_authorizer_info.go
- get_authorizer_option.go
- msg_handler.go
- msg_serve_mux.go
- notification.go
- query_auth.go
- serve_http.go
- server.go
- server_frontend.go
- set_authorizer_option.go
- verify_ticket_getter.go