msgpush

package
v0.0.0-...-7086fdf Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessTokenResp

type AccessTokenResp struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
	ErrCode     int    `json:"errcode"`
	ErrMsg      string `json:"errmsg"`
}

AccessTokenResp 微信 access_token 响应

type EventHandler

type EventHandler func(ctx context.Context, msg *Message) (string, error)

EventHandler 微信事件处理回调函数类型,返回可选的回复文本

type MPUserInfoResp

type MPUserInfoResp struct {
	Subscribe     int    `json:"subscribe"`
	OpenID        string `json:"openid"`
	Nickname      string `json:"nickname"`
	Sex           int    `json:"sex"`
	Language      string `json:"language"`
	City          string `json:"city"`
	Province      string `json:"province"`
	Country       string `json:"country"`
	HeadImgURL    string `json:"headimgurl"`
	SubscribeTime int64  `json:"subscribe_time"`
	UnionID       string `json:"unionid"`
	Remark        string `json:"remark"`
	GroupID       int    `json:"groupid"`
	ErrCode       int    `json:"errcode"`
	ErrMsg        string `json:"errmsg"`
}

MPUserInfoResp 公众号用户信息响应

type Message

type Message struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string   `xml:"ToUserName"`
	FromUserName string   `xml:"FromUserName"`
	CreateTime   int64    `xml:"CreateTime"`
	MsgType      string   `xml:"MsgType"`
	Event        string   `xml:"Event"`
	EventKey     string   `xml:"EventKey"`
	Ticket       string   `xml:"Ticket"`
	Content      string   `xml:"Content"`
	MsgID        int64    `xml:"MsgId"`
}

Message 微信推送的消息结构

type MsgCrypt

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

MsgCrypt 微信公众号安全模式消息加解密

func NewMsgCrypt

func NewMsgCrypt(token, encodingAESKey, appID string) (*MsgCrypt, error)

NewMsgCrypt 创建 MsgCrypt 实例 encodingAESKey 是微信后台配置的 43 字符 base64 编码密钥

func (*MsgCrypt) DecryptEchoStr

func (mc *MsgCrypt) DecryptEchoStr(msgSignature, timestamp, nonce, echostr string) (string, error)

DecryptEchoStr 解密 URL 验证的 echostr

func (*MsgCrypt) DecryptMessage

func (mc *MsgCrypt) DecryptMessage(msgSignature, timestamp, nonce string, body []byte) ([]byte, error)

DecryptMessage 验签并解密 POST body,返回明文 XML

func (*MsgCrypt) EncryptMessage

func (mc *MsgCrypt) EncryptMessage(plainXML []byte, timestamp, nonce string) ([]byte, error)

EncryptMessage 加密回复 XML,返回加密信封 XML

type QRCodeActionInfo

type QRCodeActionInfo struct {
	Scene QRCodeScene `json:"scene"`
}

QRCodeActionInfo 二维码动作信息

type QRCodeCreateReq

type QRCodeCreateReq struct {
	ExpireSeconds int              `json:"expire_seconds"`
	ActionName    string           `json:"action_name"`
	ActionInfo    QRCodeActionInfo `json:"action_info"`
}

QRCodeCreateReq 创建临时二维码请求

type QRCodeCreateResp

type QRCodeCreateResp struct {
	Ticket        string `json:"ticket"`
	ExpireSeconds int    `json:"expire_seconds"`
	URL           string `json:"url"`
	ErrCode       int    `json:"errcode"`
	ErrMsg        string `json:"errmsg"`
}

QRCodeCreateResp 创建临时二维码响应

type QRCodeScene

type QRCodeScene struct {
	SceneStr string `json:"scene_str,omitempty"`
	SceneID  int    `json:"scene_id,omitempty"`
}

QRCodeScene 二维码场景值

type ReplyMessage

type ReplyMessage struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string   `xml:"ToUserName"`
	FromUserName string   `xml:"FromUserName"`
	CreateTime   int64    `xml:"CreateTime"`
	MsgType      string   `xml:"MsgType"`
	Content      string   `xml:"Content,omitempty"`
}

ReplyMessage 微信回复消息结构

type TemplateMessage

type TemplateMessage struct {
	ToUser      string                         `json:"touser"`
	TemplateID  string                         `json:"template_id"`
	URL         string                         `json:"url,omitempty"`
	MiniProgram *TemplateMiniProgram           `json:"miniprogram,omitempty"`
	Data        map[string]TemplateMessageData `json:"data"`
}

TemplateMessage 模板消息请求体

type TemplateMessageData

type TemplateMessageData struct {
	Value string `json:"value"`
	Color string `json:"color,omitempty"`
}

TemplateMessageData 模板消息数据项

type TemplateMessageResp

type TemplateMessageResp struct {
	ErrCode int    `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
	MsgID   int64  `json:"msgid"`
}

TemplateMessageResp 模板消息发送响应

type TemplateMiniProgram

type TemplateMiniProgram struct {
	AppID    string `json:"appid"`
	PagePath string `json:"pagepath"`
}

TemplateMiniProgram 小程序跳转配置

type WechatClient

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

WechatClient 微信公众号客户端

func NewWechatClient

func NewWechatClient(cfg *config.Config, logger *slog.Logger, rdb *redis.Client) *WechatClient

NewWechatClient 创建微信公众号客户端。

redis 用于跨进程共享 access_token;同 binary 内不同 WechatClient 实例传入同一个 redis client,即可避免抢夺。详见 accessTokenKeyPrefix 注释。

func (*WechatClient) ClearAccessToken

func (c *WechatClient) ClearAccessToken()

ClearAccessToken 删除 Redis 共享缓存,强制下次重新拉取。 用于 token 过期被微信返回 40001 时主动清除。

func (*WechatClient) CreateQRCode

func (c *WechatClient) CreateQRCode(ctx context.Context, scene string, expireSeconds int) (*QRCodeCreateResp, error)

CreateQRCode 创建临时二维码

func (*WechatClient) DecryptEchoStr

func (c *WechatClient) DecryptEchoStr(msgSignature, timestamp, nonce, echostr string) (string, error)

DecryptEchoStr 解密 GET 校验回调中的 echostr。仅在 IsEncryptedMode 为 true 时调用。

func (*WechatClient) DecryptMessage

func (c *WechatClient) DecryptMessage(msgSignature, timestamp, nonce string, body []byte) ([]byte, error)

DecryptMessage 解密 POST 推送的消息体。仅在 IsEncryptedMode 为 true 时调用。

func (*WechatClient) EncryptMessage

func (c *WechatClient) EncryptMessage(reply []byte, timestamp, nonce string) ([]byte, error)

EncryptMessage 加密回复内容。仅在 IsEncryptedMode 为 true 时调用。

func (*WechatClient) GetAccessToken

func (c *WechatClient) GetAccessToken(ctx context.Context) (string, error)

GetAccessToken 获取微信 access_token。

流程:

  1. 先读 Redis 共享缓存,命中直接返回。
  2. 用 singleflight 合并同进程并发 refresh 请求。
  3. SETNX 跨进程锁;持锁者调微信 /token 并写回 Redis,其他实例轮询等待 (等不到就自己重试拿锁,避免持锁者崩溃后无限等待)。

func (*WechatClient) GetMPUserInfo

func (c *WechatClient) GetMPUserInfo(ctx context.Context, openID string) (*MPUserInfoResp, error)

GetMPUserInfo 获取公众号关注用户信息

func (*WechatClient) IsEncryptedMode

func (c *WechatClient) IsEncryptedMode(encryptType string) bool

IsEncryptedMode 当前是否启用安全模式(msgCrypt 已初始化且回调声明 encrypt_type=aes)。 handler 用它决定 GET/POST 走加密分支还是明文分支。

func (*WechatClient) ParseMessage

func (c *WechatClient) ParseMessage(body io.Reader) (*Message, error)

ParseMessage 解析微信推送的消息

func (*WechatClient) SendTemplateMessage

func (c *WechatClient) SendTemplateMessage(ctx context.Context, msg *TemplateMessage) error

SendTemplateMessage 发送模板消息

func (*WechatClient) VerifySignature

func (c *WechatClient) VerifySignature(signature, timestamp, nonce string) bool

VerifySignature 验证微信服务器签名

Jump to

Keyboard shortcuts

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