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

83 lines
2.0 KiB

2 years ago
package wikeyun
import (
2 years ago
"context"
2 years ago
"go.dtapp.net/dorm"
"go.dtapp.net/goip"
"go.dtapp.net/golog"
"go.dtapp.net/gorequest"
)
2 years ago
// Client 实例
2 years ago
type Client struct {
2 years ago
requestClient *gorequest.App // 请求服务
2 years ago
config struct {
clientIp string // 当前Ip
storeId int // 店铺ID
appKey int // key
appSecret string // secret
}
log struct {
2 years ago
gormClient *dorm.GormClient // 日志数据库
gorm bool // 日志开关
logGormClient *golog.ApiClient // 日志服务
mongoClient *dorm.MongoClient // 日志数据库
mongo bool // 日志开关
logMongoClient *golog.ApiClient // 日志服务
2 years ago
}
2 years ago
}
2 years ago
// client *dorm.GormClient
2 years ago
type gormClientFun func() *dorm.GormClient
2 years ago
// client *dorm.MongoClient
// databaseName string
type mongoClientFun func() (*dorm.MongoClient, string)
2 years ago
// NewClient 创建实例化
// storeId 店铺ID
// appKey key
// appSecret secret
func NewClient(storeId, appKey int, appSecret string, gormClientFun gormClientFun, mongoClientFun mongoClientFun, debug bool) (*Client, error) {
2 years ago
var err error
2 years ago
c := &Client{}
c.config.storeId = storeId
c.config.appKey = appKey
c.config.appSecret = appSecret
2 years ago
2 years ago
c.requestClient = gorequest.NewHttp()
2 years ago
2 years ago
gormClient := gormClientFun()
2 years ago
if gormClient.Db != nil {
2 years ago
c.log.logGormClient, err = golog.NewApiGormClient(func() (*dorm.GormClient, string) {
2 years ago
return gormClient, logTable
}, debug)
if err != nil {
return nil, err
}
c.log.gorm = true
}
2 years ago
c.log.gormClient = gormClient
2 years ago
mongoClient, databaseName := mongoClientFun()
2 years ago
if mongoClient.Db != nil {
c.log.logMongoClient, err = golog.NewApiMongoClient(func() (*dorm.MongoClient, string, string) {
2 years ago
return mongoClient, databaseName, logTable
}, debug)
2 years ago
if err != nil {
return nil, err
}
2 years ago
c.log.mongo = true
2 years ago
}
2 years ago
c.log.mongoClient = mongoClient
2 years ago
2 years ago
xip := goip.GetOutsideIp(context.Background())
2 years ago
if xip != "" && xip != "0.0.0.0" {
2 years ago
c.config.clientIp = xip
2 years ago
}
return c, nil
}