|
|
|
@ -6,6 +6,22 @@ import (
|
|
|
|
|
"go.dtapp.net/gorequest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// client *dorm.GormClient
|
|
|
|
|
type gormClientFun func() *dorm.GormClient
|
|
|
|
|
|
|
|
|
|
// client *dorm.MongoClient
|
|
|
|
|
// databaseName string
|
|
|
|
|
type mongoClientFun func() (*dorm.MongoClient, string)
|
|
|
|
|
|
|
|
|
|
// ClientConfig 实例配置
|
|
|
|
|
type ClientConfig struct {
|
|
|
|
|
CustomerId int // 商家编号
|
|
|
|
|
CustomerKey string // 商家密钥
|
|
|
|
|
GormClientFun gormClientFun // 日志配置
|
|
|
|
|
MongoClientFun mongoClientFun // 日志配置
|
|
|
|
|
Debug bool // 日志开关
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Client 实例
|
|
|
|
|
type Client struct {
|
|
|
|
|
requestClient *gorequest.App // 请求服务
|
|
|
|
@ -23,31 +39,22 @@ type Client struct {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// client *dorm.GormClient
|
|
|
|
|
type gormClientFun func() *dorm.GormClient
|
|
|
|
|
|
|
|
|
|
// client *dorm.MongoClient
|
|
|
|
|
// databaseName string
|
|
|
|
|
type mongoClientFun func() (*dorm.MongoClient, string)
|
|
|
|
|
|
|
|
|
|
// NewClient 创建实例化
|
|
|
|
|
// customerId 商家编号
|
|
|
|
|
// customerKey 商家密钥
|
|
|
|
|
func NewClient(customerId int, customerKey string, gormClientFun gormClientFun, mongoClientFun mongoClientFun, debug bool) (*Client, error) {
|
|
|
|
|
func NewClient(config *ClientConfig) (*Client, error) {
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
c := &Client{}
|
|
|
|
|
|
|
|
|
|
c.config.customerId = customerId
|
|
|
|
|
c.config.customerKey = customerKey
|
|
|
|
|
c.config.customerId = config.CustomerId
|
|
|
|
|
c.config.customerKey = config.CustomerKey
|
|
|
|
|
|
|
|
|
|
c.requestClient = gorequest.NewHttp()
|
|
|
|
|
|
|
|
|
|
gormClient := gormClientFun()
|
|
|
|
|
gormClient := config.GormClientFun()
|
|
|
|
|
if gormClient.Db != nil {
|
|
|
|
|
c.log.logGormClient, err = golog.NewApiGormClient(func() (*dorm.GormClient, string) {
|
|
|
|
|
return gormClient, logTable
|
|
|
|
|
}, debug)
|
|
|
|
|
}, config.Debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -55,11 +62,11 @@ func NewClient(customerId int, customerKey string, gormClientFun gormClientFun,
|
|
|
|
|
}
|
|
|
|
|
c.log.gormClient = gormClient
|
|
|
|
|
|
|
|
|
|
mongoClient, databaseName := mongoClientFun()
|
|
|
|
|
mongoClient, databaseName := config.MongoClientFun()
|
|
|
|
|
if mongoClient.Db != nil {
|
|
|
|
|
c.log.logMongoClient, err = golog.NewApiMongoClient(func() (*dorm.MongoClient, string, string) {
|
|
|
|
|
return mongoClient, databaseName, logTable
|
|
|
|
|
}, debug)
|
|
|
|
|
}, config.Debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|