core

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2022 License: Apache-2.0 Imports: 27 Imported by: 0

README

微信开放平台 SDK 核心 package

微信开放平台的处理逻辑都在这个 package 里面, 其他的模块都是在这个 package 基础上的再封装!

回调请求处理

一个回调地址(多个公众号可以共用一个回调地址)的 http 请求对应了一个 http handler(http.Handler, gin.HandlerFunc…), 这个 http handler 里面的主要逻辑是调用对应公众号的 core.Server 的 ServeHTTP 方法来处理回调请求, core.Server.ServeHTTP 做签名的验证和消息解密, 然后调用 core.Server 的 core.Handler 属性的 ServeMsg 方法来处理消息(事件).
回调请求处理逻辑图

Documentation

Overview

微信开放平台 SDK 的核心库

Index

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 Cache

type Cache interface {
	// Get a cached value by key.
	Get(ctx context.Context, key string) (interface{}, error)
	// GetMulti is a batch version of Get.
	GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
	// Set a cached value with key and expire time.
	Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
	// Delete cached value by key.
	// Should not return error if key not found
	Delete(ctx context.Context, key string) error
	// Increment a cached int value by key, as a counter.
	Incr(ctx context.Context, key string) error
	// Decrement a cached int value by key, as a counter.
	Decr(ctx context.Context, key string) error
	// Check if a cached value exists or not.
	// if key is expired, return (false, nil)
	IsExist(ctx context.Context, key string) (bool, error)
	// Clear all cache.
	ClearAll(ctx context.Context) error
	// Start gc routine based on config string settings.
	StartAndGC(config string) error
}

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

func (clt *Client) GetJSON(incompleteURL string, response interface{}) (err error)

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 Error

type Error struct {
	ErrCode int64  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

func (*Error) Error

func (err *Error) Error() string

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL