| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package wechat
- import (
- "dashoo.cn/opms_libary/plugin/wechat/context"
- "fmt"
- "github.com/gogf/gf/os/gcache"
- "sync"
- )
- var Client *ClientImpl
- var memCache *gcache.Cache
- // ClientImpl struct
- type ClientImpl struct {
- Context *context.Context
- }
- func init() {
- Client = NewClient()
- }
- // NewClient init
- func NewClient() *ClientImpl {
- var config = context.Config{
- //微信公众平台,需要填写的信息
- AppID: "wx7a7372d6b94c0ee9", //g.Config().GetString("wechat.app-id"), //"your app id",
- AppSecret: "5733eee31e2625dd2ebf18e2d4cd3cbc", //g.Config().GetString("wechat.app-secret"), //"your app secret",
- Token: "WEIXIN_dashoo_labsop_171210", //g.Config().GetString("wechat.token"), //"your token",
- EncodingAESKey: "9b2TW1dend4Vsu4prELD7Wfg0WHpHf48i6OtViYimfZ", //g.Config().GetString("wechat.aes-key"), //"your encoding aes key",
- }
- return newClient(config)
- }
- func newClient(cfg context.Config) *ClientImpl {
- context := new(context.Context)
- initContext(&cfg, context)
- return &ClientImpl{context}
- }
- func initContext(cfg *context.Config, context *context.Context) {
- if cfg.Cache == nil {
- if memCache == nil {
- memCache = gcache.New()
- }
- cfg.Cache = memCache
- }
- context.Config = cfg
- context.SetAccessTokenLock(new(sync.RWMutex))
- context.SetJsAPITicketLock(new(sync.RWMutex))
- }
- //MpClient 公众平台
- func (wc *ClientImpl) MpClient() (media *Media, err error) {
- err = wc.checkCfgBase()
- if err != nil {
- return
- }
- media = NewMedia()
- media.ClientImpl = wc
- return
- }
- //MiniClient 公众平台
- func (wc *ClientImpl) MiniClient() (media *Mini, err error) {
- err = wc.checkCfgBase()
- if err != nil {
- return
- }
- media = NewMini()
- media.ClientImpl = wc
- return
- }
- //checkCfgBase 检查配置基本信息
- func (wc *ClientImpl) checkCfgBase() (err error) {
- if wc.Context.AppID == "" {
- return fmt.Errorf("%s", "配置中没有AppID")
- }
- if wc.Context.AppSecret == "" {
- return fmt.Errorf("%s", "配置中没有AppSecret")
- }
- if wc.Context.Token == "" {
- return fmt.Errorf("%s", "配置中没有Token")
- }
- return
- }
|