diff --git a/app.go b/app.go deleted file mode 100644 index 158f688..0000000 --- a/app.go +++ /dev/null @@ -1,67 +0,0 @@ -package dingdanxia - -import ( - "go.dtapp.net/golog" - "go.dtapp.net/gorequest" - "gorm.io/gorm" -) - -type App struct { - apiKey string // 密钥 - pgsql *gorm.DB // pgsql数据库 - client *gorequest.App // 请求客户端 - log *golog.Api // 日志服务 - logTableName string // 日志表名 - logStatus bool // 日志状态 -} - -// NewApp 创建实例 -func NewApp(apiKey string, pgsql *gorm.DB) *App { - app := &App{apiKey: apiKey} - app.client = gorequest.NewHttp() - if pgsql != nil { - app.pgsql = pgsql - app.logStatus = true - app.logTableName = "dingdanxia" - 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) { - - // 公共参数 - params["apikey"] = app.apiKey - - // 创建请求 - client := app.client - - // 设置请求地址 - client.SetUri(url) - - // 设置方式 - client.SetMethod(method) - - // 设置格式 - client.SetContentTypeForm() - - // 设置参数 - 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 -} diff --git a/dingdanxia.go b/dingdanxia.go new file mode 100644 index 0000000..5c9ca81 --- /dev/null +++ b/dingdanxia.go @@ -0,0 +1,72 @@ +package dingdanxia + +import ( + "go.dtapp.net/golog" + "go.dtapp.net/gorequest" + "gorm.io/gorm" +) + +type ConfigClient struct { + ApiKey string // 密钥 + PgsqlDb *gorm.DB // pgsql数据库 +} + +type Client struct { + client *gorequest.App // 请求客户端 + log *golog.Api // 日志服务 + logTableName string // 日志表名 + logStatus bool // 日志状态 + config *ConfigClient // 配置 +} + +func NewClient(config *ConfigClient) *Client { + + c := &Client{config: config} + + c.client = gorequest.NewHttp() + if c.config.PgsqlDb != nil { + c.logStatus = true + c.logTableName = "dingdanxia" + 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) { + + // 公共参数 + params["apikey"] = c.config.ApiKey + + // 创建请求 + client := c.client + + // 设置请求地址 + client.SetUri(url) + + // 设置方式 + client.SetMethod(method) + + // 设置格式 + client.SetContentTypeForm() + + // 设置参数 + 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 +} diff --git a/jd.jy_order_details.go b/jd.jy_order_details.go index aa7ca96..5780c5d 100644 --- a/jd.jy_order_details.go +++ b/jd.jy_order_details.go @@ -39,11 +39,11 @@ func NewJdJyOrderDetailsResult(result JdJyOrderDetailsResponse, body []byte, htt } // JdJyOrderDetails 【官方不维护】 京佣订单 -func (app *App) JdJyOrderDetails(notMustParams ...Params) *JdJyOrderDetailsResult { +func (c *Client) JdJyOrderDetails(notMustParams ...Params) *JdJyOrderDetailsResult { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/jd/jy_order_details", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/jd/jy_order_details", params, http.MethodPost) // 定义 var response JdJyOrderDetailsResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/jd.order_details2.go b/jd.order_details2.go index fd85bf9..a82b3df 100644 --- a/jd.order_details2.go +++ b/jd.order_details2.go @@ -7,11 +7,11 @@ import ( // JdOrderDetails2 【官方不维护】 京东联盟订单行查询 // https://www.dingdanxia.com/doc/180/94 -func (app *App) JdOrderDetails2(notMustParams ...Params) *JdJyOrderDetailsResult { +func (c *Client) JdOrderDetails2(notMustParams ...Params) *JdJyOrderDetailsResult { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/jd/order_details2", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/jd/order_details2", params, http.MethodPost) // 定义 var response JdJyOrderDetailsResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/log.go b/log.go index 8d69b77..f33d4e8 100644 --- a/log.go +++ b/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/params.go b/params.go index 6b089ca..d37aa46 100644 --- a/params.go +++ b/params.go @@ -8,7 +8,7 @@ func NewParams() Params { return p } -func (app *App) NewParamsWith(params ...Params) Params { +func (c *Client) NewParamsWith(params ...Params) Params { p := make(Params) for _, v := range params { p.SetParams(v) diff --git a/waimai.meituan_orderid.go b/waimai.meituan_orderid.go index b1331f3..5270b4a 100644 --- a/waimai.meituan_orderid.go +++ b/waimai.meituan_orderid.go @@ -49,13 +49,13 @@ func NewWaiMaiMeituanOrderIdResult(result WaiMaiMeituanOrderIdResponse, body []b // WaiMaiMeituanOrderId 美团联盟外卖/闪购/优选/酒店订单查询API(订单号版) // https://www.dingdanxia.com/doc/179/173 -func (app *App) WaiMaiMeituanOrderId(orderId string) *WaiMaiMeituanOrderIdResult { +func (c *Client) WaiMaiMeituanOrderId(orderId string) *WaiMaiMeituanOrderIdResult { // 参数 param := NewParams() param.Set("order_id", orderId) - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/waimai/meituan_orderid", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/waimai/meituan_orderid", params, http.MethodPost) // 定义 var response WaiMaiMeituanOrderIdResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/waimai.meituan_orders.go b/waimai.meituan_orders.go index 0f807d9..da707ce 100644 --- a/waimai.meituan_orders.go +++ b/waimai.meituan_orders.go @@ -40,11 +40,11 @@ func NewWaiMaiMeituanOrdersResult(result WaiMaiMeituanOrdersResponse, body []byt // WaiMaiMeituanOrders 美团联盟外卖/闪购/优选/酒店订单查询API // https://www.dingdanxia.com/doc/176/173 -func (app *App) WaiMaiMeituanOrders(notMustParams ...Params) *WaiMaiMeituanOrdersResult { +func (c *Client) WaiMaiMeituanOrders(notMustParams ...Params) *WaiMaiMeituanOrdersResult { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/waimai/meituan_orders", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/waimai/meituan_orders", params, http.MethodPost) // 定义 var response WaiMaiMeituanOrdersResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/waimai.meituan_privilege.go b/waimai.meituan_privilege.go index d310283..a2533ee 100644 --- a/waimai.meituan_privilege.go +++ b/waimai.meituan_privilege.go @@ -37,15 +37,15 @@ func NewWaiMaiMeituanPrivilegeResult(result WaiMaiMeituanPrivilegeResponse, body // WaiMaiMeituanPrivilege 美团外卖CPS推广API接口 // https://www.dingdanxia.com/doc/174/173 -func (app *App) WaiMaiMeituanPrivilege(sid string, generateWeApp, qrcode bool) *WaiMaiMeituanPrivilegeResult { +func (c *Client) WaiMaiMeituanPrivilege(sid string, generateWeApp, qrcode bool) *WaiMaiMeituanPrivilegeResult { // 参数 param := NewParams() param.Set("sid", sid) param.Set("generate_we_app", generateWeApp) param.Set("qrcode", qrcode) - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/waimai/meituan_privilege", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/waimai/meituan_privilege", params, http.MethodPost) // 定义 var response WaiMaiMeituanPrivilegeResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/waimai.meituan_sg_privilege.go b/waimai.meituan_sg_privilege.go index 131d9d2..e173240 100644 --- a/waimai.meituan_sg_privilege.go +++ b/waimai.meituan_sg_privilege.go @@ -36,15 +36,15 @@ func NewWaiMaiMeituanSgPrivilegeResult(result WaiMaiMeituanSgPrivilegeResponse, // WaiMaiMeituanSgPrivilege 美团闪购CPS推广API接口 // https://www.dingdanxia.com/doc/195/173 -func (app *App) WaiMaiMeituanSgPrivilege(sid string, generateWeApp, qrcode bool) *WaiMaiMeituanSgPrivilegeResult { +func (c *Client) WaiMaiMeituanSgPrivilege(sid string, generateWeApp, qrcode bool) *WaiMaiMeituanSgPrivilegeResult { // 参数 param := NewParams() param.Set("sid", sid) param.Set("generate_we_app", generateWeApp) param.Set("qrcode", qrcode) - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://api.tbk.dingdanxia.com/waimai/meituan_sg_privilege", params, http.MethodPost) + request, err := c.request("https://api.tbk.dingdanxia.com/waimai/meituan_sg_privilege", params, http.MethodPost) // 定义 var response WaiMaiMeituanSgPrivilegeResponse err = json.Unmarshal(request.ResponseBody, &response)