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.
wechatunion/client.go

61 lines
1.6 KiB

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