Documentation ¶
Overview ¶
微信开放平台 SDK 的核心库
Index ¶
- Constants
- type AuthServer
- func (srv *AuthServer) AppId() string
- func (srv *AuthServer) AppSecret() string
- func (srv *AuthServer) CacheProvider() Cache
- func (srv *AuthServer) ComponentRefreshToken(currentToken string) (token string, err error)
- func (srv *AuthServer) ComponentToken() (token string, err error)
- func (srv *AuthServer) ServeHTTP(w http.ResponseWriter, r *http.Request, query url.Values) (msg []byte)
- func (srv *AuthServer) SetAESKey(base64AESKey string) (err error)
- func (srv *AuthServer) SetToken(token string) (err error)
- type Cache
- type Client
- type DefaultAccessTokenServer
- type Error
- type ErrorHandler
- type ErrorHandlerFunc
Constants ¶
View Source
const ( ErrCodeOK = 0 ErrCodeInvalidCredential = 40001 // access_token 过期错误码 ErrCodeAccessTokenExpired = 42001 // access_token 过期错误码(maybe!!!) )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthServer ¶
type AuthServer struct {
// contains filtered or unexported fields
}
AuthServer 用于处理微信服务器的回调请求, 并发安全!
通常情况下一个 AuthServer 实例用于处理一个开放平台的回调消息, 并且刷新平台的token和tickprovider;
func NewAuthServer ¶
func NewAuthServer(appId, appSecret, token, base64AESKey string, httpClient *http.Client, cacheProvider Cache) (srv *AuthServer)
NewAuthServer 创建一个新的 AuthServer.
appId: 必选; 开放平台的AppId; appSecret: 必选; 开放平台的AppSecret; token: 必须; 开放平台用于验证签名的token; base64AESKey: 必选; 开放平台处理ticket回掉的aeskey; handler: 必须; 处理微信服务器推送过来的消息(事件)的Handler; cacheProvider: 用于缓存provider ticker的缓存, ticker 10分钟刷新一次, 如果服务器crash或者重启,10分钟内授权可能出问题.
func (*AuthServer) AppId ¶
func (srv *AuthServer) AppId() string
func (*AuthServer) AppSecret ¶
func (srv *AuthServer) AppSecret() string
func (*AuthServer) CacheProvider ¶
func (srv *AuthServer) CacheProvider() Cache
func (*AuthServer) ComponentRefreshToken ¶
func (srv *AuthServer) ComponentRefreshToken(currentToken string) (token string, err error)
func (*AuthServer) ComponentToken ¶
func (srv *AuthServer) ComponentToken() (token string, err error)
func (*AuthServer) ServeHTTP ¶
func (srv *AuthServer) ServeHTTP(w http.ResponseWriter, r *http.Request, query url.Values) (msg []byte)
ServeHTTP 处理微信服务器的回调请求, query 参数可以为 nil.
func (*AuthServer) SetAESKey ¶
func (srv *AuthServer) SetAESKey(base64AESKey string) (err error)
SetAESKey 设置aes加密解密key.
base64AESKey: aes加密解密key, 43字节长(base64编码, 去掉了尾部的'=').
func (*AuthServer) SetToken ¶
func (srv *AuthServer) SetToken(token string) (err error)
SetToken 设置签名token.
type Client ¶
type Client struct { AuthServer *AuthServer HttpClient *http.Client }
func NewClient ¶
func NewClient(srv *AuthServer, clt *http.Client) *Client
NewClient 创建一个新的 Client.
如果 clt == nil 则默认用 util.DefaultHttpClient
func (*Client) GetJSON ¶
GetJSON HTTP GET 微信资源, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.
NOTE: 1. 一般不需要调用这个方法, 请直接调用高层次的封装函数; 2. 最终的 URL == incompleteURL + access_token; 3. response 格式有要求, 要么是 *Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field): struct { Error ... }
func (*Client) PostJSON ¶
func (clt *Client) PostJSON(incompleteURL string, request interface{}, response interface{}) (err error)
PostJSON 用 encoding/json 把 request marshal 为 JSON, HTTP POST 到微信服务器, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.
NOTE: 1. 一般不需要调用这个方法, 请直接调用高层次的封装函数; 2. 最终的 URL == incompleteURL + access_token; 3. response 格式有要求, 要么是 *Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field): struct { Error ... }
type DefaultAccessTokenServer ¶
type DefaultAccessTokenServer struct {
// contains filtered or unexported fields
}
DefaultAccessTokenServer 实现了 AccessTokenServer 接口.
NOTE: 1. 用于单进程环境. 2. 因为 DefaultAccessTokenServer 同时也是一个简单的中控服务器, 而不是仅仅实现 AccessTokenServer 接口, 所以整个系统只能存在一个 DefaultAccessTokenServer 实例!
func NewComponentAccessTokenServer ¶
func NewComponentAccessTokenServer(appId string, token string, refreshToken string, client *Client) (srv *DefaultAccessTokenServer)
NewDefaultAccessTokenServer 开放平台的DefaultAccessTokenServer, 用于获取开放平台授权appid的token 注意: appid为授权的公众号、小程序appid , 必填字段 token为授权token, 可以为空 refreshtoken 用于刷新token的refresh token, 必填字段 client 开放平台的client, 必填字段
func (*DefaultAccessTokenServer) IID01332E16DF5011E5A9D5A4DB30FED8E1 ¶
func (srv *DefaultAccessTokenServer) IID01332E16DF5011E5A9D5A4DB30FED8E1()
func (*DefaultAccessTokenServer) RefreshToken ¶
func (srv *DefaultAccessTokenServer) RefreshToken(currentToken string) (token string, err error)
func (*DefaultAccessTokenServer) Token ¶
func (srv *DefaultAccessTokenServer) Token() (token string, err error)
type ErrorHandler ¶
type ErrorHandler interface {
ServeError(http.ResponseWriter, *http.Request, error)
}
var DefaultErrorHandler ErrorHandler = ErrorHandlerFunc(defaultErrorHandlerFunc)
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
func (ErrorHandlerFunc) ServeError ¶
func (fn ErrorHandlerFunc) ServeError(w http.ResponseWriter, r *http.Request, err error)
Click to show internal directories.
Click to hide internal directories.