60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package wechat
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"gitlab.batiao8.com/open/gosdk/wechat/cache"
|
|
"gitlab.batiao8.com/open/gosdk/wechat/context"
|
|
"gitlab.batiao8.com/open/gosdk/wechat/server"
|
|
)
|
|
|
|
// Wechat struct
|
|
type Wechat struct {
|
|
Context *context.Context
|
|
}
|
|
|
|
// Config for user
|
|
type Config struct {
|
|
AppID string
|
|
AppSecret string
|
|
Token string
|
|
EncodingAESKey string
|
|
PayMchID string //支付 - 商户 ID
|
|
PayNotifyURL string //支付 - 接受微信支付结果通知的接口地址
|
|
PayKey string //支付 - 商户后台设置的支付 key
|
|
Cache cache.Cache
|
|
}
|
|
|
|
// NewWechat init
|
|
func NewWechat(cfg *Config) *Wechat {
|
|
context := new(context.Context)
|
|
copyConfigToContext(cfg, context)
|
|
return &Wechat{context}
|
|
}
|
|
|
|
func copyConfigToContext(cfg *Config, context *context.Context) {
|
|
context.AppID = cfg.AppID
|
|
context.AppSecret = cfg.AppSecret
|
|
context.Token = cfg.Token
|
|
context.EncodingAESKey = cfg.EncodingAESKey
|
|
context.PayMchID = cfg.PayMchID
|
|
context.PayKey = cfg.PayKey
|
|
context.PayNotifyURL = cfg.PayNotifyURL
|
|
context.Cache = cfg.Cache
|
|
context.SetAccessTokenLock(new(sync.RWMutex))
|
|
context.SetJsAPITicketLock(new(sync.RWMutex))
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// GetAccessToken 获取access_token
|
|
func (wc *Wechat) GetAccessToken() (string, error) {
|
|
return wc.Context.GetAccessToken()
|
|
}
|