Skip to content

Commit

Permalink
增加新功能:监听订阅消息事件
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Aug 1, 2021
1 parent 068867e commit 2e2708b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
42 changes: 38 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ go get -u github.com/medivhzhan/weapp/v3
- [getPubTemplateTitleList](#getPubTemplateTitleList)
- [getTemplateList](#getTemplateList)
- [sendSubscribeMessage](#sendSubscribeMessage)
- [监听订阅消息事件](#监听订阅消息事件)
- [解密](#解密)
- [解密手机号码](#解密手机号码)
- [解密分享内容](#解密分享内容)
Expand Down Expand Up @@ -588,15 +589,15 @@ 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 {
lof.Fatalf("init server error: %s", err)
}

// 文本消息
srv.OnCustomerServiceTextMessage(func(msg *weapp.TextMessageResult) *weapp.TransferCustomerMessage {
srv.OnCustomerServiceTextMessage(func(msg *server.TextMessageResult) *weapp.TransferCustomerMessage {

reply := cli.CSMsgText{
Content: "content",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
}

```

---

## 解密
Expand Down
4 changes: 2 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions server/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" // 用户改变订阅消息事件
)
52 changes: 52 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 2e2708b

Please sign in to comment.