1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

[feature] Format the code and improve Mini Program authorization to o… (#473)

* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session)

* [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html)

* upgrade json error

* upgrade json error

Co-authored-by: houseme <houseme@outlook.com>
This commit is contained in:
houseme
2021-09-08 11:03:23 +08:00
committed by GitHub
parent 47adf42208
commit 96c1f98944
90 changed files with 787 additions and 760 deletions

View File

@@ -12,35 +12,35 @@ const (
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
)
//Manager 消息管理者,可以发送消息
// Manager 消息管理者,可以发送消息
type Manager struct {
*context.Context
}
//NewMessageManager 实例化消息管理者
// NewMessageManager 实例化消息管理者
func NewMessageManager(context *context.Context) *Manager {
return &Manager{
context,
}
}
//CustomerMessage 客服消息
// CustomerMessage 客服消息
type CustomerMessage struct {
ToUser string `json:"touser"` //接受者OpenID
Msgtype MsgType `json:"msgtype"` //客服消息类型
Text *MediaText `json:"text,omitempty"` //可选
Image *MediaResource `json:"image,omitempty"` //可选
Voice *MediaResource `json:"voice,omitempty"` //可选
Video *MediaVideo `json:"video,omitempty"` //可选
Music *MediaMusic `json:"music,omitempty"` //可选
News *MediaNews `json:"news,omitempty"` //可选
Mpnews *MediaResource `json:"mpnews,omitempty"` //可选
Wxcard *MediaWxcard `json:"wxcard,omitempty"` //可选
Msgmenu *MediaMsgmenu `json:"msgmenu,omitempty"` //可选
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
ToUser string `json:"touser"` // 接受者OpenID
Msgtype MsgType `json:"msgtype"` // 客服消息类型
Text *MediaText `json:"text,omitempty"` // 可选
Image *MediaResource `json:"image,omitempty"` // 可选
Voice *MediaResource `json:"voice,omitempty"` // 可选
Video *MediaVideo `json:"video,omitempty"` // 可选
Music *MediaMusic `json:"music,omitempty"` // 可选
News *MediaNews `json:"news,omitempty"` // 可选
Mpnews *MediaResource `json:"mpnews,omitempty"` // 可选
Wxcard *MediaWxcard `json:"wxcard,omitempty"` // 可选
Msgmenu *MediaMsgmenu `json:"msgmenu,omitempty"` // 可选
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` // 可选
}
//NewCustomerTextMessage 文本消息结构体构造方法
// NewCustomerTextMessage 文本消息结构体构造方法
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
@@ -51,7 +51,7 @@ func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
}
}
//NewCustomerImgMessage 图片消息的构造方法
// NewCustomerImgMessage 图片消息的构造方法
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
@@ -62,7 +62,7 @@ func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
}
}
//NewCustomerVoiceMessage 语音消息的构造方法
// NewCustomerVoiceMessage 语音消息的构造方法
func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
@@ -73,7 +73,7 @@ func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
}
}
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
// NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
func NewCustomerMiniprogrampageMessage(toUser, title, appID, pagePath, thumbMediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
@@ -87,17 +87,17 @@ func NewCustomerMiniprogrampageMessage(toUser, title, appID, pagePath, thumbMedi
}
}
//MediaText 文本消息的文字
// MediaText 文本消息的文字
type MediaText struct {
Content string `json:"content"`
}
//MediaResource 消息使用的永久素材id
// MediaResource 消息使用的永久素材id
type MediaResource struct {
MediaID string `json:"media_id"`
}
//MediaVideo 视频消息包含的内容
// MediaVideo 视频消息包含的内容
type MediaVideo struct {
MediaID string `json:"media_id"`
ThumbMediaID string `json:"thumb_media_id"`
@@ -105,7 +105,7 @@ type MediaVideo struct {
Description string `json:"description"`
}
//MediaMusic 音乐消息包括的内容
// MediaMusic 音乐消息包括的内容
type MediaMusic struct {
Title string `json:"title"`
Description string `json:"description"`
@@ -114,12 +114,12 @@ type MediaMusic struct {
ThumbMediaID string `json:"thumb_media_id"`
}
//MediaNews 图文消息的内容
// MediaNews 图文消息的内容
type MediaNews struct {
Articles []MediaArticles `json:"articles"`
}
//MediaArticles 图文消息的内容的文章列表中的单独一条
// MediaArticles 图文消息的内容的文章列表中的单独一条
type MediaArticles struct {
Title string `json:"title"`
Description string `json:"description"`
@@ -127,25 +127,25 @@ type MediaArticles struct {
Picurl string `json:"picurl"`
}
//MediaMsgmenu 菜单消息的内容
// MediaMsgmenu 菜单消息的内容
type MediaMsgmenu struct {
HeadContent string `json:"head_content"`
List []MsgmenuItem `json:"list"`
TailContent string `json:"tail_content"`
}
//MsgmenuItem 菜单消息的菜单按钮
// MsgmenuItem 菜单消息的菜单按钮
type MsgmenuItem struct {
ID string `json:"id"`
Content string `json:"content"`
}
//MediaWxcard 卡券的id
// MediaWxcard 卡券的id
type MediaWxcard struct {
CardID string `json:"card_id"`
}
//MediaMiniprogrampage 小程序消息
// MediaMiniprogrampage 小程序消息
type MediaMiniprogrampage struct {
Title string `json:"title"`
AppID string `json:"appid"`
@@ -153,7 +153,7 @@ type MediaMiniprogrampage struct {
ThumbMediaID string `json:"thumb_media_id"`
}
//Send 发送客服消息
// Send 发送客服消息
func (manager *Manager) Send(msg *CustomerMessage) error {
accessToken, err := manager.Context.GetAccessToken()
if err != nil {

View File

@@ -1,6 +1,6 @@
package message
//Image 图片消息
// Image 图片消息
type Image struct {
CommonToken
@@ -9,7 +9,7 @@ type Image struct {
} `xml:"Image"`
}
//NewImage 回复图片消息
// NewImage 回复图片消息
func NewImage(mediaID string) *Image {
image := new(Image)
image.Image.MediaID = mediaID

View File

@@ -16,67 +16,67 @@ type EventType string
type InfoType string
const (
//MsgTypeText 表示文本消息
// MsgTypeText 表示文本消息
MsgTypeText MsgType = "text"
//MsgTypeImage 表示图片消息
// MsgTypeImage 表示图片消息
MsgTypeImage = "image"
//MsgTypeVoice 表示语音消息
// MsgTypeVoice 表示语音消息
MsgTypeVoice = "voice"
//MsgTypeVideo 表示视频消息
// MsgTypeVideo 表示视频消息
MsgTypeVideo = "video"
//MsgTypeMiniprogrampage 表示小程序卡片消息
// MsgTypeMiniprogrampage 表示小程序卡片消息
MsgTypeMiniprogrampage = "miniprogrampage"
//MsgTypeShortVideo 表示短视频消息[限接收]
// MsgTypeShortVideo 表示短视频消息[限接收]
MsgTypeShortVideo = "shortvideo"
//MsgTypeLocation 表示坐标消息[限接收]
// MsgTypeLocation 表示坐标消息[限接收]
MsgTypeLocation = "location"
//MsgTypeLink 表示链接消息[限接收]
// MsgTypeLink 表示链接消息[限接收]
MsgTypeLink = "link"
//MsgTypeMusic 表示音乐消息[限回复]
// MsgTypeMusic 表示音乐消息[限回复]
MsgTypeMusic = "music"
//MsgTypeNews 表示图文消息[限回复]
// MsgTypeNews 表示图文消息[限回复]
MsgTypeNews = "news"
//MsgTypeTransfer 表示消息消息转发到客服
// MsgTypeTransfer 表示消息消息转发到客服
MsgTypeTransfer = "transfer_customer_service"
//MsgTypeEvent 表示事件推送消息
// MsgTypeEvent 表示事件推送消息
MsgTypeEvent = "event"
)
const (
//EventSubscribe 订阅
// EventSubscribe 订阅
EventSubscribe EventType = "subscribe"
//EventUnsubscribe 取消订阅
// EventUnsubscribe 取消订阅
EventUnsubscribe = "unsubscribe"
//EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
// EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
EventScan = "SCAN"
//EventLocation 上报地理位置事件
// EventLocation 上报地理位置事件
EventLocation = "LOCATION"
//EventClick 点击菜单拉取消息时的事件推送
// EventClick 点击菜单拉取消息时的事件推送
EventClick = "CLICK"
//EventView 点击菜单跳转链接时的事件推送
// EventView 点击菜单跳转链接时的事件推送
EventView = "VIEW"
//EventScancodePush 扫码推事件的事件推送
// EventScancodePush 扫码推事件的事件推送
EventScancodePush = "scancode_push"
//EventScancodeWaitmsg 扫码推事件且弹出“消息接收中”提示框的事件推送
// EventScancodeWaitmsg 扫码推事件且弹出“消息接收中”提示框的事件推送
EventScancodeWaitmsg = "scancode_waitmsg"
//EventPicSysphoto 弹出系统拍照发图的事件推送
// EventPicSysphoto 弹出系统拍照发图的事件推送
EventPicSysphoto = "pic_sysphoto"
//EventPicPhotoOrAlbum 弹出拍照或者相册发图的事件推送
// EventPicPhotoOrAlbum 弹出拍照或者相册发图的事件推送
EventPicPhotoOrAlbum = "pic_photo_or_album"
//EventPicWeixin 弹出微信相册发图器的事件推送
// EventPicWeixin 弹出微信相册发图器的事件推送
EventPicWeixin = "pic_weixin"
//EventLocationSelect 弹出地理位置选择器的事件推送
// EventLocationSelect 弹出地理位置选择器的事件推送
EventLocationSelect = "location_select"
//EventTemplateSendJobFinish 发送模板消息推送通知
// EventTemplateSendJobFinish 发送模板消息推送通知
EventTemplateSendJobFinish = "TEMPLATESENDJOBFINISH"
//EventMassSendJobFinish 群发消息推送通知
// EventMassSendJobFinish 群发消息推送通知
EventMassSendJobFinish = "MASSSENDJOBFINISH"
//EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
// EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
EventWxaMediaCheck = "wxa_media_check"
)
const (
//微信开放平台需要用到
// 微信开放平台需要用到
// InfoTypeVerifyTicket 返回ticket
InfoTypeVerifyTicket InfoType = "component_verify_ticket"
@@ -88,13 +88,13 @@ const (
InfoTypeUpdateAuthorized = "updateauthorized"
)
//MixMessage 存放所有微信发送过来的消息和事件
// MixMessage 存放所有微信发送过来的消息和事件
type MixMessage struct {
CommonToken
//基本消息
MsgID int64 `xml:"MsgId"` //其他消息推送过来是MsgId
TemplateMsgID int64 `xml:"MsgID"` //模板消息推送成功的消息是MsgID
// 基本消息
MsgID int64 `xml:"MsgId"` // 其他消息推送过来是MsgId
TemplateMsgID int64 `xml:"MsgID"` // 模板消息推送成功的消息是MsgID
Content string `xml:"Content"`
Recognition string `xml:"Recognition"`
PicURL string `xml:"PicUrl"`
@@ -109,7 +109,7 @@ type MixMessage struct {
Description string `xml:"Description"`
URL string `xml:"Url"`
//事件相关
// 事件相关
Event EventType `xml:"Event"`
EventKey string `xml:"EventKey"`
Ticket string `xml:"Ticket"`
@@ -168,23 +168,23 @@ type MixMessage struct {
TraceID string `xml:"trace_id"`
StatusCode int `xml:"status_code"`
//设备相关
// 设备相关
device.MsgDevice
}
//EventPic 发图事件推送
// EventPic 发图事件推送
type EventPic struct {
PicMd5Sum string `xml:"PicMd5Sum"`
}
//EncryptedXMLMsg 安全模式下的消息体
// EncryptedXMLMsg 安全模式下的消息体
type EncryptedXMLMsg struct {
XMLName struct{} `xml:"xml" json:"-"`
ToUserName string `xml:"ToUserName" json:"ToUserName"`
EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
}
//ResponseEncryptedXMLMsg 需要返回的消息体
// ResponseEncryptedXMLMsg 需要返回的消息体
type ResponseEncryptedXMLMsg struct {
XMLName struct{} `xml:"xml" json:"-"`
EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
@@ -212,27 +212,27 @@ type CommonToken struct {
MsgType MsgType `xml:"MsgType"`
}
//SetToUserName set ToUserName
// SetToUserName set ToUserName
func (msg *CommonToken) SetToUserName(toUserName CDATA) {
msg.ToUserName = toUserName
}
//SetFromUserName set FromUserName
// SetFromUserName set FromUserName
func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
msg.FromUserName = fromUserName
}
//SetCreateTime set createTime
// SetCreateTime set createTime
func (msg *CommonToken) SetCreateTime(createTime int64) {
msg.CreateTime = createTime
}
//SetMsgType set MsgType
// SetMsgType set MsgType
func (msg *CommonToken) SetMsgType(msgType MsgType) {
msg.MsgType = msgType
}
//GetOpenID get the FromUserName value
// GetOpenID get the FromUserName value
func (msg *CommonToken) GetOpenID() string {
return string(msg.FromUserName)
}

View File

@@ -1,6 +1,6 @@
package message
//Music 音乐消息
// Music 音乐消息
type Music struct {
CommonToken
@@ -13,7 +13,7 @@ type Music struct {
} `xml:"Music"`
}
//NewMusic 回复音乐消息
// NewMusic 回复音乐消息
func NewMusic(title, description, musicURL, hQMusicURL, thumbMediaID string) *Music {
music := new(Music)
music.Music.Title = title

View File

@@ -1,6 +1,6 @@
package message
//News 图文消息
// News 图文消息
type News struct {
CommonToken
@@ -8,7 +8,7 @@ type News struct {
Articles []*Article `xml:"Articles>item,omitempty"`
}
//NewNews 初始化图文消息
// NewNews 初始化图文消息
func NewNews(articles []*Article) *News {
news := new(News)
news.ArticleCount = len(articles)
@@ -16,7 +16,7 @@ func NewNews(articles []*Article) *News {
return news
}
//Article 单篇文章
// Article 单篇文章
type Article struct {
Title string `xml:"Title,omitempty"`
Description string `xml:"Description,omitempty"`
@@ -24,7 +24,7 @@ type Article struct {
URL string `xml:"Url,omitempty"`
}
//NewArticle 初始化文章
// NewArticle 初始化文章
func NewArticle(title, description, picURL, url string) *Article {
article := new(Article)
article.Title = title

View File

@@ -2,13 +2,13 @@ package message
import "errors"
//ErrInvalidReply 无效的回复
// ErrInvalidReply 无效的回复
var ErrInvalidReply = errors.New("无效的回复消息")
//ErrUnsupportReply 不支持的回复类型
// ErrUnsupportReply 不支持的回复类型
var ErrUnsupportReply = errors.New("不支持的回复消息")
//Reply 消息回复
// Reply 消息回复
type Reply struct {
MsgType MsgType
MsgData interface{}

View File

@@ -14,36 +14,36 @@ const (
subscribeTemplateDelURL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate"
)
//Subscribe 订阅消息
// Subscribe 订阅消息
type Subscribe struct {
*context.Context
}
//NewSubscribe 实例化
// NewSubscribe 实例化
func NewSubscribe(context *context.Context) *Subscribe {
tpl := new(Subscribe)
tpl.Context = context
return tpl
}
//SubscribeMessage 发送的订阅消息内容
// SubscribeMessage 发送的订阅消息内容
type SubscribeMessage struct {
ToUser string `json:"touser"` // 必须, 接受者OpenID
TemplateID string `json:"template_id"` // 必须, 模版ID
Page string `json:"page,omitempty"` // 可选, 跳转网页时填写
Data map[string]*SubscribeDataItem `json:"data"` // 必须, 模板数据
MiniProgram struct {
AppID string `json:"appid"` //所需跳转到的小程序appid该小程序appid必须与发模板消息的公众号是绑定关联关系
PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,示例index?foo=bar
} `json:"miniprogram"` //可选,跳转至小程序地址
AppID string `json:"appid"` // 所需跳转到的小程序appid该小程序appid必须与发模板消息的公众号是绑定关联关系
PagePath string `json:"pagepath"` // 所需跳转到小程序的具体页面路径,支持带参数,示例index?foo=bar
} `json:"miniprogram"` // 可选,跳转至小程序地址
}
//SubscribeDataItem 模版内某个 .DATA 的值
// SubscribeDataItem 模版内某个 .DATA 的值
type SubscribeDataItem struct {
Value string `json:"value"`
}
//Send 发送订阅消息
// Send 发送订阅消息
func (tpl *Subscribe) Send(msg *SubscribeMessage) (err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
@@ -61,10 +61,10 @@ func (tpl *Subscribe) Send(msg *SubscribeMessage) (err error) {
// PrivateSubscribeItem 私有订阅消息模板
type PrivateSubscribeItem struct {
PriTmplID string `json:"priTmplId"` // 添加至帐号下的模板 id发送订阅通知时所需
Title string `json:"title"` //模版标题
Content string `json:"content"` //模版内容
Example string `json:"example"` //模板内容示例
SubType int `json:"type"` //模版类型2 为一次性订阅3 为长期订阅
Title string `json:"title"` // 模版标题
Content string `json:"content"` // 模版内容
Example string `json:"example"` // 模板内容示例
SubType int `json:"type"` // 模版类型2 为一次性订阅3 为长期订阅
}
type resPrivateSubscribeList struct {
@@ -72,7 +72,7 @@ type resPrivateSubscribeList struct {
SubscriptionList []*PrivateSubscribeItem `json:"data"`
}
//List 获取私有订阅消息模板列表
// List 获取私有订阅消息模板列表
func (tpl *Subscribe) List() (templateList []*PrivateSubscribeItem, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()

View File

@@ -15,19 +15,19 @@ const (
templateDelURL = "https://api.weixin.qq.com/cgi-bin/template/del_private_template"
)
//Template 模板消息
// Template 模板消息
type Template struct {
*context.Context
}
//NewTemplate 实例化
// NewTemplate 实例化
func NewTemplate(context *context.Context) *Template {
tpl := new(Template)
tpl.Context = context
return tpl
}
//TemplateMessage 发送的模板消息内容
// TemplateMessage 发送的模板消息内容
type TemplateMessage struct {
ToUser string `json:"touser"` // 必须, 接受者OpenID
TemplateID string `json:"template_id"` // 必须, 模版ID
@@ -36,12 +36,12 @@ type TemplateMessage struct {
Data map[string]*TemplateDataItem `json:"data"` // 必须, 模板数据
MiniProgram struct {
AppID string `json:"appid"` //所需跳转到的小程序appid该小程序appid必须与发模板消息的公众号是绑定关联关系
PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,示例index?foo=bar
} `json:"miniprogram"` //可选,跳转至小程序地址
AppID string `json:"appid"` // 所需跳转到的小程序appid该小程序appid必须与发模板消息的公众号是绑定关联关系
PagePath string `json:"pagepath"` // 所需跳转到小程序的具体页面路径,支持带参数,示例index?foo=bar
} `json:"miniprogram"` // 可选,跳转至小程序地址
}
//TemplateDataItem 模版内某个 .DATA 的值
// TemplateDataItem 模版内某个 .DATA 的值
type TemplateDataItem struct {
Value string `json:"value"`
Color string `json:"color,omitempty"`
@@ -53,7 +53,7 @@ type resTemplateSend struct {
MsgID int64 `json:"msgid"`
}
//Send 发送模板消息
// Send 发送模板消息
func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
@@ -95,7 +95,7 @@ type resTemplateList struct {
TemplateList []*TemplateItem `json:"template_list"`
}
//List 获取模板列表
// List 获取模板列表
func (tpl *Template) List() (templateList []*TemplateItem, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()

View File

@@ -1,12 +1,12 @@
package message
//Text 文本消息
// Text 文本消息
type Text struct {
CommonToken
Content CDATA `xml:"Content"`
}
//NewText 初始化文本消息
// NewText 初始化文本消息
func NewText(content string) *Text {
text := new(Text)
text.Content = CDATA(content)

View File

@@ -1,18 +1,18 @@
package message
//TransferCustomer 转发客服消息
// TransferCustomer 转发客服消息
type TransferCustomer struct {
CommonToken
TransInfo *TransInfo `xml:"TransInfo,omitempty"`
}
//TransInfo 转发到指定客服
// TransInfo 转发到指定客服
type TransInfo struct {
KfAccount string `xml:"KfAccount"`
}
//NewTransferCustomer 实例化
// NewTransferCustomer 实例化
func NewTransferCustomer(kfAccount string) *TransferCustomer {
tc := new(TransferCustomer)
if kfAccount != "" {

View File

@@ -1,6 +1,6 @@
package message
//Video 视频消息
// Video 视频消息
type Video struct {
CommonToken
@@ -11,7 +11,7 @@ type Video struct {
} `xml:"Video"`
}
//NewVideo 回复图片消息
// NewVideo 回复图片消息
func NewVideo(mediaID, title, description string) *Video {
video := new(Video)
video.Video.MediaID = mediaID

View File

@@ -1,6 +1,6 @@
package message
//Voice 语音消息
// Voice 语音消息
type Voice struct {
CommonToken
@@ -9,7 +9,7 @@ type Voice struct {
} `xml:"Voice"`
}
//NewVoice 回复语音消息
// NewVoice 回复语音消息
func NewVoice(mediaID string) *Voice {
voice := new(Voice)
voice.Voice.MediaID = mediaID