weixin_api

package module
v0.0.0-...-7d2e9ce Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 14 Imported by: 0

README

weixin_api

微信公众号、小程序api

Documentation

Overview

用户管理

Index

Constants

View Source
const (
	MsgTypeText       = "text"       // 文本消息
	MsgTypeImage      = "image"      // 图片消息
	MsgTypeVoice      = "voice"      // 语音消息
	MsgTypeVideo      = "video"      // 视频消息
	MsgTypeShortVideo = "shortvideo" // 小视频消息
	MsgTypeLocation   = "location"   // 地理位置消息
	MsgTypeLink       = "link"       // 链接消息
	MsgTypeEvent      = "event"      // 事件推送
)

微信支持的消息类型

View Source
const (
	EventTypeClick       = "CLICK"       // 自定义菜单事件
	EventTypeView        = "VIEW"        // 点菜单跳转链接
	EventTypeLocation    = "LOCATION"    // 上报地理位置
	EventTypeScan        = "SCAN"        // 用户已关注
	EventTypeSubscribe   = "subscribe"   // 用户未关注
	EventTypeUnsubscribe = "unsubscribe" // 取消订阅

)

Variables

View Source
var (
	ErrInvalidHandler  = errors.New("未注册消息处理函数")
	ErrInvalidXMLToken = errors.New("xml解析token出错")
)
View Source
var ErrRepoLocked = errors.New("Repository is already locked")
View Source
var ErrTokenInvalid = errors.New("AccessToken is invalid")

Functions

func DecodeRawMessage

func DecodeRawMessage[T any](data []byte) (*T, error)

func HttpGet

func HttpGet[T any](url string) (*T, error)

func HttpGetRaw

func HttpGetRaw(url string) ([]byte, error)

func PostJSON

func PostJSON[T any](url string, body interface{}) (*T, error)

func PostJSONReturnRaw

func PostJSONReturnRaw(url string, body interface{}) ([]byte, error)

func ValidateSignature

func ValidateSignature(tok, timestamp, nonce, signature string) bool

验证签名是否合法

Types

type BaseEvent

type BaseEvent struct {
	BaseMessage
	Event string // 事件类型,subscribe(订阅)、unsubscribe(取消订阅)
}

type BaseMessage

type BaseMessage struct {
	ToUserName   string // 开发者微信号
	FromUserName string // 发送方帐号(一个OpenID)
	CreateTime   int64  // 消息创建时间 (整型)
	MsgType      string // 消息类型
	MsgId        int64  // 消息id,64位整型
}

type ClickEvent

type ClickEvent struct {
	BaseEvent
	EventKey string // 事件KEY值,与自定义菜单接口中KEY值对应
}

自定义菜单事件 事件类型,CLICK

type ClickEventHandler

type ClickEventHandler func(m *ClickEvent) error

type Engine

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

func New

func New(cfg *WeiXinApiConfig) *Engine

func (*Engine) Code2Session

func (e *Engine) Code2Session(jscode string) (*SessionInfo, error)

func (*Engine) CreateMenu

func (e *Engine) CreateMenu(menu []byte) (*ErrorMsg, error)

func (*Engine) GetAccessToken

func (e *Engine) GetAccessToken() (string, error)

func (*Engine) GetCurrentSelfMenuInfo

func (e *Engine) GetCurrentSelfMenuInfo() error

func (*Engine) GetPhoneNumber

func (e *Engine) GetPhoneNumber(code string) (*PhoneInfo, error)

func (*Engine) GetUserInfo

func (e *Engine) GetUserInfo() (*UserInfo, error)

func (*Engine) GrantAccessToken

func (e *Engine) GrantAccessToken() error

从微信服务器获取Access Token,并保存到repository里面,后续调用GetAccessToken时,再从repository里面获取

func (*Engine) HandleMessage

func (e *Engine) HandleMessage(c context.Context, data []byte) error

func (*Engine) RegClickEventHandler

func (e *Engine) RegClickEventHandler(h ClickEventHandler)

func (*Engine) RegImageMessageHandler

func (e *Engine) RegImageMessageHandler(h ImageMessageHandler)

func (*Engine) RegLinkMessageHandler

func (e *Engine) RegLinkMessageHandler(h LinkMessageHandler)

func (*Engine) RegLocationEventHandler

func (e *Engine) RegLocationEventHandler(h LocationEventHandler)

func (*Engine) RegLocationMessageHandler

func (e *Engine) RegLocationMessageHandler(h LocationMessageHandler)

func (*Engine) RegScanEventHandler

func (e *Engine) RegScanEventHandler(h ScanEventHandler)

func (*Engine) RegSubscribeEventHandler

func (e *Engine) RegSubscribeEventHandler(h SubscribeEventHandler)

func (*Engine) RegTextMessageHandler

func (e *Engine) RegTextMessageHandler(h TextMessageHandler)

func (*Engine) RegUnsubscribeEventHandler

func (e *Engine) RegUnsubscribeEventHandler(h UnsubscribeEventHandler)

func (*Engine) RegVideoMessageHandler

func (e *Engine) RegVideoMessageHandler(h VideoMessageHandler)

func (*Engine) RegViewEventHandler

func (e *Engine) RegViewEventHandler(h ViewEventHandler)

func (*Engine) RegVoiceMessageHandler

func (e *Engine) RegVoiceMessageHandler(h VoiceMessageHandler)

func (*Engine) ValidateSignature

func (e *Engine) ValidateSignature(timestamp, nonce, signature string) bool

验证签名是否合法

type ErrInvalidEventType

type ErrInvalidEventType struct {
	Type string
}

func (*ErrInvalidEventType) Error

func (err *ErrInvalidEventType) Error() string

type ErrInvalidMessageType

type ErrInvalidMessageType struct {
	Type string
}

func (*ErrInvalidMessageType) Error

func (err *ErrInvalidMessageType) Error() string

type ErrorMsg

type ErrorMsg struct {
	ErrCode int32  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

func (*ErrorMsg) Error

func (err *ErrorMsg) Error() string

type IEngine

type IEngine interface {
	GetAccessToken() (string, error)
}

type IRepository

type IRepository interface {
	GetAccessToken(ctx context.Context) (string, time.Time, error)
	UpdateAccessToken(ctx context.Context, tok string, expiredTime time.Time) error
	Lock() error // 上锁,并返回 true表示上锁成功
	UnLock()
}

type ImageMessage

type ImageMessage struct {
	BaseMessage
	PicUrl  string // 图片链接(由系统生成)
	MediaId string // 图片消息媒体id,可以调用获取临时素材接口拉取数据。
}

ImageMessage 图片消息 图片为image

type ImageMessageHandler

type ImageMessageHandler func(m *ImageMessage) error

type LinkMessage

type LinkMessage struct {
	BaseMessage
	Title       string // 消息标题
	Description string // 消息描述
	Url         string // 消息链接
}

LinkMessage 链接消息,链接为link

type LinkMessageHandler

type LinkMessageHandler func(m *LinkMessage) error

type LocationEvent

type LocationEvent struct {
	BaseEvent
	Latitude  float32 //地理位置纬度
	Longitude float32 //地理位置经度
	Precision float32 //地理位置精度
}

上报地理位置事件 事件类型,LOCATION

type LocationEventHandler

type LocationEventHandler func(m *LocationEvent) error

type LocationMessage

type LocationMessage struct {
	BaseMessage
	LocationX float64 `xml:"Location_X"` // 地理位置纬度
	LocationY float64 `xml:"Location_Y"` // 地理位置经度
	Scale     int     // 地图缩放大小
	Label     string  // 地理位置信息
}

LocationMessage 地理位置消息,地理位置为location

type LocationMessageHandler

type LocationMessageHandler func(m *LocationMessage) error

type PhoneInfo

type PhoneInfo struct {
	PhoneNumber     string
	PurePhoneNumber string
	CountryCode     string
	Watermark       Watermark
}

type QRCodeInfo

type QRCodeInfo struct {
	ErrorMsg
	Ticket    string `json:"ticket"`
	ExpiresIn int32  `json:"expire_seconds"`
	URL       string `json:"url"`
}

func CreateLimitQRCode

func CreateLimitQRCode(e IEngine, id int32) (*QRCodeInfo, error)

func CreateLimitQRCodeByStr

func CreateLimitQRCodeByStr(e IEngine, id string) (*QRCodeInfo, error)

func CreateQRCode

func CreateQRCode(e IEngine, id int32, expireSeconds int32) (*QRCodeInfo, error)

func CreateQRCodeByStr

func CreateQRCodeByStr(e IEngine, id string, expireSeconds int32) (*QRCodeInfo, error)

type ScanEvent

type ScanEvent struct {
	BaseEvent
	EventKey string // 事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
	Ticket   string //二维码的ticket,可用来换取二维码图片
}

用户已关注时的事件推送

type ScanEventHandler

type ScanEventHandler func(m *ScanEvent) error

type SessionInfo

type SessionInfo struct {
	ErrorMsg
	OpenId     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionId    string `json:"unionid"`
}

type SubscribeEvent

type SubscribeEvent struct {
	BaseEvent
	EventKey string // 事件KEY值,qrscene_为前缀,后面为二维码的参数值
	Ticket   string // 二维码的ticket,可用来换取二维码图片
}

事件类型,subscribe 事件类型,unsubscribe(取消订阅)

type SubscribeEventHandler

type SubscribeEventHandler func(m *SubscribeEvent) error

type TextMessage

type TextMessage struct {
	BaseMessage
	Content string // 文本消息内容
}

文本消息 文本为text

type TextMessageHandler

type TextMessageHandler func(m *TextMessage) error

type UnsubscribeEvent

type UnsubscribeEvent struct {
	BaseEvent
	EventKey string // 事件KEY值,qrscene_为前缀,后面为二维码的参数值
	Ticket   string // 二维码的ticket,可用来换取二维码图片
}

事件类型,unsubscribe 事件类型,unsubscribe(取消订阅)

type UnsubscribeEventHandler

type UnsubscribeEventHandler func(m *UnsubscribeEvent) error

type UserInfo

type UserInfo struct {
}

type VideoMessage

type VideoMessage struct {
	BaseMessage
	MediaId      string // 视频消息媒体id,可以调用获取临时素材接口拉取数据。
	ThumbMediaId string // 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
}

VideoMessage 视频/小视频消息 视频为video,小视频为shortvideo

type VideoMessageHandler

type VideoMessageHandler func(m *VideoMessage) error

type ViewEvent

type ViewEvent struct {
	BaseEvent
	EventKey string // 事件KEY值,设置的跳转URL

}

点击菜单跳转链接时的事件推送 事件类型,VIEW

type ViewEventHandler

type ViewEventHandler func(m *ViewEvent) error

type VoiceMessage

type VoiceMessage struct {
	BaseMessage
	MediaId      string // 语音消息媒体id,可以调用获取临时素材接口拉取数据。
	Format       string // 语音格式,如amr,speex等
	Recongnition string // 消息id,64位整型
}

VoiceMessage 语音消息 语音为voice

type VoiceMessageHandler

type VoiceMessageHandler func(m *VoiceMessage) error

type Watermark

type Watermark struct {
	AppId     string `json:"appid"`     // 应用的appid
	Timestamp int64  `json:"timestamp"` // 操作的时间戳
}

type WeiXinApiConfig

type WeiXinApiConfig struct {
	AppId        string
	AppSecret    string
	AppToken     string
	WeiXinDomain string
	Repository   IRepository
	// AccessToken            string
	HandleTextMessage      func(m *TextMessage) error
	HandleImageMessage     func(m *ImageMessage) error
	HandleVoiceMessage     func(m *VoiceMessage) error
	HandleVideoMessage     func(m *VideoMessage) error
	HandleLocationMessage  func(m *LocationMessage) error
	HandleLinkMessage      func(m *LinkMessage) error
	HandleClickEvent       func(m *ClickEvent) error
	HandleLocationEvent    func(m *LocationEvent) error
	HandleViewEvent        func(m *ViewEvent) error
	HandleScanEvent        func(m *ScanEvent) error
	HandleSubscribeEvent   func(m *SubscribeEvent) error
	HandleUnsubscribeEvent func(m *UnsubscribeEvent) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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