yaotian пре 8 година
родитељ
комит
675a3cc58e
6 измењених фајлова са 95 додато и 52 уклоњено
  1. 2 1
      examples/beego/beego.go
  2. 7 2
      mch.go
  3. 19 14
      mp.go
  4. 21 0
      server/context/config.go
  5. 8 7
      server/context/context.go
  6. 38 28
      wechat.go

+ 2 - 1
examples/beego/beego.go

@@ -7,11 +7,12 @@ import (
 	"github.com/astaxie/beego/context"
 	"github.com/yaotian/gowechat"
 	"github.com/yaotian/gowechat/mp/message"
+	gcontext "github.com/yaotian/gowechat/server/context"
 )
 
 func hello(ctx *context.Context) {
 	//配置微信参数
-	config := &gowechat.Config{
+	config := gcontext.Config{
 		AppID:          "your app id",
 		AppSecret:      "your app secret",
 		Token:          "your token",

+ 7 - 2
mch.go

@@ -5,12 +5,17 @@ import (
 	"github.com/yaotian/gowechat/mch/paytool"
 )
 
+//MchMgr mch mgt
+type MchMgr struct {
+	Wechat
+}
+
 // GetPay 基本支付api
-func (wc *Wechat) GetPay() *pay.Pay {
+func (wc *MchMgr) GetPay() *pay.Pay {
 	return pay.NewPay(wc.Context)
 }
 
 // GetPayTool 支付工具,发红包等
-func (wc *Wechat) GetPayTool() *paytool.PayTool {
+func (wc *MchMgr) GetPayTool() *paytool.PayTool {
 	return paytool.NewPayTool(wc.Context)
 }

+ 19 - 14
mp.go

@@ -11,37 +11,42 @@ import (
 	"github.com/yaotian/gowechat/mp/user"
 )
 
+//MpMgr mp mgr
+type MpMgr struct {
+	Wechat
+}
+
 //GetAccessToken 获取access_token
-func (wc *Wechat) GetAccessToken() (string, error) {
-	return wc.Context.GetAccessToken()
+func (c *MpMgr) GetAccessToken() (string, error) {
+	return c.Context.GetAccessToken()
 }
 
 // GetOauth oauth2网页授权
-func (wc *Wechat) GetOauth() *oauth.Oauth {
-	return oauth.NewOauth(wc.Context)
+func (c *MpMgr) GetOauth() *oauth.Oauth {
+	return oauth.NewOauth(c.Context)
 }
 
 // GetMaterial 素材管理
-func (wc *Wechat) GetMaterial() *material.Material {
-	return material.NewMaterial(wc.Context)
+func (c *MpMgr) GetMaterial() *material.Material {
+	return material.NewMaterial(c.Context)
 }
 
 // GetJs js-sdk配置
-func (wc *Wechat) GetJs() *jssdk.Js {
-	return jssdk.NewJs(wc.Context)
+func (c *MpMgr) GetJs() *jssdk.Js {
+	return jssdk.NewJs(c.Context)
 }
 
 // GetMenu 菜单管理接口
-func (wc *Wechat) GetMenu() *menu.Menu {
-	return menu.NewMenu(wc.Context)
+func (c *MpMgr) GetMenu() *menu.Menu {
+	return menu.NewMenu(c.Context)
 }
 
 // GetUser 用户管理接口
-func (wc *Wechat) GetUser() *user.User {
-	return user.NewUser(wc.Context)
+func (c *MpMgr) GetUser() *user.User {
+	return user.NewUser(c.Context)
 }
 
 // GetTemplate 模板消息接口
-func (wc *Wechat) GetTemplate() *template.Template {
-	return template.NewTemplate(wc.Context)
+func (c *MpMgr) GetTemplate() *template.Template {
+	return template.NewTemplate(c.Context)
 }

+ 21 - 0
server/context/config.go

@@ -0,0 +1,21 @@
+package context
+
+import "github.com/astaxie/beego/cache"
+
+// Config for user
+type Config struct {
+	AppID          string
+	AppSecret      string
+	Token          string
+	EncodingAESKey string
+	Cache          cache.Cache
+
+	//mch商户平台需要的变量
+	//证书
+	SslCertFilePath string //证书文件的路径
+	SslKeyFilePath  string
+	SslCertContent  string //证书的内容
+	SslKeyContent   string
+	MchID           string
+	MchAPIKey       string //商户平台设置的api key
+}

+ 8 - 7
server/context/context.go

@@ -9,10 +9,11 @@ import (
 
 // Context struct
 type Context struct {
-	AppID          string
-	AppSecret      string
-	Token          string
-	EncodingAESKey string
+	Config
+	// AppID          string
+	// AppSecret      string
+	// Token          string
+	// EncodingAESKey string
 
 	Cache cache.Cache
 
@@ -28,9 +29,9 @@ type Context struct {
 	HTTPClient  *http.Client
 	SHTTPClient *http.Client //SSL client
 
-	//商户平台APIKey
-	MchAPIKey string
-	MchID     string
+	// //商户平台APIKey
+	// MchAPIKey string
+	// MchID     string
 }
 
 // Query returns the keyed url query value if it exists

+ 38 - 28
wechat.go

@@ -1,6 +1,7 @@
 package gowechat
 
 import (
+	"fmt"
 	"net/http"
 	"sync"
 
@@ -15,40 +16,18 @@ type Wechat struct {
 	Context *context.Context
 }
 
-// Config for user
-type Config struct {
-	AppID          string
-	AppSecret      string
-	Token          string
-	EncodingAESKey string
-	Cache          cache.Cache
-
-	//mch商户平台需要的变量
-	//证书
-	SslCertFilePath string //证书文件的路径
-	SslKeyFilePath  string
-	SslCertContent  string //证书的内容
-	SslKeyContent   string
-	MchID           string
-	MchAPIKey       string //商户平台设置的api key
-}
-
 // NewWechat init
-func NewWechat(cfg *Config) *Wechat {
+func NewWechat(cfg context.Config) *Wechat {
 	context := new(context.Context)
 	initContext(cfg, context)
 	return &Wechat{context}
 }
 
-func initContext(cfg *Config, context *context.Context) {
-	context.AppID = cfg.AppID
-	context.AppSecret = cfg.AppSecret
-	context.Token = cfg.Token
-	context.EncodingAESKey = cfg.EncodingAESKey
-
+func initContext(cfg context.Config, context *context.Context) {
 	if cfg.Cache == nil {
 		cfg.Cache, _ = cache.NewCache("memory", `{"interval":60}`)
 	}
+	context.Config = cfg
 
 	context.SetAccessTokenLock(new(sync.RWMutex))
 	context.SetJsAPITicketLock(new(sync.RWMutex))
@@ -65,9 +44,6 @@ func initContext(cfg *Config, context *context.Context) {
 			context.SHTTPClient = client
 		}
 	}
-
-	context.MchAPIKey = cfg.MchAPIKey
-	context.MchID = cfg.MchID
 }
 
 // GetServer 消息管理
@@ -76,3 +52,37 @@ func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *serv
 	wc.Context.Writer = writer
 	return server.NewServer(wc.Context)
 }
+
+//Mch 商户平台
+func (wc *Wechat) Mch() (mch *MchMgr, err error) {
+	mch = new(MchMgr)
+	mch.Wechat = *wc
+	return
+}
+
+//Mp 公众平台
+func (wc *Wechat) Mp() (mp *MpMgr, err error) {
+	err = wc.checkCfgBase()
+	if err != nil {
+		return
+	}
+	mp = new(MpMgr)
+	mp.Wechat = *wc
+	return
+}
+
+//checkCfgBase 检查配置基本信息
+func (wc *Wechat) checkCfgBase() (err error) {
+	return
+}
+
+func (wc *Wechat) checkCfgMch() (err error) {
+	err = wc.checkCfgBase()
+	if err != nil {
+		return
+	}
+	if wc.Context.MchID == "" || wc.Context.MchAPIKey == "" {
+		return fmt.Errorf("%s", "配置中没有MchID或者MchAPIKey")
+	}
+	return
+}