wxopen

package module
v1.0.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: Apache-2.0 Imports: 23 Imported by: 1

README

fastwego/wxopen

A fast wxopen development sdk written in Golang

GoDoc Go Report Card

快速开始 & demo

go get github.com/fastwego/wxopen
// 创建 第三方平台 实例
myPlatform := wxopen.NewPlatform(wxopen.PlatformConfig{
    AppId:     viper.GetString("APPID"),
    AppSecret: viper.GetString("APPSECRET"),
    Token:     viper.GetString("TOKEN"),
    AesKey:    viper.GetString("AESKEY"),
})

// 代 公众号 调用 api
app, err := myPlatform.NewOffiAccount(appid)
payload := []byte(`
{
     "button":[
     {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"view",
               "name":"搜索",
               "url":"http://www.baidu.com/"
            }]
       }]
}`)

resp, err := menu.Create(app, payload)
fmt.Println(resp, err)

// 代 小程序 调用 api
mini, err := myPlatform.NewMiniprogram(appid) 
feedback, err := operation.GetFeedback(mini)
fmt.Println(string(feedback), err)

完整的演示项目:

https://github.com/fastwego/wxopen-demo

接口列表:

架构设计

sdk

框架特点

快速

「快」作为框架设计的核心理念,体现在方方面面:

  • 使用 Go 语言,开发快、编译快、部署快、运行快,轻松服务海量用户
  • 丰富的文档演示代码 ,快速上手,5 分钟即可搭建一套完整的微信开放平台服务
  • 独立清晰的模块划分,快速熟悉整个框架,没有意外,一切都是你期望的样子
  • 甚至连框架自身的大部分代码也是自动生成的,维护更新快到超乎想象
符合直觉

作为第三方开发框架,尽可能贴合官方文档和设计,不引入新的概念,不给开发者添加学习负担

简洁而不过度封装

作为具体业务和微信之间的中间层,专注于通道的角色:帮业务把配置/材料投递到微信,将微信响应/推送透传回业务

官方文档就是最好的文档

每个接口的注释都附带官方文档的链接,让你随时翻阅,省时省心

完备的单元测试

100% 覆盖每一个接口,让你每一次调用都信心满满

详细的日志

每个关键环节都为你完整记录,Debug 倍轻松,你可以自由定义日志输出,甚至可以关闭日志

支持服务集群

单台服务器支撑不住访问流量/想提高服务可用性?

只需 重载 GetComponentAccessTokenFunc 方法 ,从中控服务获取 AccessToken,即可解决多实例刷新冲突/覆盖的问题

活跃的开发者社区

FastWeGo 是一套完整的微信开发框架,包括公众号、开放平台、微信支付、企业微信、小程序、小游戏等微信服务,拥有庞大的开发者用户群体

你遇到的所有问题几乎都可以在社区找到解决方案

参与贡献

欢迎提交 pull request / issue / 文档,一起让微信开发更快更好!

Faster we go together!

加入开发者交流群

赞助商

感谢以下合作伙伴支持:

Documentation

Overview

微信开放平台 SDK

See: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Third_party_platform_appid.html

Index

Constants

This section is empty.

Variables

View Source
var (
	WXServerUrl                     = "https://api.weixin.qq.com" // 微信 api 服务器地址
	UserAgent                       = "fastwego/wxopen"
	ErrorComponentAccessTokenExpire = errors.New("component_access_token expire")
	ErrorSystemBusy                 = errors.New("system busy")
)

Functions

func GetAuthorizerAccessToken

func GetAuthorizerAccessToken(platform *Platform, appid string) (accessToken string, err error)

GetAuthorizerAccessToken 获取 authorizer_access_token

框架默认将 authorizer_access_token 缓存在本地

实际业务 建议 存储到数据库

func GetComponentAccessToken

func GetComponentAccessToken(ctx *Platform) (accessToken string, err error)

从 公众号实例 的 ComponentAccessToken 管理器 获取 access_token

如果没有 access_token 或者 已过期,那么刷新

获得新的 access_token 后 过期时间设置为 0.9 * expiresIn 提供一定冗余

func GetComponentVerifyTicket

func GetComponentVerifyTicket(platform *Platform) (appTicket string, err error)

GetComponentVerifyTicket 获取 component_verify_ticket

func NoticeAuthorizerAccessTokenExpire

func NoticeAuthorizerAccessTokenExpire(platform *Platform, appid string) (err error)

NoticeAuthorizerAccessTokenExpire 通知 authorizer_access_token 过期

框架默认将 authorizer_access_token 缓存在本地

实际业务 建议 存储到数据库

func NoticeComponentAccessTokenExpire

func NoticeComponentAccessTokenExpire(ctx *Platform) (err error)

NoticeComponentAccessTokenExpire 只需将本地存储的 access_token 删除,即完成了 access_token 已过期的 主动通知

retry 请求的时候,会发现本地没有 access_token ,从而触发refresh

func ReceiveComponentVerifyTicket

func ReceiveComponentVerifyTicket(platform *Platform, ticket string) (err error)

ReceiveComponentVerifyTicket 接收 component_verify_ticket

Types

type Client

type Client struct {
	Ctx *Platform
}

Client 用于向微信接口发送请求

func (*Client) HTTPGet

func (client *Client) HTTPGet(uri string) (resp []byte, err error)

HTTPGet GET 请求

func (*Client) HTTPPost

func (client *Client) HTTPPost(uri string, payload io.Reader, contentType string) (resp []byte, err error)

HTTPPost POST 请求

type GetAuthorizerAccessTokenFunc

type GetAuthorizerAccessTokenFunc func(platform *Platform, appid string) (authorizerAccessToken string, err error)

GetAuthorizerAccessTokenFunc 获取 AuthorizerAccessToken 方法接口

type GetComponentAccessTokenFunc

type GetComponentAccessTokenFunc func(platform *Platform) (accessToken string, err error)

GetComponentAccessTokenFunc 获取 component_access_token 方法接口

type GetComponentVerifyTicketFunc

type GetComponentVerifyTicketFunc func(platform *Platform) (ticket string, err error)

GetComponentVerifyTicketFunc 获取 component_verify_ticket 方法接口

type NoticeAuthorizerAccessTokenExpireFunc

type NoticeAuthorizerAccessTokenExpireFunc func(platform *Platform, appid string) (err error)

NoticeAuthorizerAccessTokenExpireFunc 通知刷新 AuthorizerAccessToken 方法接口

type NoticeComponentAccessTokenExpireFunc

type NoticeComponentAccessTokenExpireFunc func(platform *Platform) (err error)

NoticeComponentAccessTokenExpireFunc 通知中控 刷新 component_access_token

type Platform

type Platform struct {
	Config PlatformConfig
	Cache  cachego.Cache
	Client Client
	Server Server
	Logger *log.Logger

	GetComponentAccessTokenHandler          GetComponentAccessTokenFunc
	NoticeComponentAccessTokenExpireHandler NoticeComponentAccessTokenExpireFunc

	GetComponentVerifyTicketHandler     GetComponentVerifyTicketFunc
	ReceiveComponentVerifyTicketHandler ReceiveComponentVerifyTicketFunc

	GetAuthorizerAccessTokenHandler          GetAuthorizerAccessTokenFunc
	NoticeAuthorizerAccessTokenExpireHandler NoticeAuthorizerAccessTokenExpireFunc
}

Platform 平台实例

func NewPlatform

func NewPlatform(config PlatformConfig) (platform *Platform)

创建 平台 实例

func (*Platform) NewMiniprogram

func (platform *Platform) NewMiniprogram(appid string) (mini *miniprogram.Miniprogram, err error)

创建 小程序 实例

func (*Platform) NewOffiAccount

func (platform *Platform) NewOffiAccount(appid string) (offiAccount *offiaccount.OffiAccount, err error)

创建公众号实例

type PlatformConfig

type PlatformConfig struct {
	AppId     string
	AppSecret string
	Token     string
	AesKey    string
}

PlatformConfig 平台 配置

type ReceiveComponentVerifyTicketFunc

type ReceiveComponentVerifyTicketFunc func(platform *Platform, ticket string) (err error)

ReceiveComponentVerifyTicketFunc 接收 component_verify_ticket 方法接口

type Server

type Server struct {
	Ctx *Platform
}

响应微信请求 或 推送消息/事件 的服务器

func (*Server) ParseXML

func (s *Server) ParseXML(body []byte) (m interface{}, err error)

ParseXML 解析微信推送过来的消息/事件

func (*Server) Response

func (s *Server) Response(writer http.ResponseWriter, request *http.Request, reply interface{}) (err error)

Response 响应微信消息 (自动判断是否要加密)

Directories

Path Synopsis
apis
account
Package account 开放平台-账号管理
Package account 开放平台-账号管理
auth
Package auth 开放平台
Package auth 开放平台
oauth
Package oauth 代公众号发起网页授权
Package oauth 代公众号发起网页授权
Package test 模拟微信服务器 测试
Package test 模拟微信服务器 测试
type

Jump to

Keyboard shortcuts

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