diff --git a/service/eastiot/Iot_api.query_ordered_pkgInfo.go b/service/eastiot/Iot_api.query_ordered_pkgInfo.go index ac543ed1..493bcf88 100644 --- a/service/eastiot/Iot_api.query_ordered_pkgInfo.go +++ b/service/eastiot/Iot_api.query_ordered_pkgInfo.go @@ -34,13 +34,13 @@ func NewIotApiQueryOrderedPkgInfoResult(result IotApiQueryOrderedPkgInfoResponse // IotApiQueryOrderedPkgInfo 查询流量卡已订购流量包 // https://www.showdoc.com.cn/916774523755909/5092045889939625 -func (app *App) IotApiQueryOrderedPkgInfo(simId string) *IotApiQueryOrderedPkgInfoResult { +func (c *Client) IotApiQueryOrderedPkgInfo(simId string) *IotApiQueryOrderedPkgInfoResult { // 参数 - param := NewParams() + param := gorequest.NewParams() param.Set("simId", simId) - params := app.NewParamsWith(param) + params := gorequest.NewParamsWith(param) // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/queryOrderedPkgInfo", params, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/queryOrderedPkgInfo", params, http.MethodPost) // 定义 var response IotApiQueryOrderedPkgInfoResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/Iot_api.query_sim_pkgInfo.go b/service/eastiot/Iot_api.query_sim_pkgInfo.go index c8ba1f9a..d4978994 100644 --- a/service/eastiot/Iot_api.query_sim_pkgInfo.go +++ b/service/eastiot/Iot_api.query_sim_pkgInfo.go @@ -40,14 +40,14 @@ func NewIotApiQuerySimPkgInfoResult(result IotApiQuerySimPkgInfoResponse, body [ // IotApiQuerySimPkgInfo 流量卡可用流量包查询 // https://www.showdoc.com.cn/916774523755909/4880284631482420 -func (app *App) IotApiQuerySimPkgInfo(simId string, sd int) *IotApiQuerySimPkgInfoResult { +func (c *Client) IotApiQuerySimPkgInfo(simId string, sd int) *IotApiQuerySimPkgInfoResult { // 参数 - param := NewParams() + param := gorequest.NewParams() param.Set("simId", simId) param.Set("sd", sd) - params := app.NewParamsWith(param) + params := gorequest.NewParamsWith(param) // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/querySimPkgInfo", params, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/querySimPkgInfo", params, http.MethodPost) // 定义 var response IotApiQuerySimPkgInfoResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/Iot_api.query_user_pkgInfo.go b/service/eastiot/Iot_api.query_user_pkgInfo.go index 6d6309a6..6a5dba08 100644 --- a/service/eastiot/Iot_api.query_user_pkgInfo.go +++ b/service/eastiot/Iot_api.query_user_pkgInfo.go @@ -39,9 +39,9 @@ func NewIotApiQueryUserPkgInfoResult(result IotApiQueryUserPkgInfoResponse, body // IotApiQueryUserPkgInfo 账户可用流量包查询 // https://www.showdoc.com.cn/916774523755909/4850094776758927 -func (app *App) IotApiQueryUserPkgInfo() *IotApiQueryUserPkgInfoResult { +func (c *Client) IotApiQueryUserPkgInfo() *IotApiQueryUserPkgInfoResult { // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/queryUserPkgInfo", map[string]interface{}{}, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/queryUserPkgInfo", map[string]interface{}{}, http.MethodPost) // 定义 var response IotApiQueryUserPkgInfoResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/Iot_api.recharge_sim.go b/service/eastiot/Iot_api.recharge_sim.go index 872987c9..dd96f73b 100644 --- a/service/eastiot/Iot_api.recharge_sim.go +++ b/service/eastiot/Iot_api.recharge_sim.go @@ -24,11 +24,11 @@ func NewIotApiRechargeSimResult(result IotApiRechargeSimResponse, body []byte, h // IotApiRechargeSim 单卡流量充值 // https://www.showdoc.com.cn/916774523755909/4880284631482420 -func (app *App) IotApiRechargeSim(notMustParams ...Params) *IotApiRechargeSimResult { +func (c *Client) IotApiRechargeSim(notMustParams ...gorequest.Params) *IotApiRechargeSimResult { // 参数 - params := app.NewParamsWith(notMustParams...) + params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/rechargeSim", params, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/rechargeSim", params, http.MethodPost) // 定义 var response IotApiRechargeSimResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/api.Iot_api.get_all_sim_type.go b/service/eastiot/api.Iot_api.get_all_sim_type.go index 54a50760..b112f9e6 100644 --- a/service/eastiot/api.Iot_api.get_all_sim_type.go +++ b/service/eastiot/api.Iot_api.get_all_sim_type.go @@ -29,9 +29,9 @@ func NewIotApiGetAllSimTypeResult(result IotApiGetAllSimTypeResponse, body []byt // IotApiGetAllSimType 卡类型列表查询 // https://www.showdoc.com.cn/916774523755909/4858492092033167 -func (app *App) IotApiGetAllSimType() *IotApiGetAllSimTypeResult { +func (c *Client) IotApiGetAllSimType() *IotApiGetAllSimTypeResult { // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/getAllSimType", map[string]interface{}{}, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/getAllSimType", map[string]interface{}{}, http.MethodPost) // 定义 var response IotApiGetAllSimTypeResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/app.go b/service/eastiot/app.go deleted file mode 100644 index 5d6b462c..00000000 --- a/service/eastiot/app.go +++ /dev/null @@ -1,73 +0,0 @@ -package eastiot - -import ( - "go.dtapp.net/library/utils/golog" - "go.dtapp.net/library/utils/gomongo" - "go.dtapp.net/library/utils/gorequest" - "gorm.io/gorm" - "time" -) - -type App struct { - appId string - apiKey string - mongo *gomongo.Client // 日志数据库 - pgsql *gorm.DB // pgsql数据库 - client *gorequest.App // 请求客户端 - log *golog.Api // 日志服务 - logTableName string // 日志表名 - logStatus bool // 日志状态 -} - -func NewApp(appID string, apiKey string, pgsql *gorm.DB) *App { - app := &App{appId: appID, apiKey: apiKey} - app.client = gorequest.NewHttp() - if pgsql != nil { - app.pgsql = pgsql - app.logStatus = true - app.logTableName = "eastiot" - 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["appId"] = app.appId - params["timeStamp"] = time.Now().Unix() - - // 签名 - params["sign"] = app.getSign(app.apiKey, params) - - // 创建请求 - 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/service/eastiot/client.go b/service/eastiot/client.go new file mode 100644 index 00000000..971adf3a --- /dev/null +++ b/service/eastiot/client.go @@ -0,0 +1,42 @@ +package eastiot + +import ( + "go.dtapp.net/library/utils/golog" + "go.dtapp.net/library/utils/gomongo" + "go.dtapp.net/library/utils/gorequest" + "gorm.io/gorm" +) + +type ConfigClient struct { + AppId string + ApiKey string + Mongo *gomongo.Client // 日志数据库 + PgsqlDb *gorm.DB // pgsql数据库 +} + +type Client struct { + client *gorequest.App // 请求客户端 + log *golog.ApiClient // 日志服务 + logStatus bool // 日志状态 + config *ConfigClient // 配置 +} + +func NewClient(config *ConfigClient) (*Client, error) { + + var err error + c := &Client{config: config} + + c.client = gorequest.NewHttp() + if c.config.PgsqlDb != nil { + c.logStatus = true + c.log, err = golog.NewApiClient(&golog.ConfigApiClient{ + Db: c.config.PgsqlDb, + TableName: logTable, + }) + if err != nil { + return nil, err + } + } + + return c, nil +} diff --git a/service/eastiot/const.go b/service/eastiot/const.go new file mode 100644 index 00000000..59eb7ff4 --- /dev/null +++ b/service/eastiot/const.go @@ -0,0 +1,9 @@ +package eastiot + +const ( + apiUrl = "http://m2m.eastiot.net" +) + +const ( + logTable = "eastiot" +) diff --git a/service/eastiot/pgsql.go b/service/eastiot/log.go similarity index 93% rename from service/eastiot/pgsql.go rename to service/eastiot/log.go index 1a182091..8e85ab25 100644 --- a/service/eastiot/pgsql.go +++ b/service/eastiot/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/eastiot/lot_api.query_user_balance.go b/service/eastiot/lot_api.query_user_balance.go index b9986f8e..711ee3e3 100644 --- a/service/eastiot/lot_api.query_user_balance.go +++ b/service/eastiot/lot_api.query_user_balance.go @@ -27,9 +27,9 @@ func NewIotApiQueryUserBalanceResult(result IotApiQueryUserBalanceResponse, body // IotApiQueryUserBalance 余额查询 // https://www.showdoc.com.cn/916774523755909/4857910459512420 -func (app *App) IotApiQueryUserBalance() *IotApiQueryUserBalanceResult { +func (c *Client) IotApiQueryUserBalance() *IotApiQueryUserBalanceResult { // 请求 - request, err := app.request("http://m2m.eastiot.net/Api/IotApi/queryUserBalance", map[string]interface{}{}, http.MethodPost) + request, err := c.request(apiUrl+"/Api/IotApi/queryUserBalance", map[string]interface{}{}, http.MethodPost) // 定义 var response IotApiQueryUserBalanceResponse err = json.Unmarshal(request.ResponseBody, &response) diff --git a/service/eastiot/params.go b/service/eastiot/params.go deleted file mode 100644 index 78f585e5..00000000 --- a/service/eastiot/params.go +++ /dev/null @@ -1,27 +0,0 @@ -package eastiot - -// Params 请求参数 -type Params map[string]interface{} - -func NewParams() Params { - p := make(Params) - return p -} - -func (app *App) NewParamsWith(params ...Params) Params { - p := make(Params) - for _, v := range params { - p.SetParams(v) - } - return p -} - -func (p Params) Set(key string, value interface{}) { - p[key] = value -} - -func (p Params) SetParams(params Params) { - for key, value := range params { - p[key] = value - } -} diff --git a/service/eastiot/request.go b/service/eastiot/request.go new file mode 100644 index 00000000..9721a4ef --- /dev/null +++ b/service/eastiot/request.go @@ -0,0 +1,44 @@ +package eastiot + +import ( + "go.dtapp.net/library/utils/gorequest" + "time" +) + +func (c *Client) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) { + + // 公共参数 + params["appId"] = c.config.AppId + params["timeStamp"] = time.Now().Unix() + + // 签名 + params["sign"] = c.getSign(params) + + // 创建请求 + 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/service/eastiot/sign.go b/service/eastiot/sign.go index 5c0662c9..f2bcde7e 100644 --- a/service/eastiot/sign.go +++ b/service/eastiot/sign.go @@ -8,7 +8,7 @@ import ( "strconv" ) -func (app *App) getSign(ApiKey string, p map[string]interface{}) string { +func (c *Client) getSign(p map[string]interface{}) string { var keys []string for k := range p { keys = append(keys, k) @@ -16,13 +16,13 @@ func (app *App) getSign(ApiKey string, p map[string]interface{}) string { sort.Strings(keys) signStr := "" for _, key := range keys { - signStr += fmt.Sprintf("%s=%s&", key, app.getString(p[key])) + signStr += fmt.Sprintf("%s=%s&", key, c.getString(p[key])) } - signStr += fmt.Sprintf("apiKey=%s", ApiKey) + signStr += fmt.Sprintf("apiKey=%s", c.config.ApiKey) return gomd5.ToUpper(signStr) } -func (app *App) getString(i interface{}) string { +func (c *Client) getString(i interface{}) string { switch v := i.(type) { case string: return v