gosdk/adminapi/application.go

47 lines
988 B
Go
Raw Normal View History

2024-01-21 18:12:39 +08:00
package adminapi
import (
"encoding/json"
"fmt"
2024-01-21 18:49:51 +08:00
"gitlab.batiao8.com/open/gosdk/util"
2024-01-21 18:12:39 +08:00
)
type Application struct {
token string
address string
}
func NewApplication(addreess string, token string) *Application {
if addreess == "" {
addreess = "http://127.0.0.1:9281"
}
return &Application{
address: addreess,
token: token,
}
}
func (m *Application) Login(username, password, appid string) (interface{}, error) {
reqUrl := fmt.Sprintf("%s/admin/app/login", m.address)
params := make(map[string]interface{})
params["username"] = username
params["password"] = password
params["appid"] = appid
reqBody, _ := json.Marshal(params)
body, err := util.HttpPostJson(reqUrl, map[string]string{
"x-token": m.token,
}, reqBody)
if err != nil {
return nil, err
}
var rsp util.DataResponse
if err := json.Unmarshal(body, &rsp); err != nil {
return nil, err
}
if rsp.Code != 0 {
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
}
return rsp.Data, nil
}