- update eastiot

master
李光春 2 years ago
parent efe77317be
commit bf43a718d3

@ -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)

@ -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)

@ -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)

@ -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)

@ -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)

@ -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
}

@ -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
}

@ -0,0 +1,9 @@
package eastiot
const (
apiUrl = "http://m2m.eastiot.net"
)
const (
logTable = "eastiot"
)

@ -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, //【请求】链接

@ -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)

@ -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
}
}

@ -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
}

@ -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

Loading…
Cancel
Save