diff --git a/service/meituan/api.generate_link.go b/service/meituan/api.generate_link.go index f3078121..d8c70eb1 100644 --- a/service/meituan/api.generate_link.go +++ b/service/meituan/api.generate_link.go @@ -25,19 +25,19 @@ func NewApiGenerateLinkResult(result ApiGenerateLinkResponse, body []byte, http // ApiGenerateLink 自助取链接口(新版) // https://union.meituan.com/v2/apiDetail?id=25 -func (app *App) ApiGenerateLink(actId int64, sid string, linkType, shortLink int) *ApiGenerateLinkResult { +func (c *Client) ApiGenerateLink(actId int64, sid string, linkType, shortLink int) *ApiGenerateLinkResult { // 参数 param := gorequest.NewParams() - param.Set("actId", actId) // 活动id,可以在联盟活动列表中查看获取 - param.Set("appkey", app.appKey) // 媒体名称,可在推广者备案-媒体管理中查询 - param.Set("sid", sid) // 推广位sid,支持通过接口自定义创建,不受平台200个上限限制,长度不能超过64个字符,支持小写字母和数字,历史已创建的推广位不受这个约束 - param.Set("linkType", linkType) // 投放链接的类型 - param.Set("shortLink", shortLink) // 获取长链还是短链 + param.Set("actId", actId) // 活动id,可以在联盟活动列表中查看获取 + param.Set("appkey", c.config.AppKey) // 媒体名称,可在推广者备案-媒体管理中查询 + param.Set("sid", sid) // 推广位sid,支持通过接口自定义创建,不受平台200个上限限制,长度不能超过64个字符,支持小写字母和数字,历史已创建的推广位不受这个约束 + param.Set("linkType", linkType) // 投放链接的类型 + param.Set("shortLink", shortLink) // 获取长链还是短链 // 转换 params := gorequest.NewParamsWith(param) - params["sign"] = app.getSign(app.secret, params) + params["sign"] = c.getSign(c.config.Secret, params) // 请求 - request, err := app.request("https://openapi.meituan.com/api/generateLink", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/api/generateLink", params, http.MethodGet) // 定义 var response ApiGenerateLinkResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/api.getqualityscorebysid.go b/service/meituan/api.getqualityscorebysid.go index 962c3c20..168498be 100644 --- a/service/meituan/api.getqualityscorebysid.go +++ b/service/meituan/api.getqualityscorebysid.go @@ -34,15 +34,15 @@ func NewApiGetQuaLitYsCoreBySidResult(result ApiGetQuaLitYsCoreBySidResponse, bo // ApiGetQuaLitYsCoreBySid 优选sid质量分&复购率查询 // https://union.meituan.com/v2/apiDetail?id=28 -func (app *App) ApiGetQuaLitYsCoreBySid(notMustParams ...gorequest.Params) *ApiGetQuaLitYsCoreBySidResult { +func (c *Client) ApiGetQuaLitYsCoreBySid(notMustParams ...gorequest.Params) *ApiGetQuaLitYsCoreBySidResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求时刻10位时间戳(秒级),有效期60s params["ts"] = gotime.Current().Timestamp() - params["appkey"] = app.appKey - params["sign"] = app.getSign(app.secret, params) + params["appkey"] = c.config.AppKey + params["sign"] = c.getSign(c.config.Secret, params) // 请求 - request, err := app.request("https://openapi.meituan.com/api/getqualityscorebysid", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/api/getqualityscorebysid", params, http.MethodGet) // 定义 var response ApiGetQuaLitYsCoreBySidResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/api.mini_code.go b/service/meituan/api.mini_code.go index fac31ad2..6e9e0afe 100644 --- a/service/meituan/api.mini_code.go +++ b/service/meituan/api.mini_code.go @@ -25,17 +25,17 @@ func NewApiMiniCodeResult(result ApiMiniCodeResponse, body []byte, http goreques // ApiMiniCode 小程序生成二维码(新版) // https://union.meituan.com/v2/apiDetail?id=26 -func (app *App) ApiMiniCode(actId int64, sid string) *ApiMiniCodeResult { +func (c *Client) ApiMiniCode(actId int64, sid string) *ApiMiniCodeResult { // 参数 param := gorequest.NewParams() - param.Set("appkey", app.appKey) + param.Set("appkey", c.config.AppKey) param.Set("sid", sid) param.Set("actId", actId) // 转换 params := gorequest.NewParamsWith(param) - params["sign"] = app.getSign(app.secret, params) + params["sign"] = c.getSign(c.config.Secret, params) // 请求 - request, err := app.request("https://openapi.meituan.com/api/miniCode", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/api/miniCode", params, http.MethodGet) // 定义 var response ApiMiniCodeResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/api.order.go b/service/meituan/api.order.go index c0451375..dd36c96c 100644 --- a/service/meituan/api.order.go +++ b/service/meituan/api.order.go @@ -47,14 +47,14 @@ func NewApiOrderResult(result ApiOrderResponse, body []byte, http gorequest.Resp // ApiOrder 单订单查询接口(新版) // https://union.meituan.com/v2/apiDetail?id=24 -func (app *App) ApiOrder(notMustParams ...gorequest.Params) *ApiOrderResult { +func (c *Client) ApiOrder(notMustParams ...gorequest.Params) *ApiOrderResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求时刻10位时间戳(秒级),有效期60s - params["appkey"] = app.appKey - params["sign"] = app.getSign(app.secret, params) + params["appkey"] = c.config.AppKey + params["sign"] = c.getSign(c.config.Secret, params) // 请求 - request, err := app.request("https://openapi.meituan.com/api/order", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/api/order", params, http.MethodGet) // 定义 var response ApiOrderResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/api.order_list.go b/service/meituan/api.order_list.go index a1c22a03..38ad1691 100644 --- a/service/meituan/api.order_list.go +++ b/service/meituan/api.order_list.go @@ -46,15 +46,15 @@ func NewApiOrderListResult(result ApiOrderListResponse, body []byte, http gorequ // ApiOrderList 订单列表查询接口(新版) // https://union.meituan.com/v2/apiDetail?id=23 -func (app *App) ApiOrderList(notMustParams ...gorequest.Params) *ApiOrderListResult { +func (c *Client) ApiOrderList(notMustParams ...gorequest.Params) *ApiOrderListResult { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求时刻10位时间戳(秒级),有效期60s params["ts"] = gotime.Current().Timestamp() - params["appkey"] = app.appKey - params["sign"] = app.getSign(app.secret, params) + params["appkey"] = c.config.AppKey + params["sign"] = c.getSign(c.config.Secret, params) // 请求 - request, err := app.request("https://openapi.meituan.com/api/orderList", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/api/orderList", params, http.MethodGet) // 定义 var response ApiOrderListResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/app.go b/service/meituan/app.go deleted file mode 100644 index 3f140a2d..00000000 --- a/service/meituan/app.go +++ /dev/null @@ -1,68 +0,0 @@ -package meituan - -import ( - "go.dtapp.net/library/utils/golog" - "go.dtapp.net/library/utils/gorequest" - "gorm.io/gorm" -) - -// App 美团联盟 -type App struct { - secret string // 秘钥 - appKey string // 渠道标记 - pgsql *gorm.DB // pgsql数据库 - client *gorequest.App // 请求客户端 - log *golog.Api // 日志服务 - logTableName string // 日志表名 - logStatus bool // 日志状态 -} - -func NewApp(secret string, appKey string, pgsql *gorm.DB) *App { - app := &App{secret: secret, appKey: appKey} - app.client = gorequest.NewHttp() - if pgsql != nil { - app.pgsql = pgsql - app.logStatus = true - app.logTableName = "meituan" - app.log = golog.NewApi(&golog.ApiConfig{ - Db: pgsql, - TableName: app.logTableName, - }) - } - return app -} - -func (app *App) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) { - - // 创建请求 - client := app.client - - // 设置请求地址 - client.SetUri(url) - - // 设置方式 - client.SetMethod(method) - - // 设置格式 - client.SetContentTypeJson() - - // 设置参数 - client.SetParams(params) - - // 发起请求 - request, err := client.Request() - if err != nil { - return gorequest.Response{}, err - } - - // 日志 - if app.logStatus == true { - go app.postgresqlLog(request) - } - - return request, err -} - -func (app *App) GetAppKey() string { - return app.appKey -} diff --git a/service/meituan/client.go b/service/meituan/client.go new file mode 100644 index 00000000..ca11068b --- /dev/null +++ b/service/meituan/client.go @@ -0,0 +1,75 @@ +package meituan + +import ( + "go.dtapp.net/library/utils/golog" + "go.dtapp.net/library/utils/gorequest" + "gorm.io/gorm" +) + +type ConfigClient struct { + Secret string // 秘钥 + AppKey string // 渠道标记 + PgsqlDb *gorm.DB // pgsql数据库 +} + +// Client 美团联盟 +type Client struct { + client *gorequest.App // 请求客户端 + log *golog.Api // 日志服务 + logTableName string // 日志表名 + logStatus bool // 日志状态 + config *ConfigClient +} + +func NewClient(config *ConfigClient) *Client { + + c := &Client{} + c.config = config + + c.client = gorequest.NewHttp() + if c.config.PgsqlDb != nil { + c.logStatus = true + c.logTableName = "meituan" + c.log = golog.NewApi(&golog.ApiConfig{ + Db: c.config.PgsqlDb, + TableName: c.logTableName, + }) + } + + return c +} + +func (c *Client) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) { + + // 创建请求 + client := c.client + + // 设置请求地址 + client.SetUri(url) + + // 设置方式 + client.SetMethod(method) + + // 设置格式 + client.SetContentTypeJson() + + // 设置参数 + client.SetParams(params) + + // 发起请求 + request, err := client.Request() + if err != nil { + return gorequest.Response{}, err + } + + // 日志 + if c.logStatus == true { + go c.postgresqlLog(request) + } + + return request, err +} + +func (c *Client) GetAppKey() string { + return c.config.AppKey +} diff --git a/service/meituan/pgsql.go b/service/meituan/log.go similarity index 93% rename from service/meituan/pgsql.go rename to service/meituan/log.go index 97e9464f..af8577de 100644 --- a/service/meituan/pgsql.go +++ b/service/meituan/log.go @@ -8,8 +8,8 @@ import ( ) // 记录日志 -func (app *App) postgresqlLog(request gorequest.Response) { - app.log.Record(golog.ApiPostgresqlLog{ +func (c *Client) postgresqlLog(request gorequest.Response) { + c.log.Record(golog.ApiPostgresqlLog{ RequestTime: golog.TimeString{Time: request.RequestTime}, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 diff --git a/service/meituan/poi.area.go b/service/meituan/poi.area.go index e4840dce..4d9ed95b 100644 --- a/service/meituan/poi.area.go +++ b/service/meituan/poi.area.go @@ -31,13 +31,13 @@ func NewPoiAreaResult(result PoiAreaResponse, body []byte, http gorequest.Respon // PoiArea 基础数据 - 商圈接口 // https://openapi.meituan.com/#api-0.%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE-GetHttpsOpenapiMeituanComPoiAreaCityid1 -func (app *App) PoiArea(cityID int) *PoiAreaResult { +func (c *Client) PoiArea(cityID int) *PoiAreaResult { // 参数 param := gorequest.NewParams() param.Set("cityid", cityID) params := gorequest.NewParamsWith(param) // 请求 - request, err := app.request("https://openapi.meituan.com/poi/area", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/poi/area", params, http.MethodGet) // 定义 var response PoiAreaResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/poi.category.go b/service/meituan/poi.category.go index 09769da7..a96819ff 100644 --- a/service/meituan/poi.category.go +++ b/service/meituan/poi.category.go @@ -31,13 +31,13 @@ func NewPoiCategoryResult(result PoiCategoryResponse, body []byte, http goreques // PoiCategory 基础数据 - 品类接口 // https://openapi.meituan.com/#api-0.%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE-GetHttpsOpenapiMeituanComPoiDistrictCityid1 -func (app *App) PoiCategory(cityID int) *PoiCategoryResult { +func (c *Client) PoiCategory(cityID int) *PoiCategoryResult { // 参数 param := gorequest.NewParams() param.Set("cityid", cityID) params := gorequest.NewParamsWith(param) // 请求 - request, err := app.request("https://openapi.meituan.com/poi/category", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/poi/category", params, http.MethodGet) // 定义 var response PoiCategoryResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/poi.city.go b/service/meituan/poi.city.go index 713b132a..a49c04dd 100644 --- a/service/meituan/poi.city.go +++ b/service/meituan/poi.city.go @@ -28,9 +28,9 @@ func NewPoiCityResult(result PoiCityResponse, body []byte, http gorequest.Respon // PoiCity 基础数据 - 开放城市接口 // https://openapi.meituan.com/#api-0.%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE-GetHttpsOpenapiMeituanComPoiCity -func (app *App) PoiCity() *PoiCityResult { +func (c *Client) PoiCity() *PoiCityResult { // 请求 - request, err := app.request("https://openapi.meituan.com/poi/city", map[string]interface{}{}, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/poi/city", map[string]interface{}{}, http.MethodGet) // 定义 var response PoiCityResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/poi.district.go b/service/meituan/poi.district.go index 7c826133..88214b57 100644 --- a/service/meituan/poi.district.go +++ b/service/meituan/poi.district.go @@ -27,13 +27,13 @@ func NewPoiDistrictResult(result PoiDistrictResponse, body []byte, http goreques // PoiDistrict 基础数据 - 城市的行政区接口 // https://openapi.meituan.com/#api-0.%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE-GetHttpsOpenapiMeituanComPoiDistrictCityid1 -func (app *App) PoiDistrict(cityID int) *PoiDistrictResult { +func (c *Client) PoiDistrict(cityID int) *PoiDistrictResult { // 参数 param := gorequest.NewParams() param.Set("cityid", cityID) params := gorequest.NewParamsWith(param) // 请求 - request, err := app.request("https://openapi.meituan.com/poi/district", params, http.MethodGet) + request, err := c.request("https://openapi.meituan.com/poi/district", params, http.MethodGet) // 定义 var response PoiDistrictResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/meituan/service_http.order.go b/service/meituan/service_http.order.go index 41e1f8ef..a3b36e5e 100644 --- a/service/meituan/service_http.order.go +++ b/service/meituan/service_http.order.go @@ -37,9 +37,9 @@ type ResponseServeHttpOrder struct { // ServeHttpOrder 订单回推接口(新版) // https://union.meituan.com/v2/apiDetail?id=22 -func (app *App) ServeHttpOrder(c *gin.Context) (validateJson ResponseServeHttpOrder, err error) { +func (c *Client) ServeHttpOrder(gCtx *gin.Context) (validateJson ResponseServeHttpOrder, err error) { // 声明接收的变量 - err = c.ShouldBind(&validateJson) + err = gCtx.ShouldBind(&validateJson) return } diff --git a/service/meituan/sign.go b/service/meituan/sign.go index 9745b5d3..7ff7aae0 100644 --- a/service/meituan/sign.go +++ b/service/meituan/sign.go @@ -5,7 +5,6 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" - "io" "sort" "strconv" @@ -13,7 +12,7 @@ import ( // 签名(sign)生成逻辑(新版) // https://union.meituan.com/v2/apiDetail?id=27 -func (app *App) getSign(Secret string, params map[string]interface{}) string { +func (c *Client) getSign(Secret string, params map[string]interface{}) string { // 参数按照参数名的字典升序排列 var keys []string for k := range params { @@ -23,7 +22,7 @@ func (app *App) getSign(Secret string, params map[string]interface{}) string { signStr := bytes.NewBufferString(Secret) for _, k := range keys { signStr.WriteString(k) - signStr.WriteString(app.getString(params[k])) + signStr.WriteString(c.getString(params[k])) } signStr.WriteString(Secret) // md5加密 @@ -32,7 +31,7 @@ func (app *App) getSign(Secret string, params map[string]interface{}) string { return hex.EncodeToString(has.Sum(nil)) } -func (app *App) getString(i interface{}) string { +func (c *Client) getString(i interface{}) string { switch v := i.(type) { case string: return v