From 584b50ab56f757f15d3030f7a88900fbc49f9a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 12 Aug 2022 17:15:52 +0800 Subject: [PATCH] - update request --- api.buy.go | 5 ++-- api.customer.go | 5 ++-- api.order.go | 5 ++-- api.outer-order.go | 5 ++-- api.product.cache.go | 47 ++++++++++++++++++++++++++++++++++ api.product.go | 7 ++--- api.product.recharge-params.go | 5 ++-- const.go | 9 +++++-- go.mod | 10 ++++---- go.sum | 20 +++++++-------- request.go | 39 ++++++++++++++++++++++++++-- 11 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 api.product.cache.go diff --git a/api.buy.go b/api.buy.go index 834a6eb..edf718a 100644 --- a/api.buy.go +++ b/api.buy.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -38,11 +39,11 @@ func newApiBuyResult(result ApiBuyResponse, body []byte, http gorequest.Response // ApiBuy 购买商品 // http://doc.cqmeihu.cn/sales/buy.html -func (c *Client) ApiBuy(notMustParams ...gorequest.Params) *ApiBuyResult { +func (c *Client) ApiBuy(ctx context.Context, notMustParams ...gorequest.Params) *ApiBuyResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(apiUrl+"/api/buy", params) + request, err := c.request(ctx, apiUrl+"/api/buy", params) // 定义 var response ApiBuyResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/api.customer.go b/api.customer.go index 15a79e1..8560f71 100644 --- a/api.customer.go +++ b/api.customer.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -28,9 +29,9 @@ func newApiCustomerResult(result ApiCustomerResponse, body []byte, http goreques // ApiCustomer 获取商家信息 // http://doc.cqmeihu.cn/sales/merchant-info.html -func (c *Client) ApiCustomer() *ApiCustomerResult { +func (c *Client) ApiCustomer(ctx context.Context) *ApiCustomerResult { // 请求 - request, err := c.request(apiUrl+"/api/customer", map[string]interface{}{}) + request, err := c.request(ctx, apiUrl+"/api/customer", map[string]interface{}{}) // 定义 var response ApiCustomerResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/api.order.go b/api.order.go index 911c60f..c2b3269 100644 --- a/api.order.go +++ b/api.order.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -52,13 +53,13 @@ func newApiOrderResult(result ApiOrderResponse, body []byte, http gorequest.Resp // ApiOrder 获取单个订单信息。 // 仅能获取自己购买的订单。 // http://doc.cqmeihu.cn/sales/order-info.html -func (c *Client) ApiOrder(orderId string) *ApiOrderResult { +func (c *Client) ApiOrder(ctx context.Context, orderId string) *ApiOrderResult { // 参数 param := gorequest.NewParams() param.Set("order_id", orderId) params := gorequest.NewParamsWith(param) // 请求 - request, err := c.request(apiUrl+"/api/order", params) + request, err := c.request(ctx, apiUrl+"/api/order", params) // 定义 var response ApiOrderResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/api.outer-order.go b/api.outer-order.go index be01aa0..0236b9f 100644 --- a/api.outer-order.go +++ b/api.outer-order.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -52,13 +53,13 @@ func newApiOuterOrderResult(result ApiOuterOrderResponse, body []byte, http gore // ApiOuterOrder 使用外部订单号获取单个订单信息 // 仅能获取自己购买的订单 // http://doc.cqmeihu.cn/sales/outer-order-info.html -func (c *Client) ApiOuterOrder(orderId string) *ApiOuterOrderResult { +func (c *Client) ApiOuterOrder(ctx context.Context, orderId string) *ApiOuterOrderResult { // 参数 param := gorequest.NewParams() param.Set("outer_order_id", orderId) params := gorequest.NewParamsWith(param) // 请求 - request, err := c.request(apiUrl+"/api/outer-order", params) + request, err := c.request(ctx, apiUrl+"/api/outer-order", params) // 定义 var response ApiOuterOrderResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/api.product.cache.go b/api.product.cache.go new file mode 100644 index 0000000..6cf09ce --- /dev/null +++ b/api.product.cache.go @@ -0,0 +1,47 @@ +package kashangwl + +import ( + "context" + "encoding/json" + "fmt" + "go.dtapp.net/gorequest" + "net/http" +) + +type ApiProductCacheResponse struct { + Code int `json:"code"` + Message string `json:"message"` + Data struct { + GoodsId string `json:"goods_id"` + ApiGoodsId int `json:"api_goods_id"` + GoodsName string `json:"goods_name"` + ClassificationName string `json:"classification_name"` + GoodsPrice float64 `json:"goods_price"` + PurchaseTips string `json:"purchase_tips"` + } `json:"data"` + Time int `json:"time"` + TraceId string `json:"trace_id"` +} + +type ApiProductCacheResult struct { + Result ApiProductCacheResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 + Err error // 错误 +} + +func newApiProductCacheResult(result ApiProductCacheResponse, body []byte, http gorequest.Response, err error) *ApiProductCacheResult { + return &ApiProductCacheResult{Result: result, Body: body, Http: http, Err: err} +} + +// ApiProductCache [缓存,需托管授权]获取单个商品信息 +func (c *Client) ApiProductCache(ctx context.Context, productId int64) *ApiProductCacheResult { + // 参数 + params := gorequest.NewParams() + // 请求 + request, err := c.requestCache(ctx, fmt.Sprintf("%s/%d/goods_info/%d", apiUrlCache, c.GetCustomerId(), productId), params, http.MethodGet) + // 定义 + var response ApiProductCacheResponse + err = json.Unmarshal(request.ResponseBody, &response) + return newApiProductCacheResult(response, request.ResponseBody, request, err) +} diff --git a/api.product.go b/api.product.go index edaccac..9e10de8 100644 --- a/api.product.go +++ b/api.product.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -17,7 +18,7 @@ type ApiProductResponse struct { SuperiorCommissionsRate int `json:"superior_commissions_rate"` // 上级佣金比例 Type int `json:"type"` // 商品类型(1:充值,2:卡密,3:卡券,4:人工) SupplyState int `json:"supply_state"` // 库存状态(1:充足,2:断货) - StockState int `json:"stock_state"` // 状态(1:上架,2:维护,3:下架) + StockState int `json:"stock_state"` // 状态(1:上架,2:维护,3:下架) BanStartAt string `json:"ban_start_at"` // 禁售开始时间 BanEndAt string `json:"ban_end_at"` // 禁售结束时间 } `json:"data"` @@ -36,12 +37,12 @@ func newApiProductResult(result ApiProductResponse, body []byte, http gorequest. // ApiProduct 获取单个商品信息 // http://doc.cqmeihu.cn/sales/product-info.html -func (c *Client) ApiProduct(productId int64) *ApiProductResult { +func (c *Client) ApiProduct(ctx context.Context, productId int64) *ApiProductResult { // 参数 params := gorequest.NewParams() params.Set("product_id", productId) // 请求 - request, err := c.request(apiUrl+"/api/product", params) + request, err := c.request(ctx, apiUrl+"/api/product", params) // 定义 var response ApiProductResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/api.product.recharge-params.go b/api.product.recharge-params.go index ae8033b..ae1ce33 100644 --- a/api.product.recharge-params.go +++ b/api.product.recharge-params.go @@ -1,6 +1,7 @@ package kashangwl import ( + "context" "encoding/json" "go.dtapp.net/gorequest" ) @@ -32,12 +33,12 @@ func newApiProductRechargeParamsResult(result ApiProductRechargeParamsResponse, // ApiProductRechargeParams 接口说明 // 获取商品的充值参数(仅支持充值类商品) // http://doc.cqmeihu.cn/sales/ProductParams.html -func (c *Client) ApiProductRechargeParams(productId int64) *ApiProductRechargeParamsResult { +func (c *Client) ApiProductRechargeParams(ctx context.Context, productId int64) *ApiProductRechargeParamsResult { // 参数 params := gorequest.NewParams() params.Set("product_id", productId) // 请求 - request, err := c.request(apiUrl+"/api/product/recharge-params", params) + request, err := c.request(ctx, apiUrl+"/api/product/recharge-params", params) // 定义 var response ApiProductRechargeParamsResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/const.go b/const.go index 8cc1a3b..7a5545a 100644 --- a/const.go +++ b/const.go @@ -5,11 +5,16 @@ const ( ) const ( - apiUrl = "http://www.kashangwl.com" + CodeCacheSuccess = 1 +) + +const ( + apiUrl = "http://www.kashangwl.com" + apiUrlCache = "https://api.dtapp.net/v1/866ka" ) const ( logTable = "kashangwl" ) -const Version = "1.0.1" +const Version = "1.0.2" diff --git a/go.mod b/go.mod index e0d7f4a..3039b0a 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module go.dtapp.net/kashangwl go 1.19 require ( - go.dtapp.net/dorm v1.0.14 - go.dtapp.net/golog v1.0.21 + go.dtapp.net/dorm v1.0.16 + go.dtapp.net/golog v1.0.22 go.dtapp.net/gorequest v1.0.24 gorm.io/gorm v1.23.8 ) @@ -69,14 +69,14 @@ require ( go.dtapp.net/gotime v1.0.5 // indirect go.dtapp.net/goxml v1.0.1 // indirect go.mongodb.org/mongo-driver v1.10.1 // indirect - go.uber.org/atomic v1.9.0 // indirect + go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.8.0 // indirect go.uber.org/zap v1.22.0 // indirect golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect + golang.org/x/net v0.0.0-20220811182439-13a9a731de15 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect - golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect + golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index cf17e1a..27893af 100644 --- a/go.sum +++ b/go.sum @@ -503,14 +503,14 @@ github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= -go.dtapp.net/dorm v1.0.14 h1:HDsaMwKGlKFKt59Y3hPI9aVnSjfXOyjoHw2uoXErKEo= -go.dtapp.net/dorm v1.0.14/go.mod h1:bHG7BmgeLaAlc56myYF63lwZAIuMeWRAqHBb/L84dLM= +go.dtapp.net/dorm v1.0.16 h1:aLBW9mgXjN9enV7cAqNpErQPDZk1oM6JxX62vz6hTdQ= +go.dtapp.net/dorm v1.0.16/go.mod h1:bHG7BmgeLaAlc56myYF63lwZAIuMeWRAqHBb/L84dLM= go.dtapp.net/goip v1.0.24 h1:62k3xt9I/YLUwA5tLp7YC8XPskkswc4RJrvxRRxjwIY= go.dtapp.net/goip v1.0.24/go.mod h1:tps0yoq5kSykLGDb01vuai47hzAQ6nYUPFWLdlQA2Oo= go.dtapp.net/gojson v1.0.1 h1:MHeSGlq1KxzL7rCkm18fhwW4GNORHohdDMmxY5PupKY= go.dtapp.net/gojson v1.0.1/go.mod h1:TkkpTNxHBKxul0e7gC5MrL1K4ICFB9mQ7wHzjBah3/k= -go.dtapp.net/golog v1.0.21 h1:pSGmDz3SJTH7M/NvVLZb+YJd8uNl5g18aORibuaY2JU= -go.dtapp.net/golog v1.0.21/go.mod h1:Wxm2Kh77JN5zFDZ72el9E9c/YkoJnOvHVusB7SqvoC8= +go.dtapp.net/golog v1.0.22 h1:sOJr5f/iLk/6irT/RuwTQSTwvL2DR8SIhzMsKdHo0Ic= +go.dtapp.net/golog v1.0.22/go.mod h1:sbn2WQXmlukcZ4T3Kz9iWOSznL8H3RCkD+1nicZHMfI= go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs= go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8= go.dtapp.net/gorequest v1.0.24 h1:N2RJOpCXPWbsjfQ8iYJI1EYC2se3I4QhK1l94DSJsuE= @@ -533,8 +533,8 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= +go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= @@ -605,8 +605,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced h1:3dYNDff0VT5xj+mbj2XucFst9WKk6PdGOrb9n+SbIvw= -golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220811182439-13a9a731de15 h1:cik0bxZUSJVDyaHf1hZPSDsU8SZHGQZQMeueXCE7yBQ= +golang.org/x/net v0.0.0-20220811182439-13a9a731de15/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -651,8 +651,8 @@ golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs= -golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/request.go b/request.go index 7ffb4af..e530ceb 100644 --- a/request.go +++ b/request.go @@ -1,11 +1,12 @@ package kashangwl import ( + "context" "go.dtapp.net/gorequest" "time" ) -func (c *Client) request(url string, params map[string]interface{}) (gorequest.Response, error) { +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}) (gorequest.Response, error) { // 公共参数 params["timestamp"] = time.Now().UnixNano() / 1e6 @@ -34,7 +35,41 @@ func (c *Client) request(url string, params map[string]interface{}) (gorequest.R // 日志 if c.config.PgsqlDb != nil { - go c.log.GormMiddleware(request, Version) + go c.log.GormMiddleware(ctx, request, Version) + } + if c.config.MongoDb != nil { + go c.log.MongoMiddleware(request) + } + + return request, err +} + +func (c *Client) requestCache(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) { + + // 创建请求 + client := c.client + + // 设置请求地址 + client.SetUri(url) + + // 设置方式 + client.SetMethod(method) + + // 设置FORM格式 + client.SetContentTypeJson() + + // 设置参数 + client.SetParams(params) + + // 发起请求 + request, err := client.Request() + if err != nil { + return gorequest.Response{}, err + } + + // 日志 + if c.config.PgsqlDb != nil { + go c.log.GormMiddleware(ctx, request, Version) } if c.config.MongoDb != nil { go c.log.MongoMiddleware(request)