|
|
@@ -2,8 +2,12 @@ package server
|
|
|
|
|
|
import (
|
|
|
"encoding/xml"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
+ "reflect"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
|
|
|
"github.com/silenceper/wechat/context"
|
|
|
"github.com/silenceper/wechat/message"
|
|
|
@@ -13,8 +17,20 @@ import (
|
|
|
//Server struct
|
|
|
type Server struct {
|
|
|
*context.Context
|
|
|
+
|
|
|
+ openID string
|
|
|
+
|
|
|
+ messageHandler func(message.MixMessage) *message.Reply
|
|
|
+
|
|
|
+ requestRawXMLMsg []byte
|
|
|
+ requestMsg message.MixMessage
|
|
|
+ responseRawXMLMsg []byte
|
|
|
+ responseMsg interface{}
|
|
|
+
|
|
|
isSafeMode bool
|
|
|
- rawXMLMsg string
|
|
|
+ random []byte
|
|
|
+ nonce string
|
|
|
+ timestamp int64
|
|
|
}
|
|
|
|
|
|
//NewServer init
|
|
|
@@ -26,18 +42,22 @@ func NewServer(context *context.Context) *Server {
|
|
|
|
|
|
//Serve 处理微信的请求消息
|
|
|
func (srv *Server) Serve() error {
|
|
|
-
|
|
|
if !srv.Validate() {
|
|
|
return fmt.Errorf("请求校验失败")
|
|
|
}
|
|
|
|
|
|
echostr, exists := srv.GetQuery("echostr")
|
|
|
if exists {
|
|
|
- return srv.String(echostr)
|
|
|
+ srv.String(echostr)
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
- srv.handleRequest()
|
|
|
+ response, err := srv.handleRequest()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
+ srv.buildResponse(response)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
@@ -50,19 +70,37 @@ func (srv *Server) Validate() bool {
|
|
|
}
|
|
|
|
|
|
//HandleRequest 处理微信的请求
|
|
|
-func (srv *Server) handleRequest() {
|
|
|
+func (srv *Server) handleRequest() (reply *message.Reply, err error) {
|
|
|
+ //set isSafeMode
|
|
|
srv.isSafeMode = false
|
|
|
encryptType := srv.Query("encrypt_type")
|
|
|
if encryptType == "aes" {
|
|
|
srv.isSafeMode = true
|
|
|
}
|
|
|
|
|
|
- _, err := srv.getMessage()
|
|
|
+ //set openID
|
|
|
+ srv.openID = srv.Query("openid")
|
|
|
+
|
|
|
+ var msg interface{}
|
|
|
+ msg, err = srv.getMessage()
|
|
|
if err != nil {
|
|
|
- fmt.Printf("%v", err)
|
|
|
+ return
|
|
|
}
|
|
|
+ mixMessage, success := msg.(message.MixMessage)
|
|
|
+ if !success {
|
|
|
+ err = errors.New("消息类型转换失败")
|
|
|
+ }
|
|
|
+ srv.requestMsg = mixMessage
|
|
|
+ reply = srv.messageHandler(mixMessage)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
+//GetOpenID return openID
|
|
|
+func (srv *Server) GetOpenID() string {
|
|
|
+ return srv.openID
|
|
|
+}
|
|
|
+
|
|
|
+//getMessage 解析微信返回的消息
|
|
|
func (srv *Server) getMessage() (interface{}, error) {
|
|
|
var rawXMLMsgBytes []byte
|
|
|
var err error
|
|
|
@@ -74,26 +112,117 @@ func (srv *Server) getMessage() (interface{}, error) {
|
|
|
|
|
|
//验证消息签名
|
|
|
timestamp := srv.Query("timestamp")
|
|
|
+ srv.timestamp, err = strconv.ParseInt(timestamp, 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
nonce := srv.Query("nonce")
|
|
|
+ srv.nonce = nonce
|
|
|
msgSignature := srv.Query("msg_signature")
|
|
|
- msgSignatureCreate := util.Signature(srv.Token, timestamp, nonce, encryptedXMLMsg.EncryptedMsg)
|
|
|
- if msgSignature != msgSignatureCreate {
|
|
|
+ msgSignatureGen := util.Signature(srv.Token, timestamp, nonce, encryptedXMLMsg.EncryptedMsg)
|
|
|
+ if msgSignature != msgSignatureGen {
|
|
|
return nil, fmt.Errorf("消息不合法,验证签名失败")
|
|
|
}
|
|
|
|
|
|
//解密
|
|
|
- rawXMLMsgBytes, err = util.DecryptMsg(srv.AppID, encryptedXMLMsg.EncryptedMsg, srv.EncodingAESKey)
|
|
|
+ srv.random, rawXMLMsgBytes, err = util.DecryptMsg(srv.AppID, encryptedXMLMsg.EncryptedMsg, srv.EncodingAESKey)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("消息解密失败,err=%v", err)
|
|
|
+ return nil, fmt.Errorf("消息解密失败, err=%v", err)
|
|
|
}
|
|
|
} else {
|
|
|
rawXMLMsgBytes, err = ioutil.ReadAll(srv.Request.Body)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("从body中解析xml失败,err=%v", err)
|
|
|
+ return nil, fmt.Errorf("从body中解析xml失败, err=%v", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ srv.requestRawXMLMsg = rawXMLMsgBytes
|
|
|
+
|
|
|
+ return srv.parseRequestMessage(rawXMLMsgBytes)
|
|
|
+}
|
|
|
+
|
|
|
+func (srv *Server) parseRequestMessage(rawXMLMsgBytes []byte) (msg message.MixMessage, err error) {
|
|
|
+ msg = message.MixMessage{}
|
|
|
+ err = xml.Unmarshal(rawXMLMsgBytes, &msg)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//SetMessageHandler 设置用户自定义的回调方法
|
|
|
+func (srv *Server) SetMessageHandler(handler func(message.MixMessage) *message.Reply) {
|
|
|
+ srv.messageHandler = handler
|
|
|
+}
|
|
|
+
|
|
|
+func (srv *Server) buildResponse(reply *message.Reply) (err error) {
|
|
|
+ defer func() {
|
|
|
+ if e := recover(); e != nil {
|
|
|
+ err = fmt.Errorf("panic error: %v", err)
|
|
|
}
|
|
|
+ }()
|
|
|
+ if reply == nil {
|
|
|
+ //do nothing
|
|
|
+ return nil
|
|
|
}
|
|
|
+ msgType := reply.MsgType
|
|
|
+ switch msgType {
|
|
|
+ case message.MsgTypeText:
|
|
|
+ case message.MsgTypeImage:
|
|
|
+ case message.MsgTypeVoice:
|
|
|
+ case message.MsgTypeVideo:
|
|
|
+ case message.MsgTypeMusic:
|
|
|
+ case message.MsgTypeNews:
|
|
|
+ case message.MsgTypeTransfer:
|
|
|
+ default:
|
|
|
+ err = message.ErrUnsupportReply
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ msgData := reply.MsgData
|
|
|
+ value := reflect.ValueOf(msgData)
|
|
|
+ //msgData must be a ptr
|
|
|
+ kind := value.Kind().String()
|
|
|
+ if 0 != strings.Compare("ptr", kind) {
|
|
|
+ return message.ErrUnsupportReply
|
|
|
+ }
|
|
|
+
|
|
|
+ params := make([]reflect.Value, 1)
|
|
|
+ params[0] = reflect.ValueOf(srv.requestMsg.FromUserName)
|
|
|
+ value.MethodByName("SetToUserName").Call(params)
|
|
|
+
|
|
|
+ params[0] = reflect.ValueOf(srv.requestMsg.ToUserName)
|
|
|
+ value.MethodByName("SetFromUserName").Call(params)
|
|
|
|
|
|
- srv.rawXMLMsg = string(rawXMLMsgBytes)
|
|
|
- fmt.Println(srv.rawXMLMsg)
|
|
|
- return nil, nil
|
|
|
+ params[0] = reflect.ValueOf(msgType)
|
|
|
+ value.MethodByName("SetMsgType").Call(params)
|
|
|
+
|
|
|
+ params[0] = reflect.ValueOf(util.GetCurrTs())
|
|
|
+ value.MethodByName("SetCreateTime").Call(params)
|
|
|
+
|
|
|
+ srv.responseMsg = msgData
|
|
|
+ srv.responseRawXMLMsg, err = xml.Marshal(msgData)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//Send 将自定义的消息发送
|
|
|
+func (srv *Server) Send() (err error) {
|
|
|
+ replyMsg := srv.responseMsg
|
|
|
+ if srv.isSafeMode {
|
|
|
+ //安全模式下对消息进行加密
|
|
|
+ var encryptedMsg []byte
|
|
|
+ encryptedMsg, err = util.EncryptMsg(srv.random, srv.responseRawXMLMsg, srv.AppID, srv.EncodingAESKey)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //TODO 如果获取不到timestamp nonce 则自己生成
|
|
|
+ timestamp := srv.timestamp
|
|
|
+ timestampStr := strconv.FormatInt(timestamp, 10)
|
|
|
+ msgSignature := util.Signature(srv.Token, timestampStr, srv.nonce, string(encryptedMsg))
|
|
|
+ replyMsg = message.ResponseEncryptedXMLMsg{
|
|
|
+ EncryptedMsg: string(encryptedMsg),
|
|
|
+ MsgSignature: msgSignature,
|
|
|
+ Timestamp: timestamp,
|
|
|
+ Nonce: srv.nonce,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ srv.XML(replyMsg)
|
|
|
+ return
|
|
|
}
|