yaotian 8 роки тому
батько
коміт
8a4f9db06e
4 змінених файлів з 22 додано та 17 видалено
  1. 1 1
      mp.go
  2. 3 2
      mp/base/base.go
  3. 0 0
      mp/oauth/oauth.go
  4. 18 14
      server/server.go

+ 1 - 1
mp.go

@@ -6,9 +6,9 @@ import (
 	"github.com/yaotian/gowechat/mp/jssdk"
 	"github.com/yaotian/gowechat/mp/material"
 	"github.com/yaotian/gowechat/mp/menu"
+	"github.com/yaotian/gowechat/mp/oauth"
 	"github.com/yaotian/gowechat/mp/template"
 	"github.com/yaotian/gowechat/mp/user"
-	"github.com/yaotian/gowechat/server/oauth"
 )
 
 //GetAccessToken 获取access_token

+ 3 - 2
mp/base/base.go

@@ -10,12 +10,13 @@ import (
 	"github.com/yaotian/gowechat/util"
 )
 
-//MpBase 微信公众
+//MpBase 微信公众平台,基本类
 type MpBase struct {
 	*context.Context
 }
 
-//HTTPGetWithAccessToken http get
+//HTTPGetWithAccessToken 微信公众平台中,自动加上access_token变量的GET调用,
+//如果失败,会清空AccessToken cache, 再试一次
 func (c *MpBase) HTTPGetWithAccessToken(url string) (resp []byte, err error) {
 	retry := 1
 Do:

server/oauth/oauth.go → mp/oauth/oauth.go


+ 18 - 14
server/server.go

@@ -8,6 +8,7 @@ import (
 	"reflect"
 	"runtime/debug"
 	"strconv"
+	"strings"
 
 	"github.com/yaotian/gowechat/mp/message"
 	"github.com/yaotian/gowechat/server/context"
@@ -42,25 +43,28 @@ func NewServer(context *context.Context) *Server {
 
 //Serve 处理微信的请求消息
 func (srv *Server) Serve() error {
-	if !srv.Validate() {
-		return fmt.Errorf("请求校验失败")
-	}
+	if strings.ToLower(srv.Context.Request.Method) == "get" {
+		if !srv.Validate() {
+			return fmt.Errorf("请求校验失败")
+		}
 
-	echostr, exists := srv.GetQuery("echostr")
-	if exists {
-		srv.String(echostr)
+		echostr, exists := srv.GetQuery("echostr")
+		if exists {
+			srv.String(echostr)
+		}
 		return nil
 	}
 
-	response, err := srv.handleRequest()
-	if err != nil {
-		return err
+	if strings.ToLower(srv.Context.Request.Method) == "post" {
+		replyMsg, err := srv.handleRequest()
+		if err != nil {
+			return err
+		}
+		//debug
+		//fmt.Println("request msg = ", string(srv.requestRawXMLMsg))
+		return srv.buildResponse(replyMsg)
 	}
-
-	//debug
-	//fmt.Println("request msg = ", string(srv.requestRawXMLMsg))
-
-	return srv.buildResponse(response)
+	return nil
 }
 
 //Validate 校验请求是否合法