-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c26d4f
commit 6503f7f
Showing
11 changed files
with
199 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package skland | ||
|
||
import ( | ||
"fmt" | ||
"github.com/starudream/go-lib/core/v2/gh" | ||
"github.com/starudream/go-lib/resty/v2" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type SignGameData struct { | ||
Ts string `json:"ts"` | ||
Awards SignGameAwards `json:"awards"` | ||
} | ||
|
||
type SignGameAward struct { | ||
Type string `json:"type"` | ||
Count int `json:"count"` | ||
Resource *SignGameRes `json:"resource"` | ||
} | ||
|
||
type SignGameRes struct { | ||
Id string `json:"id"` | ||
Type string `json:"type"` | ||
Name string `json:"name"` | ||
Rarity int `json:"rarity"` | ||
} | ||
|
||
type SignGameAwards []*SignGameAward | ||
|
||
func SignGamePlayer(uid string, account Account) (award string, hasSigned bool, err error) { | ||
account, err = RefreshToken(account) | ||
if err != nil { | ||
return | ||
} | ||
signGameData, err := signGame("1", uid, account.Skland) | ||
if err != nil { | ||
e, ok1 := resty.AsRespErr(err) | ||
if ok1 { | ||
t, ok2 := e.Response.Error().(*SKBaseResp[interface{}]) | ||
if ok2 && t.Message == "请勿重复签到!" { | ||
err = nil | ||
hasSigned = true | ||
} | ||
} else { | ||
err = fmt.Errorf("sign game error: %w", err) | ||
return | ||
} | ||
} else { | ||
award = signGameData.Awards.shortString() | ||
} | ||
return | ||
} | ||
|
||
// 签到 | ||
func signGame(gid, uid string, skland AccountSkland) (*SignGameData, error) { | ||
req := SKR().SetBody(gh.M{"gameId": gid, "uid": uid}) | ||
return SklandRequest[*SignGameData](req, "POST", "/api/v1/game/attendance", skland) | ||
} | ||
|
||
func (t SignGameAwards) shortString() string { | ||
v := make([]string, len(t)) | ||
for i, a := range t { | ||
v[i] = a.Resource.Name + "*" + strconv.Itoa(a.Count) | ||
} | ||
return strings.Join(v, ", ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package skland | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/md5" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"github.com/starudream/go-lib/core/v2/codec/json" | ||
"github.com/starudream/go-lib/core/v2/utils/structutil" | ||
"github.com/starudream/go-lib/resty/v2" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func addSign(r *resty.Request, method, path string, skland AccountSkland) { | ||
ts := strconv.FormatInt(time.Now().Unix(), 10) | ||
|
||
headers := signHeaders{Platform: "1", Timestamp: ts, DId: "743a446c83032899", VName: "1.5.1"} | ||
|
||
r.SetHeaders(tom(headers)) | ||
|
||
_, signature := sign(headers, method, path, skland.Token, r.QueryParam, r.Body) | ||
|
||
r.SetHeader("cred", skland.Cred) | ||
r.SetHeader("sign", signature) | ||
} | ||
|
||
func tom(s any) map[string]string { | ||
t := structutil.New(s) | ||
t.TagName = "json" | ||
m := map[string]string{} | ||
for k, v := range t.Map() { | ||
m[k] = v.(string) | ||
} | ||
return m | ||
} | ||
|
||
func sign(headers signHeaders, method, path, token string, query url.Values, body any) (string, string) { | ||
str := query.Encode() | ||
if method != "GET" { | ||
str = json.MustMarshalString(body) | ||
} | ||
|
||
content := path + str + headers.Timestamp + json.MustMarshalString(headers) | ||
|
||
b1 := hmac256(token, content) | ||
s1 := hex.EncodeToString(b1) | ||
b2 := md5.Sum([]byte(s1)) | ||
s2 := hex.EncodeToString(b2[:]) | ||
|
||
return content, s2 | ||
} | ||
|
||
func hmac256(key, content string) []byte { | ||
h := hmac.New(sha256.New, []byte(key)) | ||
h.Write([]byte(content)) | ||
return h.Sum(nil) | ||
} |
Oops, something went wrong.