Skip to content

Commit

Permalink
修改
Browse files Browse the repository at this point in the history
  • Loading branch information
yanjq committed May 23, 2019
1 parent 35a41d1 commit a0b926d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
7 changes: 3 additions & 4 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package weixin

import (
"github.com/arstd/log"
"net/http"
)

// EventType 事件类型
Expand All @@ -24,12 +23,12 @@ type EventBase struct {
}

// NewRecvEvent 把通用 struct 转化成相应类型的 struct
func NewRecvEvent(w http.ResponseWriter, r *http.Request,msg *Message) RecvEvent {
func NewRecvEvent(msg *Message) RecvEvent {
switch msg.Event {
case EventTypeSubscribe:
return NewEventSubscribe(w, r, msg)
return NewEventSubscribe(msg)
case EventTypeUnsubscribe:
return NewEventSubscribe(w, r, msg)
return NewEventSubscribe(msg)
case EventTypeLocation:
return NewEventLocation(msg)
case EventTypeClick:
Expand Down
4 changes: 1 addition & 3 deletions eventother.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package weixin

import "net/http"

// 微信支持的事件类型
const (
EventTypeSubscribe EventType = "subscribe"
Expand All @@ -27,7 +25,7 @@ type EventSubscribe struct {
}

// NewEventSubscribe 把通用 struct 转化成相应类型的 struct
func NewEventSubscribe(w http.ResponseWriter, r *http.Request, m *Message) *EventSubscribe {
func NewEventSubscribe(m *Message) *EventSubscribe {
return &EventSubscribe{
EventBase: EventBase{nil, m.ToUserName, m.FromUserName, m.CreateTime, m.MsgType, m.Event},
EventKey: m.EventKey,
Expand Down
3 changes: 2 additions & 1 deletion examples/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"

"github.com/arstd/log"
"github.com/jqiris/weixin"
Expand All @@ -24,7 +25,7 @@ func EventDefaultHandler(m *weixin.Message) weixin.ReplyMsg {
}

// EventSubscribeHandler 注册关注事件处理器
func EventSubscribeHandler(m *weixin.EventSubscribe) weixin.ReplyMsg {
func EventSubscribeHandler(w http.ResponseWriter, r *http.Request, m *weixin.EventSubscribe) weixin.ReplyMsg {
log.Debugf("%+v", m)

// echo message
Expand Down
6 changes: 2 additions & 4 deletions examples/msghandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package main

import (
"encoding/json"
"net/http"

"github.com/arstd/log"
"github.com/jqiris/weixin"
)

func defaultHandler(w http.ResponseWriter, r *http.Request, msg *weixin.Message) weixin.ReplyMsg {
func defaultHandler(msg *weixin.Message) weixin.ReplyMsg {
log.Debugf("%+v", msg)

event := weixin.NewRecvEvent(w, r, msg)
event := weixin.NewRecvEvent(msg)
js, _ := json.Marshal(event)

// echo message
Expand Down
10 changes: 5 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

// 各类事件处理器
var (
EventSubscribeHandler func(*EventSubscribe) ReplyMsg
EventSubscribeHandler func(w http.ResponseWriter, r *http.Request, sub *EventSubscribe) ReplyMsg
EventUnsubscribeHandler func(*EventSubscribe) ReplyMsg
EventLocationHandler func(*EventLocation) ReplyMsg
EventClickHandler func(*EventClick) ReplyMsg
Expand All @@ -41,7 +41,7 @@ var (
)

// RecvDefaultHandler 如果没有注册某类消息处理器,那么收到这类消息时,使用这个默认处理器
var RecvDefaultHandler = func(w http.ResponseWriter, r *http.Request, msg *Message) (reply ReplyMsg) {
var RecvDefaultHandler = func(msg *Message) (reply ReplyMsg) {
log.Debugf("unregistered receive message handler %s, use RecvDefaultHandler", msg.MsgType)
return nil
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func HandleMessage(w http.ResponseWriter, r *http.Request, msg *Message) (ret Re
return nil
}

return RecvDefaultHandler(w, r, msg)
return RecvDefaultHandler(msg)
}

// HandleEvent 处理各类事件
Expand All @@ -102,11 +102,11 @@ func HandleEvent(w http.ResponseWriter, r *http.Request, msg *Message) (reply Re
switch msg.Event {
case EventTypeSubscribe:
if EventSubscribeHandler != nil {
return EventSubscribeHandler(NewEventSubscribe(w,r, msg))
return EventSubscribeHandler(w,r, NewEventSubscribe(msg))
}
case EventTypeUnsubscribe:
if EventUnsubscribeHandler != nil {
return EventUnsubscribeHandler(NewEventSubscribe(w, r, msg))
return EventUnsubscribeHandler(NewEventSubscribe(msg))
}
case EventTypeLocation:
if EventLocationHandler != nil {
Expand Down

0 comments on commit a0b926d

Please sign in to comment.