Files
chain33-im/logic/auth/dtalk/auth.go
2022-03-17 15:55:27 +08:00

68 lines
1.1 KiB
Go

package acc
import (
"encoding/json"
"errors"
"time"
"gitlab.33.cn/btrade/auto_trade_tools/reqtypes"
"gitlab.33.cn/btrade/auto_trade_tools/util"
)
type talkClient struct {
url string
timeout time.Duration
}
func (a *talkClient) DoAuth(token string) (uid string, err error) {
var (
bytes []byte
)
headers := map[string]string{}
headers["FZM-SIGNATURE"] = token
bytes, err = util.HttpReq(&reqtypes.HttpParams{
Method: "POST",
ReqUrl: a.url,
HeaderMap: headers,
Timeout: a.timeout,
})
if err != nil {
return
}
var res map[string]interface{}
err = json.Unmarshal(bytes, &res)
if err != nil {
return
}
if e, ok := res["error"]; ok {
err = errors.New(e.(string))
return
}
if _, ok := res["data"]; !ok {
err = errors.New("invalid auth res")
return
}
data, ok := res["data"].(map[string]interface{})
if !ok {
err = errors.New("invalid auth data format")
return
}
if _, ok := data["address"]; !ok {
err = errors.New("invalid auth data")
return
}
uid, ok = data["address"].(string)
if !ok {
err = errors.New("invalid auth data id format")
return
}
return
}