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

Compare commits

...

8 Commits

Author SHA1 Message Date
huangx
0505969439 add CommonToken and MiniProgramMixMessage (#323)
Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-29 18:50:48 +08:00
silenceper
3014901b48 补全 golint (#322) 2020-08-27 16:33:42 +08:00
huangx
2e191c0a44 feature: 小程序增加客服消息 (#319)
* add miniprogram customer_message

* fix golint comment

* rm unnecessary unmarshal in Send

Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-27 13:46:36 +08:00
huangx
4c35924b8c feature: 公众号客服消息增加「小程序卡片消息」类型;添加 GetCustomerMessageManager (#320)
* fix officialaccount customer_message

* fix golint comment

Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-25 19:41:53 +08:00
huangx
ed508654a1 bugfix officialaccount/message/template json non-pointer error (#318)
Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-17 17:30:04 +08:00
huangx
a18fe5b58a add miniprogram/subscribe listTemplates (#314)
Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-17 15:29:59 +08:00
huangx
a6eb2eedc3 check errcode in ListUserOpenIDs (#312)
Co-authored-by: huangxiang <huangxiang@didichuxing.com>
2020-08-11 09:46:59 +08:00
NaRro
404d522d09 feat: 增加获取第三方公众号授权链接的接口 (#310) 2020-08-08 16:57:31 +08:00
15 changed files with 290 additions and 8 deletions

View File

@@ -50,4 +50,12 @@ issues:
linters-settings:
funlen:
lines: 66
statements: 40
statements: 40
issues:
include:
- EXC0002 # disable excluding of issues about comments from golint
exclude-rules:
- linters:
- stylecheck
text: "ST1000:"

View File

@@ -1,3 +1,4 @@
//Package config 小程序config配置
package config
import (

View File

@@ -0,0 +1,57 @@
package message
import "encoding/xml"
// MsgType 基本消息类型
type MsgType string
// EventType 事件类型
type EventType string
// InfoType 第三方平台授权事件类型
type InfoType string
const (
//MsgTypeText 文本消息
MsgTypeText MsgType = "text"
//MsgTypeImage 图片消息
MsgTypeImage = "image"
//MsgTypeLink 图文链接
MsgTypeLink = "link"
//MsgTypeMiniProgramPage 小程序卡片
MsgTypeMiniProgramPage = "miniprogrampage"
)
// CommonToken 消息中通用的结构
type CommonToken struct {
XMLName xml.Name `xml:"xml"`
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType MsgType `xml:"MsgType"`
}
// MiniProgramMixMessage 小程序回调的消息结构
type MiniProgramMixMessage struct {
CommonToken
MsgID int64 `xml:"MsgId"`
// 文本消息
Content string `xml:"Content"`
// 图片消息
PicURL string `xml:"PicUrl"`
MediaID string `xml:"MediaId"`
// 小程序卡片消息
Title string `xml:"Title"`
AppID string `xml:"AppId"`
PagePath string `xml:"PagePath"`
ThumbURL string `xml:"ThumbUrl"`
ThumbMediaID string `xml:"ThumbMediaId"`
// 进入会话事件
Event string `xml:"Event"`
SessionFrom string `xml:"SessionFrom"`
}

View File

@@ -0,0 +1,124 @@
package message
import (
"fmt"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/util"
)
const (
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
)
//Manager 消息管理者,可以发送消息
type Manager struct {
*context.Context
}
//NewCustomerMessageManager 实例化消息管理者
func NewCustomerMessageManager(context *context.Context) *Manager {
return &Manager{
context,
}
}
//MediaText 文本消息的文字
type MediaText struct {
Content string `json:"content"`
}
//MediaResource 消息使用的临时素材id
type MediaResource struct {
MediaID string `json:"media_id"`
}
//MediaMiniprogrampage 小程序卡片
type MediaMiniprogrampage struct {
Title string `json:"title"`
Appid string `json:"appid"`
Pagepath string `json:"pagepath"`
ThumbMediaID string `json:"thumb_media_id"`
}
// MediaLink 发送图文链接
type MediaLink struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
ThumbURL string `json:"thumb_url"`
}
//CustomerMessage 客服消息
type CustomerMessage struct {
ToUser string `json:"touser"` //接受者OpenID
Msgtype MsgType `json:"msgtype"` //客服消息类型
Text *MediaText `json:"text,omitempty"` //可选
Image *MediaResource `json:"image,omitempty"` //可选
Link *MediaLink `json:"link,omitempty"` //可选
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
}
//NewCustomerTextMessage 文本消息结构体构造方法
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeText,
Text: &MediaText{
Content: text,
},
}
}
//NewCustomerImgMessage 图片消息的构造方法
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeImage,
Image: &MediaResource{
MediaID: mediaID,
},
}
}
//NewCustomerLinkMessage 图文链接消息的构造方法
func NewCustomerLinkMessage(toUser, title, description, url, thumbURL string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeLink,
Link: &MediaLink{
Title: title,
Description: description,
URL: url,
ThumbURL: thumbURL,
},
}
}
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
func NewCustomerMiniprogrampageMessage(toUser, title, pagepath, thumbMediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeMiniProgramPage,
Miniprogrampage: &MediaMiniprogrampage{
Title: title,
Pagepath: pagepath,
ThumbMediaID: thumbMediaID,
},
}
}
//Send 发送客服消息
func (manager *Manager) Send(msg *CustomerMessage) error {
accessToken, err := manager.Context.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
response, err := util.PostJSON(uri, msg)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "SendCustomerMessage")
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
"github.com/silenceper/wechat/v2/miniprogram/message"
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
"github.com/silenceper/wechat/v2/miniprogram/tcb"
@@ -66,3 +67,8 @@ func (miniProgram *MiniProgram) GetTcb() *tcb.Tcb {
func (miniProgram *MiniProgram) GetSubscribe() *subscribe.Subscribe {
return subscribe.NewSubscribe(miniProgram.ctx)
}
// GetCustomerMessage 客服消息接口
func (miniProgram *MiniProgram) GetCustomerMessage() *message.Manager {
return message.NewCustomerMessageManager(miniProgram.ctx)
}

View File

@@ -11,6 +11,10 @@ const (
//发送订阅消息
//https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
// 获取当前帐号下的个人模板列表
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
)
// Subscribe 订阅消息
@@ -38,6 +42,21 @@ type DataItem struct {
Value string `json:"value"`
}
//TemplateItem template item
type TemplateItem struct {
PriTmplID string `json:"priTmplId"`
Title string `json:"title"`
Content string `json:"content"`
Example string `json:"example"`
Type int64 `json:"type"`
}
//TemplateList template list
type TemplateList struct {
util.CommonError
Data []TemplateItem `json:"data"`
}
// Send 发送订阅消息
func (s *Subscribe) Send(msg *Message) (err error) {
var accessToken string
@@ -52,3 +71,23 @@ func (s *Subscribe) Send(msg *Message) (err error) {
}
return util.DecodeWithCommonError(response, "Send")
}
//ListTemplates 获取当前帐号下的个人模板列表
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
accessToken, err := s.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", getTemplateURL, accessToken)
response, err := util.HTTPGet(uri)
if err != nil {
return nil, err
}
templateList := TemplateList{}
err = util.DecodeWithError(response, &templateList, "ListTemplates")
if err != nil {
return nil, err
}
return &templateList, nil
}

View File

@@ -1,3 +1,4 @@
//Package device 设备相关接口
package device
import (

View File

@@ -73,6 +73,20 @@ func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
}
}
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
func NewCustomerMiniprogrampageMessage(toUser, title, appID, pagePath, thumbMediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeMiniprogrampage,
Miniprogrampage: &MediaMiniprogrampage{
Title: title,
AppID: appID,
Pagepath: pagePath,
ThumbMediaID: thumbMediaID,
},
}
}
//MediaText 文本消息的文字
type MediaText struct {
Content string `json:"content"`
@@ -134,7 +148,7 @@ type MediaWxcard struct {
//MediaMiniprogrampage 小程序消息
type MediaMiniprogrampage struct {
Title string `json:"title"`
Appid string `json:"appid"`
AppID string `json:"appid"`
Pagepath string `json:"pagepath"`
ThumbMediaID string `json:"thumb_media_id"`
}

View File

@@ -24,6 +24,8 @@ const (
MsgTypeVoice = "voice"
//MsgTypeVideo 表示视频消息
MsgTypeVideo = "video"
//MsgTypeMiniprogrampage 表示小程序卡片消息
MsgTypeMiniprogrampage = "miniprogrampage"
//MsgTypeShortVideo 表示短视频消息[限接收]
MsgTypeShortVideo = "shortvideo"
//MsgTypeLocation 表示坐标消息[限接收]

View File

@@ -106,7 +106,7 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) {
return
}
var res resTemplateList
err = util.DecodeWithError(response, res, "ListTemplate")
err = util.DecodeWithError(response, &res, "ListTemplate")
if err != nil {
return
}

View File

@@ -93,6 +93,11 @@ func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
return message.NewTemplate(officialAccount.ctx)
}
// GetCustomerMessageManager 客服消息接口
func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
return message.NewMessageManager(officialAccount.ctx)
}
// GetDevice 获取智能设备的实例
func (officialAccount *OfficialAccount) GetDevice() *device.Device {
return device.NewDevice(officialAccount.ctx)

View File

@@ -132,7 +132,7 @@ func (user *User) GetTag() (tags []*TagInfo, err error) {
return result.Tags, nil
}
//OpenidListByTag 获取标签下粉丝列表
//OpenIDListByTag 获取标签下粉丝列表
func (user *User) OpenIDListByTag(tagID int32, nextOpenID ...string) (userList *TagOpenIDList, err error) {
accessToken, err := user.GetAccessToken()
if err != nil {

View File

@@ -52,6 +52,8 @@ type Info struct {
// OpenidList 用户列表
type OpenidList struct {
util.CommonError
Total int `json:"total"`
Count int `json:"count"`
Data struct {
@@ -124,13 +126,14 @@ func (user *User) ListUserOpenIDs(nextOpenid ...string) (*OpenidList, error) {
return nil, err
}
userlist := new(OpenidList)
err = json.Unmarshal(response, userlist)
userlist := OpenidList{}
err = util.DecodeWithError(response, &userlist, "ListUserOpenIDs")
if err != nil {
return nil, err
}
return userlist, nil
return &userlist, nil
}
// ListAllUserOpenIDs 返回所有用户OpenID列表

View File

@@ -1,8 +1,10 @@
//Package context 开放平台相关context
package context
import (
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/silenceper/wechat/v2/util"
@@ -14,6 +16,8 @@ const (
queryAuthURL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s"
refreshTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s"
getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s"
componentLoginURL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%d&biz_appid=%s"
bindComponentURL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&auth_type=%d&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&biz_appid=%s#wechat_redirect"
//TODO 获取授权方选项信息
//getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
//TODO 获取已授权的账号信息
@@ -86,6 +90,24 @@ func (ctx *Context) GetPreCode() (string, error) {
return ret.PreCode, nil
}
// GetComponentLoginPage 获取第三方公众号授权链接(扫码授权)
func (ctx *Context) GetComponentLoginPage(redirectURI string, authType int, bizAppID string) (string, error) {
code, err := ctx.GetPreCode()
if err != nil {
return "", err
}
return fmt.Sprintf(componentLoginURL, ctx.AppID, code, url.QueryEscape(redirectURI), authType, bizAppID), nil
}
// GetBindComponentURL 获取第三方公众号授权链接(链接跳转,适用移动端)
func (ctx *Context) GetBindComponentURL(redirectURI string, authType int, bizAppID string) (string, error) {
code, err := ctx.GetPreCode()
if err != nil {
return "", err
}
return fmt.Sprintf(bindComponentURL, authType, ctx.AppID, code, url.QueryEscape(redirectURI), bizAppID), nil
}
// ID 微信返回接口中各种类型字段
type ID struct {
ID int `json:"id"`

View File

@@ -2,7 +2,7 @@ package util
import "time"
//GetCurrTs return current timestamps
//GetCurrTS return current timestamps
func GetCurrTS() int64 {
return time.Now().Unix()
}