You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wechatoffice/client.go

61 lines
1.7 KiB

package wechatoffice
import (
"go.dtapp.net/dorm"
"go.dtapp.net/golog"
"go.dtapp.net/gorequest"
)
// 缓存前缀
// wechat_office:wechat_access_token:
// wechat_office:wechat_jsapi_ticket:
type redisCachePrefixFun func() (wechatAccessToken, wechatJsapiTicket string)
// ClientConfig 实例配置
type ClientConfig struct {
AppId string // 小程序唯一凭证,即 appId
AppSecret string // 小程序唯一凭证密钥,即 appSecret
RedisClient *dorm.RedisClient // 缓存数据库
RedisCachePrefixFun redisCachePrefixFun // 缓存前缀
}
// Client 实例
type Client struct {
requestClient *gorequest.App // 请求服务
config struct {
appId string // 小程序唯一凭证,即 appId
appSecret string // 小程序唯一凭证密钥,即 appSecret
accessToken string // 接口调用凭证
jsapiTicket string // 签名凭证
}
cache struct {
redisClient *dorm.RedisClient // 缓存数据库
wechatAccessTokenPrefix string // AccessToken
wechatJsapiTicketPrefix string // JsapiTicket
}
log struct {
status bool // 状态
client *golog.ApiClient // 日志服务
}
}
// NewClient 创建实例化
func NewClient(config *ClientConfig) (*Client, error) {
c := &Client{}
c.config.appId = config.AppId
c.config.appSecret = config.AppSecret
c.requestClient = gorequest.NewHttp()
c.cache.redisClient = config.RedisClient
c.cache.wechatAccessTokenPrefix, c.cache.wechatJsapiTicketPrefix = config.RedisCachePrefixFun()
if c.cache.wechatAccessTokenPrefix == "" || c.cache.wechatJsapiTicketPrefix == "" {
return nil, redisCachePrefixNoConfig
}
return c, nil
}