From 81117223d26a8b9fbd4a8a9f3191d8caa4aabf99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 17 Jun 2022 15:11:17 +0800 Subject: [PATCH] - update --- params.go | 2 +- pgsql.go | 4 +-- rest.oil.add_card.go | 6 ++-- rest.oil.card_info.go | 6 ++-- rest.oil.del_card.go | 6 ++-- rest.oil.push_order.go | 6 ++-- rest.oil.query.go | 6 ++-- rest.oiledit_card.go | 6 ++-- rest.power.add_card.go | 8 +++--- rest.power.cancel.go | 6 ++-- rest.power.card_info.go | 6 ++-- rest.power.del_card.go | 6 ++-- rest.power.edit_card.go | 6 ++-- rest.power.push_order.go | 8 +++--- rest.power.query.go | 6 ++-- rest.recharge.cancel.go | 6 ++-- rest.recharge.mobileInfo.go | 6 ++-- rest.recharge.push_order.go | 8 +++--- rest.recharge.query.go | 6 ++-- rest.user.query.go | 4 +-- sign.go | 24 ++++++++-------- version.go | 2 +- wikeyun.go | 56 ++++++++++++++++++++----------------- 23 files changed, 103 insertions(+), 97 deletions(-) diff --git a/params.go b/params.go index 4d772cc..4046471 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/pgsql.go b/pgsql.go index d72c531..e28c33d 100644 --- a/pgsql.go +++ b/pgsql.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/rest.oil.add_card.go b/rest.oil.add_card.go index aeabc0a..d1c067e 100644 --- a/rest.oil.add_card.go +++ b/rest.oil.add_card.go @@ -1,10 +1,10 @@ package wikeyun // RestOilCardAdd 添加充值卡 -func (app *App) RestOilCardAdd(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilCardAdd(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/addCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/addCard", params) return request.ResponseBody, err } diff --git a/rest.oil.card_info.go b/rest.oil.card_info.go index 418906c..ecd7752 100644 --- a/rest.oil.card_info.go +++ b/rest.oil.card_info.go @@ -1,10 +1,10 @@ package wikeyun // RestOilCardInfo 油卡详情 -func (app *App) RestOilCardInfo(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilCardInfo(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/cardInfo", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/cardInfo", params) return request.ResponseBody, err } diff --git a/rest.oil.del_card.go b/rest.oil.del_card.go index 5c985a8..1c5f38e 100644 --- a/rest.oil.del_card.go +++ b/rest.oil.del_card.go @@ -1,10 +1,10 @@ package wikeyun // RestOilCardDel 油卡删除 -func (app *App) RestOilCardDel(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilCardDel(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/delCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/delCard", params) return request.ResponseBody, err } diff --git a/rest.oil.push_order.go b/rest.oil.push_order.go index c0a2dfb..20730b1 100644 --- a/rest.oil.push_order.go +++ b/rest.oil.push_order.go @@ -1,10 +1,10 @@ package wikeyun // RestOilOrderPush 充值下单 -func (app *App) RestOilOrderPush(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilOrderPush(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/pushOrder", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/pushOrder", params) return request.ResponseBody, err } diff --git a/rest.oil.query.go b/rest.oil.query.go index c62c16b..38ada9b 100644 --- a/rest.oil.query.go +++ b/rest.oil.query.go @@ -1,10 +1,10 @@ package wikeyun // RestOilOrderQuery 订单查询 -func (app *App) RestOilOrderQuery(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilOrderQuery(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/query", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/query", params) return request.ResponseBody, err } diff --git a/rest.oiledit_card.go b/rest.oiledit_card.go index 93d4da2..63a8110 100644 --- a/rest.oiledit_card.go +++ b/rest.oiledit_card.go @@ -1,10 +1,10 @@ package wikeyun // RestOilCardEdit 编辑充值卡 -func (app *App) RestOilCardEdit(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestOilCardEdit(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Oil/editCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Oil/editCard", params) return request.ResponseBody, err } diff --git a/rest.power.add_card.go b/rest.power.add_card.go index 78b1419..747e009 100644 --- a/rest.power.add_card.go +++ b/rest.power.add_card.go @@ -34,12 +34,12 @@ func NewRestPowerAddCardResult(result RestPowerAddCardResponse, body []byte, htt // RestPowerAddCard 添加电费充值卡 // https://open.wikeyun.cn/#/apiDocument/9/document/326 -func (app *App) RestPowerAddCard(notMustParams ...Params) *RestPowerAddCardResult { +func (c *Client) RestPowerAddCard(notMustParams ...Params) *RestPowerAddCardResult { // 参数 - params := app.NewParamsWith(notMustParams...) - params.Set("store_id", app.storeId) // 店铺ID + params := c.NewParamsWith(notMustParams...) + params.Set("store_id", c.config.StoreId) // 店铺ID // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/addCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/addCard", params) // 定义 var response RestPowerAddCardResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.power.cancel.go b/rest.power.cancel.go index fd13214..e9e288f 100644 --- a/rest.power.cancel.go +++ b/rest.power.cancel.go @@ -24,13 +24,13 @@ func NewRestPowerCancelResult(result RestPowerCancelResponse, body []byte, http // RestPowerCancel 电费订单取消 // https://open.wikeyun.cn/#/apiDocument/9/document/323 -func (app *App) RestPowerCancel(orderNumber string) *RestPowerCancelResult { +func (c *Client) RestPowerCancel(orderNumber string) *RestPowerCancelResult { // 参数 param := NewParams() param.Set("order_number", orderNumber) // 取消的单号,多个用英文逗号隔开 - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/cancel", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/cancel", params) // 定义 var response RestPowerCancelResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.power.card_info.go b/rest.power.card_info.go index b94c558..1d10035 100644 --- a/rest.power.card_info.go +++ b/rest.power.card_info.go @@ -2,10 +2,10 @@ package wikeyun // PowerCardInfo 电费充值卡详情 // https://open.wikeyun.cn/#/apiDocument/9/document/333 -func (app *App) PowerCardInfo(notMustParams ...Params) (body []byte, err error) { +func (c *Client) PowerCardInfo(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/cardInfo", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/cardInfo", params) return request.ResponseBody, err } diff --git a/rest.power.del_card.go b/rest.power.del_card.go index d021c0c..1a4f8f1 100644 --- a/rest.power.del_card.go +++ b/rest.power.del_card.go @@ -25,13 +25,13 @@ func NewRestPowerDelCardResult(result RestPowerDelCardResponse, body []byte, htt // RestPowerDelCard 删除电费充值卡 // https://open.wikeyun.cn/#/apiDocument/9/document/330 -func (app *App) RestPowerDelCard(cardId string) *RestPowerDelCardResult { +func (c *Client) RestPowerDelCard(cardId string) *RestPowerDelCardResult { // 参数 param := NewParams() param.Set("card_id", cardId) - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/delCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/delCard", params) // 定义 var response RestPowerDelCardResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.power.edit_card.go b/rest.power.edit_card.go index 80f4a01..64816ae 100644 --- a/rest.power.edit_card.go +++ b/rest.power.edit_card.go @@ -2,10 +2,10 @@ package wikeyun // RestPowerEditCard 编辑电费充值卡 // https://open.wikeyun.cn/#/apiDocument/9/document/329 -func (app *App) RestPowerEditCard(notMustParams ...Params) (body []byte, err error) { +func (c *Client) RestPowerEditCard(notMustParams ...Params) (body []byte, err error) { // 参数 - params := app.NewParamsWith(notMustParams...) + params := c.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/editCard", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/editCard", params) return request.ResponseBody, err } diff --git a/rest.power.push_order.go b/rest.power.push_order.go index 0349769..0ee0318 100644 --- a/rest.power.push_order.go +++ b/rest.power.push_order.go @@ -27,12 +27,12 @@ func NewRestPowerPushOrderResult(result RestPowerPushOrderResponse, body []byte, // RestPowerPushOrder 电费充值API // https://open.wikeyun.cn/#/apiDocument/9/document/311 -func (app *App) RestPowerPushOrder(notMustParams ...Params) *RestPowerPushOrderResult { +func (c *Client) RestPowerPushOrder(notMustParams ...Params) *RestPowerPushOrderResult { // 参数 - params := app.NewParamsWith(notMustParams...) - params.Set("store_id", app.storeId) // 店铺ID + params := c.NewParamsWith(notMustParams...) + params.Set("store_id", c.config.StoreId) // 店铺ID // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/pushOrder", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/pushOrder", params) // 定义 var response RestPowerPushOrderResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.power.query.go b/rest.power.query.go index 77a3e2e..8bc79d3 100644 --- a/rest.power.query.go +++ b/rest.power.query.go @@ -34,13 +34,13 @@ func NewRestPowerQueryResult(result RestPowerQueryResponse, body []byte, http go // RestPowerQuery 电费订单查询 // https://open.wikeyun.cn/#/apiDocument/9/document/313 -func (app *App) RestPowerQuery(orderNumber string) *RestPowerQueryResult { +func (c *Client) RestPowerQuery(orderNumber string) *RestPowerQueryResult { // 参数 param := NewParams() param.Set("order_number", orderNumber) // 平台单号 - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Power/query", params) + request, err := c.request("https://router.wikeyun.cn/rest/Power/query", params) // 定义 var response RestPowerQueryResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.recharge.cancel.go b/rest.recharge.cancel.go index 2ed05f3..43855c6 100644 --- a/rest.recharge.cancel.go +++ b/rest.recharge.cancel.go @@ -24,13 +24,13 @@ func NewRestRechargeCancelResult(result RestRechargeCancelResponse, body []byte, // RestRechargeCancel 话费订单取消 // https://open.wikeyun.cn/#/apiDocument/9/document/300 -func (app *App) RestRechargeCancel(orderNumber string) *RestRechargeCancelResult { +func (c *Client) RestRechargeCancel(orderNumber string) *RestRechargeCancelResult { // 参数 param := NewParams() param.Set("order_number", orderNumber) // 取消的单号,多个用英文逗号隔开 - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Recharge/cancel", params) + request, err := c.request("https://router.wikeyun.cn/rest/Recharge/cancel", params) // 定义 var response RestRechargeCancelResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.recharge.mobileInfo.go b/rest.recharge.mobileInfo.go index b5f17f7..7d55df4 100644 --- a/rest.recharge.mobileInfo.go +++ b/rest.recharge.mobileInfo.go @@ -44,13 +44,13 @@ func NewRestRechargeMobileInfoResult(result RestRechargeMobileInfoResponse, body // RestRechargeMobileInfo 查询手机归属地信息以及是否携号转网 // https://open.wikeyun.cn/#/apiDocument/9/document/374 -func (app *App) RestRechargeMobileInfo(orderNumber string) *RestRechargeMobileInfoResult { +func (c *Client) RestRechargeMobileInfo(orderNumber string) *RestRechargeMobileInfoResult { // 参数 param := NewParams() param.Set("order_number", orderNumber) // 平台单号 - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Recharge/mobileInfo", params) + request, err := c.request("https://router.wikeyun.cn/rest/Recharge/mobileInfo", params) // 定义 var response RestRechargeMobileInfoResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.recharge.push_order.go b/rest.recharge.push_order.go index a841868..a8287ce 100644 --- a/rest.recharge.push_order.go +++ b/rest.recharge.push_order.go @@ -26,12 +26,12 @@ func NewRestRechargePushOrderResult(result RestRechargePushOrderResponse, body [ // RestRechargePushOrder 话费充值推送 // https://open.wikeyun.cn/#/apiDocument/9/document/298 -func (app *App) RestRechargePushOrder(notMustParams ...Params) *RestRechargePushOrderResult { +func (c *Client) RestRechargePushOrder(notMustParams ...Params) *RestRechargePushOrderResult { // 参数 - params := app.NewParamsWith(notMustParams...) - params.Set("store_id", app.storeId) // 店铺ID + params := c.NewParamsWith(notMustParams...) + params.Set("store_id", c.config.StoreId) // 店铺ID // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Recharge/pushOrder", params) + request, err := c.request("https://router.wikeyun.cn/rest/Recharge/pushOrder", params) // 定义 var response RestRechargePushOrderResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.recharge.query.go b/rest.recharge.query.go index e609798..5b2118f 100644 --- a/rest.recharge.query.go +++ b/rest.recharge.query.go @@ -34,13 +34,13 @@ func NewRestRechargeQueryResult(result RestRechargeQueryResponse, body []byte, h // RestRechargeQuery 话费订单查询 // https://open.wikeyun.cn/#/apiDocument/9/document/299 -func (app *App) RestRechargeQuery(orderNumber string) *RestRechargeQueryResult { +func (c *Client) RestRechargeQuery(orderNumber string) *RestRechargeQueryResult { // 参数 param := NewParams() param.Set("order_number", orderNumber) // 平台订单号 - params := app.NewParamsWith(param) + params := c.NewParamsWith(param) // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/Recharge/query", params) + request, err := c.request("https://router.wikeyun.cn/rest/Recharge/query", params) // 定义 var response RestRechargeQueryResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/rest.user.query.go b/rest.user.query.go index 45a2fa6..355ff93 100644 --- a/rest.user.query.go +++ b/rest.user.query.go @@ -29,9 +29,9 @@ func NewRestUserQueryResult(result RestUserQueryResponse, body []byte, http gore // RestUserQuery 用户信息 // https://open.wikeyun.cn/#/apiDocument/10/document/336 -func (app *App) RestUserQuery() *RestUserQueryResult { +func (c *Client) RestUserQuery() *RestUserQueryResult { // 请求 - request, err := app.request("https://router.wikeyun.cn/rest/User/query", map[string]interface{}{}) + request, err := c.request("https://router.wikeyun.cn/rest/User/query", map[string]interface{}{}) // 定义 var response RestUserQueryResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/sign.go b/sign.go index 4d4a65f..a5727ce 100644 --- a/sign.go +++ b/sign.go @@ -20,39 +20,39 @@ type respSign struct { } // 签名 -func (app *App) sign(params map[string]interface{}) respSign { +func (c *Client) sign(params map[string]interface{}) respSign { // 默认参数 v := "1.0" format := "json" timestamp := strconv.FormatInt(time.Now().Unix(), 10) - params["v"] = v // 客户端接口版本,目前是1.0 - params["format"] = format // 默认json - params["app_key"] = app.appKey // 应用唯一表示 - params["client"] = app.clientIp // 客户端请求ip - params["timestamp"] = timestamp // unix时间戳(秒单位) + params["v"] = v // 客户端接口版本,目前是1.0 + params["format"] = format // 默认json + params["app_key"] = c.config.AppKey // 应用唯一表示 + params["client"] = c.clientIp // 客户端请求ip + params["timestamp"] = timestamp // unix时间戳(秒单位) // 排序所有的 key var keys []string for key := range params { keys = append(keys, key) } sort.Strings(keys) - signStr := app.appSecret + signStr := c.config.AppSecret for _, key := range keys { signStr += key + getString(params[key]) } - signStr += app.appSecret + signStr += c.config.AppSecret return respSign{ - AppKey: app.appKey, + AppKey: c.config.AppKey, Timestamp: timestamp, - Client: app.clientIp, + Client: c.clientIp, V: v, Format: format, - Sign: app.createSign(signStr), + Sign: c.createSign(signStr), } } // 签名 -func (app *App) createSign(signStr string) string { +func (c *Client) createSign(signStr string) string { h := md5.New() h.Write([]byte(signStr)) cipherStr := h.Sum(nil) diff --git a/version.go b/version.go index c695edc..aa78ef7 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package wikeyun -const Version = "1.0.0" +const Version = "1.0.1" diff --git a/wikeyun.go b/wikeyun.go index 05544d3..004a6bd 100644 --- a/wikeyun.go +++ b/wikeyun.go @@ -8,49 +8,55 @@ import ( "gorm.io/gorm" ) -type App struct { - storeId int // 店铺ID - appKey int // key - appSecret string // secret - clientIp string // Ip - pgsql *gorm.DB // pgsql数据库 +type ConfigClient struct { + StoreId int // 店铺ID + AppKey int // key + AppSecret string // secret + PgsqlDb *gorm.DB // pgsql数据库 +} + +type Client struct { client *gorequest.App // 请求客户端 + clientIp string // Ip log *golog.Api // 日志服务 logTableName string // 日志表名 logStatus bool // 日志状态 + config *ConfigClient } -// NewApp 创建实例 -func NewApp(storeId, appKey int, appSecret string, pgsql *gorm.DB) *App { - app := &App{storeId: storeId, appKey: appKey, appSecret: appSecret} - app.client = gorequest.NewHttp() - if pgsql != nil { - app.pgsql = pgsql - app.logStatus = true - app.logTableName = "wikeyun" - app.log = golog.NewApi(&golog.ApiConfig{ - Db: pgsql, - TableName: app.logTableName, +func NewClient(config *ConfigClient) *Client { + + c := &Client{config: config} + c.config = config + + c.client = gorequest.NewHttp() + if c.config.PgsqlDb != nil { + c.logStatus = true + c.logTableName = "wikeyun" + c.log = golog.NewApi(&golog.ApiConfig{ + Db: c.config.PgsqlDb, + TableName: c.logTableName, }) } xip := goip.GetOutsideIp() if xip != "" && xip != "0.0.0.0" { - app.clientIp = xip + c.clientIp = xip } - return app + + return c } // 请求接口 -func (app *App) request(url string, params map[string]interface{}) (resp gorequest.Response, err error) { +func (c *Client) request(url string, params map[string]interface{}) (resp gorequest.Response, err error) { // 签名 - sign := app.sign(params) + sign := c.sign(params) // 创建请求 - client := app.client + client := c.client // 设置请求地址 - client.SetUri(fmt.Sprintf("%s?app_key=%d×tamp=%s&client=%s&format=%s&v=%s&sign=%s", url, app.appKey, sign.Timestamp, sign.Client, sign.Format, sign.V, sign.Sign)) + client.SetUri(fmt.Sprintf("%s?app_key=%d×tamp=%s&client=%s&format=%s&v=%s&sign=%s", url, c.config.AppKey, sign.Timestamp, sign.Client, sign.Format, sign.V, sign.Sign)) // 设置FORM格式 client.SetContentTypeForm() @@ -65,8 +71,8 @@ func (app *App) request(url string, params map[string]interface{}) (resp goreque } // 日志 - if app.logStatus == true { - go app.postgresqlLog(request) + if c.logStatus == true { + go c.postgresqlLog(request) } return request, err