work_callback

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(cfg *config.Config) (client *Client)

NewClient 初始化

func (*Client) GetCallbackMessage

func (r *Client) GetCallbackMessage(encryptedMsg []byte, msg interface{}) (err error)

GetCallbackMessage 获取回调事件中的消息内容

 //Gin框架的使用示例
	r.POST("/v1/event/callback", func(c *gin.Context) {
		var (
			message kf.CallbackMessage
			body []byte
		)
		// 读取原始消息内容
		body, err = c.GetRawData()
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}
		// 解析原始数据
		message, err = kfClient.GetCallbackMessage(body)
		if err != nil {
			c.String(http.StatusInternalServerError, "消息获取失败")
			return
		}
		fmt.Println(message)
		c.String(200, "ok")
	})

func (*Client) VerifyURL

func (r *Client) VerifyURL(options SignatureOptions) (string, error)

VerifyURL 验证请求参数是否合法并返回解密后的消息内容

 //Gin框架的使用示例
	r.GET("/v1/event/callback", func(c *gin.Context) {
		options := kf.SignatureOptions{}
		//获取回调的的校验参数
		if = c.ShouldBindQuery(&options); err != nil {
			c.String(http.StatusUnauthorized, "参数解析失败")
		}
		// 调用VerifyURL方法校验当前请求,如果合法则把解密后的内容作为响应返回给微信服务器
		echo, err := kfClient.VerifyURL(options)
		if err == nil {
			c.String(http.StatusOK, echo)
		} else {
			c.String(http.StatusUnauthorized, "非法请求来源")
		}
	})

type SignatureOptions

type SignatureOptions struct {
	Signature string `form:"msg_signature"`
	TimeStamp string `form:"timestamp"`
	Nonce     string `form:"nonce"`
	EchoStr   string `form:"echostr"`
}

SignatureOptions 微信服务器验证参数

Jump to

Keyboard shortcuts

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