diff --git a/event.go b/event.go index 66abc0c..246543d 100644 --- a/event.go +++ b/event.go @@ -2,7 +2,6 @@ package weixin import ( "github.com/arstd/log" - "net/http" ) // EventType 事件类型 @@ -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: diff --git a/eventother.go b/eventother.go index 1592cac..08afeb5 100644 --- a/eventother.go +++ b/eventother.go @@ -1,7 +1,5 @@ package weixin -import "net/http" - // 微信支持的事件类型 const ( EventTypeSubscribe EventType = "subscribe" @@ -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, diff --git a/examples/eventhandler.go b/examples/eventhandler.go index 6a4e30e..7097cbd 100644 --- a/examples/eventhandler.go +++ b/examples/eventhandler.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net/http" "github.com/arstd/log" "github.com/jqiris/weixin" @@ -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 diff --git a/examples/msghandler.go b/examples/msghandler.go index 896e701..13ce58d 100644 --- a/examples/msghandler.go +++ b/examples/msghandler.go @@ -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 diff --git a/handler.go b/handler.go index 8dd4c06..f9fcc6b 100644 --- a/handler.go +++ b/handler.go @@ -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 @@ -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 } @@ -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 处理各类事件 @@ -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 {