Explorar el Código

context重构

yaotian hace 8 años
padre
commit
2485f34140

server/context/access_token.go → context/access_token.go


server/context/config.go → context/config.go


server/context/context.go → context/context.go


server/context/render.go → context/render.go


+ 9 - 3
examples/beego/beego.go

@@ -6,8 +6,8 @@ import (
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/context"
 	"github.com/yaotian/gowechat"
+	gcontext "github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/mp/message"
-	gcontext "github.com/yaotian/gowechat/server/context"
 )
 
 func hello(ctx *context.Context) {
@@ -21,7 +21,13 @@ func hello(ctx *context.Context) {
 	wc := gowechat.NewWechat(config)
 
 	// 传入request和responseWriter
-	server := wc.GetServer(ctx.Request, ctx.ResponseWriter)
+	var mp *gowechat.MpMgr
+	var err error
+	mp, err = wc.Mp()
+	if err != nil {
+		return
+	}
+	server := mp.GetServer(ctx.Request, ctx.ResponseWriter)
 	//设置接收消息的处理方法
 	server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
 
@@ -31,7 +37,7 @@ func hello(ctx *context.Context) {
 	})
 
 	//处理消息接收以及回复
-	err := server.Serve()
+	err = server.Serve()
 	if err != nil {
 		fmt.Println(err)
 		return

+ 1 - 1
mch/base/base.go

@@ -7,7 +7,7 @@ import (
 	"io/ioutil"
 	"net/http"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 )
 
 //MchBase base mch

+ 1 - 1
mch/pay/pay.go

@@ -2,7 +2,7 @@ package pay
 
 import (
 	"github.com/yaotian/gowechat/mch/base"
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 )
 
 //Pay pay

+ 1 - 1
mch/paytool/cash_coupon.go

@@ -2,7 +2,7 @@ package paytool
 
 import (
 	"github.com/yaotian/gowechat/mch/base"
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 )
 
 //PayTool pay tool

+ 10 - 0
mp.go

@@ -3,10 +3,13 @@
 package gowechat
 
 import (
+	"net/http"
+
 	"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/server"
 	"github.com/yaotian/gowechat/mp/template"
 	"github.com/yaotian/gowechat/mp/user"
 )
@@ -50,3 +53,10 @@ func (c *MpMgr) GetUser() *user.User {
 func (c *MpMgr) GetTemplate() *template.Template {
 	return template.NewTemplate(c.Context)
 }
+
+// GetServer 消息管理
+func (c *MpMgr) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
+	c.Context.Request = req
+	c.Context.Writer = writer
+	return server.NewServer(c.Context)
+}

+ 1 - 1
mp/base/base.go

@@ -6,7 +6,7 @@ import (
 	"net/http"
 	"strings"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/jssdk/jssdk.go

@@ -5,7 +5,7 @@ import (
 	"fmt"
 	"time"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/material/material.go

@@ -5,7 +5,7 @@ import (
 	"errors"
 	"fmt"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/menu/menu.go

@@ -5,7 +5,7 @@ import (
 	"fmt"
 
 	"github.com/yaotian/gowechat/mp/base"
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/oauth/oauth.go

@@ -6,7 +6,7 @@ import (
 	"net/http"
 	"net/url"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

server/README.md → mp/server/README.md


+ 1 - 1
server/server.go

@@ -10,8 +10,8 @@ import (
 	"strconv"
 	"strings"
 
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/mp/message"
-	"github.com/yaotian/gowechat/server/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/template/template.go

@@ -4,7 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 1
mp/user/user.go

@@ -5,7 +5,7 @@ import (
 	"fmt"
 
 	"github.com/yaotian/gowechat/mp/base"
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 

+ 1 - 10
wechat.go

@@ -2,12 +2,10 @@ package gowechat
 
 import (
 	"fmt"
-	"net/http"
 	"sync"
 
 	"github.com/astaxie/beego/cache"
-	"github.com/yaotian/gowechat/server"
-	"github.com/yaotian/gowechat/server/context"
+	"github.com/yaotian/gowechat/context"
 	"github.com/yaotian/gowechat/util"
 )
 
@@ -46,13 +44,6 @@ func initContext(cfg context.Config, context *context.Context) {
 	}
 }
 
-// GetServer 消息管理
-func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
-	wc.Context.Request = req
-	wc.Context.Writer = writer
-	return server.NewServer(wc.Context)
-}
-
 //Mch 商户平台
 func (wc *Wechat) Mch() (mch *MchMgr, err error) {
 	mch = new(MchMgr)