diff --git a/CHANGELOG.md b/CHANGELOG.md index b3fd782b..899baf93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ - add [dingtalk](service%2Fdingtalk) - add [dingdanxia](service%2Fdingdanxia) - add [alipayopen](service%2Falipayopen) +- add [leshuazf](service%2Fleshuazf) +- add [pconline](service%2Fpconline) +- add [sendcloud](service%2Fsendcloud) ## v1.0.108 diff --git a/service/leshuazf/api.go b/service/leshuazf/api.go new file mode 100644 index 00000000..e6b1a278 --- /dev/null +++ b/service/leshuazf/api.go @@ -0,0 +1,25 @@ +package leshuazf + +import ( + "context" + "github.com/baidubce/bce-sdk-go/http" + "github.com/dtapps/go-library/utils/gorequest" +) + +func (c *Client) Get(ctx context.Context, _method string, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params, http.GET) + // 定义 + return request.ResponseBody, err +} + +func (c *Client) Post(ctx context.Context, _method string, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params, http.POST) + // 定义 + return request.ResponseBody, err +} diff --git a/service/leshuazf/apiv2.merchant.updateAuthority.go b/service/leshuazf/apiv2.merchant.updateAuthority.go index d5978d16..e7a60e06 100644 --- a/service/leshuazf/apiv2.merchant.updateAuthority.go +++ b/service/leshuazf/apiv2.merchant.updateAuthority.go @@ -1,6 +1,7 @@ package leshuazf import ( + "context" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" "net/http" @@ -22,11 +23,11 @@ func newApiV2MerchantUpdateAuthorityResult(result ApiV2MerchantUpdateAuthorityRe // ApiV2MerchantUpdateAuthority 给商户开通D0交易/结算权限接口。其中D0交易影响交易接口内t0字段能否标1,D0结算影响商户该种支付方式的秒到 // https://www.yuque.com/leshuazf/doc/dbmxyi#Vw97n -func (c *Client) ApiV2MerchantUpdateAuthority(notMustParams ...gorequest.Params) *ApiV2MerchantUpdateAuthorityResult { +func (c *Client) ApiV2MerchantUpdateAuthority(ctx context.Context, notMustParams ...gorequest.Params) *ApiV2MerchantUpdateAuthorityResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request("/apiv2/merchant/updateAuthority", params, http.MethodPost) + request, err := c.request(ctx, "/apiv2/merchant/updateAuthority", params, http.MethodPost) // 定义 var response ApiV2MerchantUpdateAuthorityResponse err = gojson.Unmarshal(request.ResponseBody, &response) diff --git a/service/leshuazf/client.go b/service/leshuazf/client.go index 6b28e790..f8cd98c6 100644 --- a/service/leshuazf/client.go +++ b/service/leshuazf/client.go @@ -1,54 +1,39 @@ package leshuazf import ( - "github.com/dtapps/go-library/utils/dorm" "github.com/dtapps/go-library/utils/golog" "github.com/dtapps/go-library/utils/gorequest" - "gorm.io/gorm" ) type ConfigClient struct { - AgentId string // 服务商编号,由乐刷分配的接入方唯一标识,明文传输。 - Environment string // 环境 - KeyAgent string - MongoDb *dorm.MongoClient // 日志数据库 - PgsqlDb *gorm.DB // 日志数据库 - DatabaseName string // 库名 + AgentId string // 服务商编号,由乐刷分配的接入方唯一标识,明文传输。 + Environment string // 环境 + KeyAgent string } // Client 乐刷 type Client struct { - client *gorequest.App // 请求客户端 - log *golog.ApiClient // 日志服务 - config *ConfigClient // 配置 + client *gorequest.App // 请求服务 + config struct { + agentId string // 服务商编号,由乐刷分配的接入方唯一标识,明文传输。 + environment string // 环境 + keyAgent string + } + log struct { + status bool // 状态 + client *golog.ApiClient // 日志服务 + } } func NewClient(config *ConfigClient) (*Client, error) { - var err error - c := &Client{config: config} + c := &Client{} - c.client = gorequest.NewHttp() + c.config.agentId = config.AgentId + c.config.environment = config.Environment + c.config.keyAgent = config.KeyAgent - if c.config.PgsqlDb != nil { - c.log, err = golog.NewApiClient( - golog.WithGormClient(c.config.PgsqlDb), - golog.WithTableName(logTable), - ) - if err != nil { - return nil, err - } - } - if c.config.MongoDb != nil { - c.log, err = golog.NewApiClient( - golog.WithMongoClient(c.config.MongoDb), - golog.WithDatabaseName(c.config.DatabaseName), - golog.WithCollectionName(logTable), - ) - if err != nil { - return nil, err - } - } + c.client = gorequest.NewHttp() return c, nil } diff --git a/service/leshuazf/config.go b/service/leshuazf/config.go new file mode 100644 index 00000000..0cbf1576 --- /dev/null +++ b/service/leshuazf/config.go @@ -0,0 +1,12 @@ +package leshuazf + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/leshuazf/data.area.go b/service/leshuazf/data.area.go index 1015a464..a1fe3381 100644 --- a/service/leshuazf/data.area.go +++ b/service/leshuazf/data.area.go @@ -1,6 +1,7 @@ package leshuazf import ( + "context" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" "net/http" @@ -30,11 +31,11 @@ func newDataAreaResult(result DataAreaResponse, body []byte, http gorequest.Resp // DataArea 代理商通过地区信息来查地区详细信息 // https://www.yuque.com/leshuazf/doc/dbmxyi#YwJl7 -func (c *Client) DataArea(notMustParams ...gorequest.Params) *DataAreaResult { +func (c *Client) DataArea(ctx context.Context, notMustParams ...gorequest.Params) *DataAreaResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request("/data/area", params, http.MethodPost) + request, err := c.request(ctx, "/data/area", params, http.MethodPost) // 定义 var response DataAreaResponse err = gojson.Unmarshal(request.ResponseBody, &response) diff --git a/service/leshuazf/data.bankbranch2.go b/service/leshuazf/data.bankbranch2.go index c5d83eec..bfdb33a4 100644 --- a/service/leshuazf/data.bankbranch2.go +++ b/service/leshuazf/data.bankbranch2.go @@ -1,6 +1,7 @@ package leshuazf import ( + "context" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" "net/http" @@ -43,11 +44,11 @@ func newDataBankBranch2Result(result DataBankBranch2Response, body []byte, http // DataBankBranch2 代理商通过联行号来查支行信息 // https://www.yuque.com/leshuazf/doc/dbmxyi#QYl0c -func (c *Client) DataBankBranch2(notMustParams ...gorequest.Params) *DataBankBranch2Result { +func (c *Client) DataBankBranch2(ctx context.Context, notMustParams ...gorequest.Params) *DataBankBranch2Result { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request("/data/bankbranch2", params, http.MethodPost) + request, err := c.request(ctx, "/data/bankbranch2", params, http.MethodPost) // 定义 var response DataBankBranch2Response err = gojson.Unmarshal(request.ResponseBody, &response) diff --git a/service/leshuazf/data.mcc.go b/service/leshuazf/data.mcc.go index bd291794..c511b489 100644 --- a/service/leshuazf/data.mcc.go +++ b/service/leshuazf/data.mcc.go @@ -1,6 +1,7 @@ package leshuazf import ( + "context" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" "net/http" @@ -29,11 +30,11 @@ func newDataMccResult(result DataMccResponse, body []byte, http gorequest.Respon // DataMcc 代理商通过MccCode来查商户类别明细 // https://www.yuque.com/leshuazf/doc/dbmxyi#jRTHN -func (c *Client) DataMcc(notMustParams ...gorequest.Params) *DataMccResult { +func (c *Client) DataMcc(ctx context.Context, notMustParams ...gorequest.Params) *DataMccResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request("/data/mcc", params, http.MethodPost) + request, err := c.request(ctx, "/data/mcc", params, http.MethodPost) // 定义 var response DataMccResponse err = gojson.Unmarshal(request.ResponseBody, &response) diff --git a/service/leshuazf/request.go b/service/leshuazf/request.go index f99de20d..033d7ea8 100644 --- a/service/leshuazf/request.go +++ b/service/leshuazf/request.go @@ -1,12 +1,14 @@ package leshuazf import ( + "context" + go_library "github.com/dtapps/go-library" "github.com/dtapps/go-library/utils/gorandom" "github.com/dtapps/go-library/utils/gorequest" "github.com/dtapps/go-library/utils/gotime" ) -func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) { +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) { // 环境 if c.GetEnvironment() == "test" { @@ -37,17 +39,14 @@ func (c *Client) request(url string, params map[string]interface{}, method strin client.SetParams(params) // 发起请求 - request, err := client.Request() + request, err := client.Request(ctx) if err != nil { return gorequest.Response{}, err } // 日志 - if c.config.PgsqlDb != nil { - go c.log.GormMiddleware(request) - } - if c.config.MongoDb != nil { - go c.log.MongoMiddleware(request) + if c.log.status { + go c.log.client.Middleware(ctx, request, go_library.Version()) } return request, err diff --git a/service/pconline/api.go b/service/pconline/api.go new file mode 100644 index 00000000..0ad30dc5 --- /dev/null +++ b/service/pconline/api.go @@ -0,0 +1,15 @@ +package pconline + +import ( + "context" + "github.com/dtapps/go-library/utils/gorequest" +) + +func (c *Client) Get(ctx context.Context, _method string, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params) + // 定义 + return request.ResponseBody, err +} diff --git a/service/pconline/client.go b/service/pconline/client.go index 3d8eaf48..932c483e 100644 --- a/service/pconline/client.go +++ b/service/pconline/client.go @@ -1,50 +1,23 @@ package pconline import ( - "github.com/dtapps/go-library/utils/dorm" "github.com/dtapps/go-library/utils/golog" "github.com/dtapps/go-library/utils/gorequest" - "gorm.io/gorm" ) -type ConfigClient struct { - MongoDb *dorm.MongoClient // 日志数据库 - PgsqlDb *gorm.DB // 日志数据库 - DatabaseName string // 库名 -} - type Client struct { - client *gorequest.App // 请求客户端 - log *golog.ApiClient // 日志服务 - config *ConfigClient // 配置 + client *gorequest.App // 请求服务 + log struct { + status bool // 状态 + client *golog.ApiClient // 日志服务 + } } -func NewClient(config *ConfigClient) (*Client, error) { +func NewClient() (*Client, error) { - var err error - c := &Client{config: config} + c := &Client{} c.client = gorequest.NewHttp() - if c.config.PgsqlDb != nil { - c.log, err = golog.NewApiClient( - golog.WithGormClient(c.config.PgsqlDb), - golog.WithTableName(logTable), - ) - if err != nil { - return nil, err - } - } - if c.config.MongoDb != nil { - c.log, err = golog.NewApiClient( - golog.WithMongoClient(c.config.MongoDb), - golog.WithDatabaseName(c.config.DatabaseName), - golog.WithCollectionName(logTable), - ) - if err != nil { - return nil, err - } - } - return c, nil } diff --git a/service/pconline/config.go b/service/pconline/config.go new file mode 100644 index 00000000..a7d6b271 --- /dev/null +++ b/service/pconline/config.go @@ -0,0 +1,12 @@ +package pconline + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/pconline/ip.go b/service/pconline/ip.go index af6594cf..732436cd 100644 --- a/service/pconline/ip.go +++ b/service/pconline/ip.go @@ -1,6 +1,7 @@ package pconline import ( + "context" "fmt" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" @@ -32,9 +33,10 @@ func newIpResult(result IpResponse, body []byte, http gorequest.Response, err er } // Ip 接口 https://whois.pconline.com.cn/ -func (c *Client) Ip(ip string) *IpResult { +func (c *Client) Ip(ctx context.Context, ip string) *IpResult { // 参数 + param := gorequest.NewParams() // 请求 - request, err := c.request(apiUrl + fmt.Sprintf("/ipJson.jsp?json=true&ip=%s", ip)) + request, err := c.request(ctx, apiUrl+fmt.Sprintf("/ipJson.jsp?json=true&ip=%s", ip), param) if err != nil { return newIpResult(IpResponse{}, request.ResponseBody, request, err) } diff --git a/service/pconline/request.go b/service/pconline/request.go index d9d1b68a..3a1a90e3 100644 --- a/service/pconline/request.go +++ b/service/pconline/request.go @@ -1,8 +1,12 @@ package pconline -import "github.com/dtapps/go-library/utils/gorequest" +import ( + "context" + go_library "github.com/dtapps/go-library" + "github.com/dtapps/go-library/utils/gorequest" +) -func (c *Client) request(url string) (gorequest.Response, error) { +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}) (gorequest.Response, error) { // 创建请求 client := c.client @@ -10,18 +14,18 @@ func (c *Client) request(url string) (gorequest.Response, error) { // 设置请求地址 client.SetUri(url) + // 设置参数 + client.SetParams(params) + // 发起请求 - request, err := client.Get() + request, err := client.Get(ctx) if err != nil { return gorequest.Response{}, err } // 日志 - if c.config.PgsqlDb != nil { - go c.log.GormMiddleware(request) - } - if c.config.MongoDb != nil { - go c.log.MongoMiddleware(request) + if c.log.status { + go c.log.client.Middleware(ctx, request, go_library.Version()) } return request, err diff --git a/service/sendcloud/api.go b/service/sendcloud/api.go new file mode 100644 index 00000000..60d4dbf9 --- /dev/null +++ b/service/sendcloud/api.go @@ -0,0 +1,25 @@ +package sendcloud + +import ( + "context" + "github.com/baidubce/bce-sdk-go/http" + "github.com/dtapps/go-library/utils/gorequest" +) + +func (c *Client) Get(ctx context.Context, _method string, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params, http.GET) + // 定义 + return request.ResponseBody, err +} + +func (c *Client) Post(ctx context.Context, _method string, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params, http.POST) + // 定义 + return request.ResponseBody, err +} diff --git a/service/sendcloud/apiv2.userinfo.get.go b/service/sendcloud/apiv2.userinfo.get.go index d4e5d996..46237322 100644 --- a/service/sendcloud/apiv2.userinfo.get.go +++ b/service/sendcloud/apiv2.userinfo.get.go @@ -1,6 +1,7 @@ package sendcloud import ( + "context" "github.com/dtapps/go-library/utils/gojson" "github.com/dtapps/go-library/utils/gorequest" "net/http" @@ -53,14 +54,14 @@ func newApiV2UserinfoGetResult(result ApiV2UserinfoGetResponse, body []byte, htt // ApiV2UserinfoGet 获取单个订单信息。 // 仅能获取自己购买的订单。 // http://doc.cqmeihu.cn/sales/ApiV2UserinfoGet-info.html -func (c *Client) ApiV2UserinfoGet() *ApiV2UserinfoGetResult { +func (c *Client) ApiV2UserinfoGet(ctx context.Context) *ApiV2UserinfoGetResult { // 参数 param := gorequest.NewParams() param.Set("apiUser", c.GetApiUser()) param.Set("apiKey", c.GetApiKey()) params := gorequest.NewParamsWith(param) // 请求 - request, err := c.request(apiUrl+"/apiv2/userinfo/get", params, http.MethodGet) + request, err := c.request(ctx, apiUrl+"/apiv2/userinfo/get", params, http.MethodGet) // 定义 var response ApiV2UserinfoGetResponse err = gojson.Unmarshal(request.ResponseBody, &response) diff --git a/service/sendcloud/client.go b/service/sendcloud/client.go index daed65f3..d40d787e 100644 --- a/service/sendcloud/client.go +++ b/service/sendcloud/client.go @@ -1,52 +1,35 @@ package sendcloud import ( - "github.com/dtapps/go-library/utils/dorm" "github.com/dtapps/go-library/utils/golog" "github.com/dtapps/go-library/utils/gorequest" - "gorm.io/gorm" ) type ConfigClient struct { - ApiUser string // API_USER - ApiKey string // API_KEY - MongoDb *dorm.MongoClient // 日志数据库 - PgsqlDb *gorm.DB // 日志数据库 - DatabaseName string // 库名 + ApiUser string // API_USER + ApiKey string // API_KEY } type Client struct { - client *gorequest.App // 请求客户端 - log *golog.ApiClient // 日志服务 - config *ConfigClient // 配置 + client *gorequest.App // 请求服务 + config struct { + apiUser string // API_USER + apiKey string // API_KEY + } + log struct { + status bool // 状态 + client *golog.ApiClient // 日志服务 + } } func NewClient(config *ConfigClient) (*Client, error) { - var err error - c := &Client{config: config} + c := &Client{} - c.client = gorequest.NewHttp() + c.config.apiUser = config.ApiUser + c.config.apiKey = config.ApiKey - if c.config.PgsqlDb != nil { - c.log, err = golog.NewApiClient( - golog.WithGormClient(c.config.PgsqlDb), - golog.WithTableName(logTable), - ) - if err != nil { - return nil, err - } - } - if c.config.MongoDb != nil { - c.log, err = golog.NewApiClient( - golog.WithMongoClient(c.config.MongoDb), - golog.WithDatabaseName(c.config.DatabaseName), - golog.WithCollectionName(logTable), - ) - if err != nil { - return nil, err - } - } + c.client = gorequest.NewHttp() return c, nil } diff --git a/service/sendcloud/config.go b/service/sendcloud/config.go new file mode 100644 index 00000000..2bd949de --- /dev/null +++ b/service/sendcloud/config.go @@ -0,0 +1,12 @@ +package sendcloud + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/sendcloud/request.go b/service/sendcloud/request.go index d80f5950..a3c1a4da 100644 --- a/service/sendcloud/request.go +++ b/service/sendcloud/request.go @@ -1,8 +1,12 @@ package sendcloud -import "github.com/dtapps/go-library/utils/gorequest" +import ( + "context" + go_library "github.com/dtapps/go-library" + "github.com/dtapps/go-library/utils/gorequest" +) -func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) { +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) { // 公共参数 //params["userid"] = c.userId //params["pwd"] = c.pwd @@ -25,17 +29,14 @@ func (c *Client) request(url string, params map[string]interface{}, method strin client.SetParams(params) // 发起请求 - request, err := client.Request() + request, err := client.Request(ctx) if err != nil { return gorequest.Response{}, err } // 日志 - if c.config.PgsqlDb != nil { - go c.log.GormMiddleware(request) - } - if c.config.MongoDb != nil { - go c.log.MongoMiddleware(request) + if c.log.status { + go c.log.client.Middleware(ctx, request, go_library.Version()) } return request, err