From 2e2708b3f56eaf05875a8c83813dd19e238f6213 Mon Sep 17 00:00:00 2001 From: Medivh Date: Sun, 1 Aug 2021 11:26:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=9A=E7=9B=91=E5=90=AC=E8=AE=A2=E9=98=85=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 42 ++++++++++++++++++++++++++++++++++---- auth.go | 4 ++-- server/event.go | 2 ++ server/server.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ server/types.go | 38 +++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index 4b8c4a6..457d10b 100644 --- a/Readme.md +++ b/Readme.md @@ -152,6 +152,7 @@ go get -u github.com/medivhzhan/weapp/v3 - [getPubTemplateTitleList](#getPubTemplateTitleList) ✅ - [getTemplateList](#getTemplateList) ✅ - [sendSubscribeMessage](#sendSubscribeMessage) ✅ + - [监听订阅消息事件](#监听订阅消息事件) ✅ - [解密](#解密) - [解密手机号码](#解密手机号码) ✅ - [解密分享内容](#解密分享内容) @@ -588,7 +589,7 @@ fmt.Printf("返回结果: %#v", res) ```go -import "github.com/medivhzhan/weapp/v3" +import "github.com/medivhzhan/weapp/v3/server" srv, err := server.NewServer("appid", "token", "aesKey", "mchID", "apiKey", false, nil) if err != nil { @@ -596,7 +597,7 @@ if err != nil { } // 文本消息 -srv.OnCustomerServiceTextMessage(func(msg *weapp.TextMessageResult) *weapp.TransferCustomerMessage { +srv.OnCustomerServiceTextMessage(func(msg *server.TextMessageResult) *weapp.TransferCustomerMessage { reply := cli.CSMsgText{ Content: "content", @@ -617,7 +618,7 @@ srv.OnCustomerServiceTextMessage(func(msg *weapp.TextMessageResult) *weapp.Trans }) // 图片消息 -srv.OnCustomerServiceImageMessage(func(msg *weapp.TextMessageResult) *weapp.TransferCustomerMessage { +srv.OnCustomerServiceImageMessage(func(msg *server.TextMessageResult) *weapp.TransferCustomerMessage { reply := cli.CSMsgImage{ MediaID: "media-id", @@ -638,7 +639,7 @@ srv.OnCustomerServiceImageMessage(func(msg *weapp.TextMessageResult) *weapp.Tran }) // 小程序卡片消息 -srv.OnCustomerServiceCardMessage(func(msg *weapp.TextMessageResult) *weapp.TransferCustomerMessage { +srv.OnCustomerServiceCardMessage(func(msg *server.TextMessageResult) *weapp.TransferCustomerMessage { reply := cli.CSMsgMPCard{ Title: "title", @@ -2988,6 +2989,39 @@ fmt.Printf("返回结果: %#v", res) ``` +### 监听订阅消息事件 + +[官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html) + +```go + +import "github.com/medivhzhan/weapp/v3/server" + +srv, err := server.NewServer("appid", "token", "aesKey", "mchID", "apiKey", false, nil) +if err != nil { + lof.Fatalf("init server error: %s", err) +} + +// 当用户触发订阅消息弹框后 +srv.OnSubscribeMsgPopup(func(msg *server.SubscribeMsgPopupEvent) { + + // Do something cool ... + return nil +}) + +// 当用户通过设置界面改变订阅消息事件内容 +srv.OnSubscribeMsgChange(func(msg *server.SubscribeMsgChangeEvent) { + // Do something cool ... + return nil +}) + +if err := srv.Serve(http.ResponseWriter, *http.Request); err != nil { + // 处理微信返回错误信息 + return +} + +``` + --- ## 解密 diff --git a/auth.go b/auth.go index fe77c0e..dfd2538 100644 --- a/auth.go +++ b/auth.go @@ -113,7 +113,7 @@ type GetPaidUnionIDResponse struct { UnionID string `json:"unionid"` } -// GetPaidUnionID 用户支付完成后,通过微信支付订单号(transaction_id)获取该用户的 UnionId, +// GetPaidUnionID 用户支付完成后,通过微信支付订单号(transaction_id)获取该用户的 UnionId func (cli *Client) GetPaidUnionID(openID, transactionID string) (*GetPaidUnionIDResponse, error) { api := baseURL + apiGetPaidUnionID accessToken, err := cli.AccessToken() @@ -133,7 +133,7 @@ func (cli *Client) getPaidUnionID(accessToken, openID, transactionID, api string return cli.getPaidUnionIDRequest(api, queries) } -// GetPaidUnionIDWithMCH 用户支付完成后,通过微信支付商户订单号和微信支付商户号(out_trade_no 及 mch_id)获取该用户的 UnionId, +// GetPaidUnionIDWithMCH 用户支付完成后,通过微信支付商户订单号和微信支付商户号(out_trade_no 及 mch_id)获取该用户的 UnionId func (cli *Client) GetPaidUnionIDWithMCH(openID, outTradeNo, mchID string) (*GetPaidUnionIDResponse, error) { api := baseURL + apiGetPaidUnionID diff --git a/server/event.go b/server/event.go index 893fcd1..50cf83b 100644 --- a/server/event.go +++ b/server/event.go @@ -27,4 +27,6 @@ const ( EventDeliveryOrderReadd EventType = "transport_readd_order" // 下单 EventPreAuthCodeGet EventType = "get_pre_auth_code" // 获取预授权码 EventRiderScoreSet EventType = "transport_set_rider_score" // 给骑手评分 + EventSubscribeMsgPopup EventType = "subscribe_msg_popup_event" // 订阅消息弹框事件 + EventSubscribeMsgChange EventType = "subscribe_msg_change_event" // 用户改变订阅消息事件 ) diff --git a/server/server.go b/server/server.go index bab5ae8..eebe72c 100644 --- a/server/server.go +++ b/server/server.go @@ -58,6 +58,8 @@ type Server struct { deliveryOrderReaddHandler func(*DeliveryOrderReaddResult) *DeliveryOrderReaddReturn preAuthCodeGetHandler func(*PreAuthCodeGetResult) *PreAuthCodeGetReturn riderScoreSetHandler func(*RiderScoreSetResult) *RiderScoreSetReturn + subscribeMsgPopup func(*SubscribeMsgPopupEvent) + subscribeMsgChange func(*SubscribeMsgChangeEvent) } // OnCustomerServiceTextMessage add handler to handle customer text service message. @@ -186,6 +188,16 @@ func (srv *Server) OnRiderScoreSet(fn func(*RiderScoreSetResult) *RiderScoreSetR srv.riderScoreSetHandler = fn } +// 当用户触发订阅消息弹框后 +func (srv *Server) OnSubscribeMsgPopup(fn func(*SubscribeMsgPopupEvent)) { + srv.subscribeMsgPopup = fn +} + +// 当用户通过设置界面改变订阅消息事件内容 +func (srv *Server) OnSubscribeMsgChange(fn func(*SubscribeMsgChangeEvent)) { + srv.subscribeMsgChange = fn +} + // NewServer 返回经过初始化的Server func NewServer(appID, token, aesKey, mchID, apiKey string, validate bool, handler func(map[string]interface{}) map[string]interface{}) (*Server, error) { @@ -319,6 +331,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.userTempsessionEnterHandler != nil { srv.userTempsessionEnterHandler(msg) } + case EventQuotaGet: msg := new(GetExpressQuotaResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -327,6 +340,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.quotaGetHandler != nil { return srv.quotaGetHandler(msg), nil } + case EventMediaCheckAsync: msg := new(MediaCheckAsyncResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -335,6 +349,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.mediaCheckAsyncHandler != nil { srv.mediaCheckAsyncHandler(msg) } + case EventAddExpressOrder: msg := new(AddExpressOrderResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -343,6 +358,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.addExpressOrderHandler != nil { return srv.addExpressOrderHandler(msg), nil } + case EventExpressOrderCancel: msg := new(CancelExpressOrderResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -351,6 +367,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.expressOrderCancelHandler != nil { return srv.expressOrderCancelHandler(msg), nil } + case EventCheckBusiness: msg := new(CheckExpressBusinessResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -359,6 +376,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.checkExpressBusinessHandler != nil { return srv.checkExpressBusinessHandler(msg), nil } + case EventDeliveryOrderStatusUpdate: msg := new(DeliveryOrderStatusUpdateResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -367,6 +385,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderStatusUpdateHandler != nil { return srv.deliveryOrderStatusUpdateHandler(msg), nil } + case EventAgentPosQuery: msg := new(AgentPosQueryResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -375,6 +394,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.agentPosQueryHandler != nil { return srv.agentPosQueryHandler(msg), nil } + case EventAuthInfoGet: msg := new(AuthInfoGetResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -383,6 +403,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.authInfoGetHandler != nil { return srv.authInfoGetHandler(msg), nil } + case EventAuthAccountCancel: msg := new(CancelAuthResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -391,6 +412,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.authAccountCancelHandler != nil { return srv.authAccountCancelHandler(msg), nil } + case EventDeliveryOrderAdd: msg := new(DeliveryOrderAddResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -399,6 +421,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderAddHandler != nil { return srv.deliveryOrderAddHandler(msg), nil } + case EventDeliveryOrderTipsAdd: msg := new(DeliveryOrderAddTipsResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -407,6 +430,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderTipsAddHandler != nil { return srv.deliveryOrderTipsAddHandler(msg), nil } + case EventDeliveryOrderCancel: msg := new(DeliveryOrderCancelResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -415,6 +439,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderCancelHandler != nil { return srv.deliveryOrderCancelHandler(msg), nil } + case EventDeliveryOrderReturnConfirm: msg := new(DeliveryOrderReturnConfirmResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -423,6 +448,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderReturnConfirmHandler != nil { return srv.deliveryOrderReturnConfirmHandler(msg), nil } + case EventDeliveryOrderPreAdd: msg := new(DeliveryOrderPreAddResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -431,6 +457,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderPreAddHandler != nil { return srv.deliveryOrderPreAddHandler(msg), nil } + case EventDeliveryOrderPreCancel: msg := new(DeliveryOrderPreCancelResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -439,6 +466,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderPreCancelHandler != nil { return srv.deliveryOrderPreCancelHandler(msg), nil } + case EventDeliveryOrderQuery: msg := new(DeliveryOrderQueryResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -447,6 +475,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderQueryHandler != nil { return srv.deliveryOrderQueryHandler(msg), nil } + case EventDeliveryOrderReadd: msg := new(DeliveryOrderReaddResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -455,6 +484,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.deliveryOrderReaddHandler != nil { return srv.deliveryOrderReaddHandler(msg), nil } + case EventPreAuthCodeGet: msg := new(PreAuthCodeGetResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -463,6 +493,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.preAuthCodeGetHandler != nil { return srv.preAuthCodeGetHandler(msg), nil } + case EventRiderScoreSet: msg := new(RiderScoreSetResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -471,6 +502,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.riderScoreSetHandler != nil { return srv.riderScoreSetHandler(msg), nil } + case EventExpressPathUpdate: msg := new(ExpressPathUpdateResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -479,6 +511,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.expressPathUpdateHandler != nil { srv.expressPathUpdateHandler(msg) } + case EventNearbyPoiAuditInfoAdd: msg := new(AddNearbyPoiResult) if err := unmarshal(raw, ctp, msg); err != nil { @@ -495,7 +528,26 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp if srv.handler != nil { return srv.handler(msg), nil } + + case EventSubscribeMsgPopup: + msg := new(SubscribeMsgPopupEvent) + if err := unmarshal(raw, ctp, msg); err != nil { + return nil, err + } + if srv.riderScoreSetHandler != nil { + srv.subscribeMsgPopup(msg) + } + + case EventSubscribeMsgChange: + msg := new(SubscribeMsgChangeEvent) + if err := unmarshal(raw, ctp, msg); err != nil { + return nil, err + } + if srv.riderScoreSetHandler != nil { + srv.subscribeMsgChange(msg) + } } + default: msg := make(map[string]interface{}) if err := unmarshal(raw, ctp, msg); err != nil { diff --git a/server/types.go b/server/types.go index 3c14f26..6d3f50e 100644 --- a/server/types.go +++ b/server/types.go @@ -658,3 +658,41 @@ type RiderScoreSetResult struct { CargoIntactScore uint `json:"cargo_intact_score" xml:"cargo_intact_score"` // 货物完整分数,范围1-5 AttitudeScore uint `json:"attitude_score" xml:"attitude_score"` // 服务态度分数 范围1-5 } + +// 订阅结果 +type SubscribeResult = string + +const ( + SubscribeResultAccept SubscribeResult = "accept" + SubscribeResultReject SubscribeResult = "reject" +) + +// 用户触发订阅消息弹框事件内容 +type SubscribeMsgPopupEvent struct { + CommonServerResult + SubscribeMsgPopupEvent []*UserSubscribedMsg `json:"SubscribeMsgPopupEvent" xml:"SubscribeMsgPopupEvent"` +} + +// 订阅的模板 +type UserSubscribedMsg struct { + // 模板id(一次订阅可能有多个id) + TemplateId string `json:"TemplateId" xml:"TemplateId"` + // 订阅结果(accept接收;reject拒收) + SubscribeStatusString string `json:"SubscribeStatusString" xml:"SubscribeStatusString"` + // 弹框场景,0代表在小程序页面内 + PopupScene string `json:"PopupScene" xml:"PopupScene"` +} + +// 用户改变订阅消息事件内容 +type SubscribeMsgChangeEvent struct { + CommonServerResult + SubscribeMsgChangeEvent []*UserChangesSubscribeMsg `json:"SubscribeMsgChangeEvent" xml:"SubscribeMsgChangeEvent"` +} + +// 订阅的模板 +type UserChangesSubscribeMsg struct { + // 模板id(一次订阅可能有多个id) + TemplateId string `json:"TemplateId" xml:"TemplateId"` + // 订阅结果(accept接收;reject拒收) + SubscribeStatusString string `json:"SubscribeStatusString" xml:"SubscribeStatusString"` +}