wxapi

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 8 Imported by: 0

README

微信公众号开发SDK

go-wx-api是对微信公众号API的封装,可以当作SDK使用

编译例子

  1. 该函数包已经使用go modules发布,需要golang 1.11.x及以上版本
  2. 请参考go-wx-apps,那里包含了例程和工具程序

使用方法

以下是一个简单的例子,用于说明使用go-wx-api的主要执行步骤。更详细的例子参考go-wx-apps

package main

import (
	"github.com/rosbit/go-wx-api/conf"
	"github.com/rosbit/go-wx-api"
	"net/http"
	"fmt"
)

const (
	token     = "微信公众号的token"
	appId     = "微信公众号appId"
	appSecret = "微信公众号的secret"
	aesKey    = "" //安全模式 使用的AESKey,如果是 明文传输,该串为空
	
	listenPort = 7070   // 服务侦听的端口号,请根据微信公众号管理端的服务器配置正确设置
	service    = "/wx"  // 微信公众号管理端服务器配置中URL的路径部分

	workerNum = 3 // 处理请求的并发数
)

func main() {
	// 步骤1. 设置配置参数
	wxconf.WxParams = wxconf.WxParamsT{Token:token, AppId:appId, AppSecret:appSecret}
	if aesKey != "" {
		if err := wxconf.SetAesKey(aesKey); err != nil {
			fmt.Printf("invalid aesKey: %v\n", err)
			return
		}
	}

	// 步骤2. 初始化SDK
	wxapi.InitWxAPI(workerNum, os.Stdout)

	// 步骤2.5 设置签名验证的中间件。由于net/http不支持中间件,省去该步骤
	// signatureChecker := wxapi.NewWxSignatureChecker(wxconf.WxParams.Token, 0, []string{service})
	// <middleWareContainer>.Use(signatureChecker)

	// 步骤3. 设置http路由,启动http服务
	http.HandleFunc(service, wxapi.Echo)     // 用于配置
	http.HandleFunc(service, wxapi.Request)  // 用于实际执行公众号请求,和wxapi.Echo只能使用一个。
	                                         // 可以使用高级路由功能同时设置,参考 github.com/rosbit/go-wx-api/samples/wx-echo-server
	http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil)
}

其它

  1. 该函数包可以处理文本消息、用户关注/取消关注事件、菜单点击事件
  2. 其它消息、事件可以根据需要扩充

Documentation

Overview

*

  • RESTful API implementation
  • GET /wx -- 用于实现服务号接口配置,实际路径通过http路由关联
  • POST /wx -- 处理微信消息/事件的入口,实际路径通过http路由关联
  • GET /redirect -- 微信网页授权接口,处理服务号菜单的总入口,
  • 不同菜单通过网页授权参数state区分,实际路径通过http路由关联
  • Rosbit Xu

*

  • 注册微信消息/事件处理函数,用于覆盖缺省处理函数
  • 当前处理的消息/事件未全部覆盖,可以根据需要在此扩充

*

  • signature checker as a http middleware
  • Rosbit Xu

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Echo

func Echo(w http.ResponseWriter, r *http.Request)

用于微信服务号设置

func InitWxAPI

func InitWxAPI(workerNum int, logger io.Writer)

func NewWxSignatureChecker

func NewWxSignatureChecker(wxToken string, timeout int, uriPrefixes []string) func(http.ResponseWriter, *http.Request, http.HandlerFunc)

*

  • 创建http处理中间件,验证消息签名,如果非法直接返回错误
  • @param wxToken 公众号在微信管理后台定义的token
  • @param timeout 消失时间戳超时处理,秒数,如果<=0不检查时间戳
  • @param uriPrefixes 需要检查签名的URI前缀列表,不相关的URI忽略检查;如果为nil,全部检查

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request)

微信网页授权

func RegisterRedictHandler

func RegisterRedictHandler(handler RedirectHandler)

*

  • 注册微信网页授权处理函数

func RegisterSubscribeEventHandler

func RegisterSubscribeEventHandler(handler SubscribeEventHandler)

func RegisterTextMsgHandler

func RegisterTextMsgHandler(handler TextMsgHandler)

func RegisterViewEventHandler

func RegisterViewEventHandler(handler ViewEventHandler)

func Request

func Request(w http.ResponseWriter, r *http.Request)

微信服务号消息/事件处理入口

func UnregisterSubscribeEventHandler

func UnregisterSubscribeEventHandler(handler UnsubscribeEventHandler)

Types

type RedirectHandler

type RedirectHandler func(openId, state string) (c string, h map[string]string, r string, err error)

*

  • [函数签名]根据服务号菜单state做跳转
  • @param openId 订阅用户的openId
  • @param state 微信网页授权中的参数,用来标识某个菜单
  • @return
  • c 需要显示服务号对话框中的内容
  • h 需要在微信内嵌浏览器中设置的header信息,包括Cookie
  • r 需要通过302跳转的URL。如果r不是空串,c的内容被忽略
  • err 如果没有错误返回nil,非nil表示错误

type SubscribeEventHandler

type SubscribeEventHandler func(*wxmsg.SubscribeEvent) wxmsg.ReplyMsg // 用户关注(subscribe)服务号事件处理

type TextMsgHandler

type TextMsgHandler func(*wxmsg.TextMsg) wxmsg.ReplyMsg // text消息处理

type UnsubscribeEventHandler

type UnsubscribeEventHandler func(*wxmsg.SubscribeEvent) wxmsg.ReplyMsg // 用户取消关注(unsubscribe)服务号事件处理

type ViewEventHandler

type ViewEventHandler func(*wxmsg.ViewEvent) wxmsg.ReplyMsg // VIEW事件处理

Directories

Path Synopsis
* * 缺省的微信网页授权处理 * * 微信网页授权处理主流程:固定线程数处理所有的请求 * 工作方式: * 1.
* * 缺省的微信网页授权处理 * * 微信网页授权处理主流程:固定线程数处理所有的请求 * 工作方式: * 1.
* * 微信服务号配置信息
* * 微信服务号配置信息
* * 微信信息加解密/签名/随机数 * 1.
* * 微信信息加解密/签名/随机数 * 1.

Jump to

Keyboard shortcuts

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