feat: bind tribally account
This commit is contained in:
@@ -16,6 +16,7 @@ const (
|
||||
ErrEncodePassword Reason = 1003 // 密码加密错误
|
||||
ErrGenerateUUid Reason = 1004 // 生成uuid错误
|
||||
ErrGenerateToken Reason = 1005 // 生成token错误
|
||||
ErrSystemConfig Reason = 1006 // 系统配置错误
|
||||
|
||||
// ======= 业务层错误:20000~29999 =======
|
||||
ErrUnknownLogicError Reason = 20000 // 未知的业务错误
|
||||
@@ -30,4 +31,5 @@ const (
|
||||
ErrInvalidApiKey Reason = 20009 // 无效的api key
|
||||
ErrRoleNotFound Reason = 20010 // 角色不存在
|
||||
ErrInvalidParam Reason = 20011 // 无效的参数
|
||||
ErrNotBindRole Reason = 20012 // 未绑定角色
|
||||
)
|
||||
|
||||
48
internal/pkg/tribally/tribally.go
Normal file
48
internal/pkg/tribally/tribally.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package tribally
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func BindTribally(apiKey, userId string) (string, error) {
|
||||
url := "https://api.tribally.games/authenticate"
|
||||
payload := strings.NewReader(fmt.Sprintf(`{"playerId": "%s"}`, userId))
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("x-api-key", apiKey)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result := struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
Data struct {
|
||||
AuthURL string `json:"authUrl"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if result.Error != "" {
|
||||
return "", fmt.Errorf("error: %s, code: %s", result.Error, result.Code)
|
||||
}
|
||||
return result.Data.AuthURL, nil
|
||||
}
|
||||
Reference in New Issue
Block a user