1
0
mirror of https://github.com/fumiama/tienyik.git synced 2026-06-10 10:00:23 +08:00

feat: add more apis & cmd tyaliv

This commit is contained in:
源文雨
2025-11-24 17:35:20 +08:00
parent da1de770ad
commit eaf6d29fd8
14 changed files with 990 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
package hcli
import (
"context"
"net/http"
"reflect"
"strconv"
@@ -21,17 +22,22 @@ var DefaultClient = http.Client{
type Client struct {
rcnt uintptr
sg tienyik.Signer
secretKey string
offsetTime int64
Appmodel string
Devicecode string
Devicetype string
Devicetype uint64
Servernode string
Tenantid string
Usereid string
Version string
Version uint64
}
func NewClient() *Client {
return &Client{
sg: tienyik.NewSigner(context.TODO()),
Appmodel: tienyik.AppModelTOB,
Devicecode: tienyik.NewDeviceCode(),
Devicetype: tienyik.DeviceTypeWEB,
@@ -39,6 +45,15 @@ func NewClient() *Client {
}
}
func (c *Client) SetSecretKey(k string) {
c.secretKey = k
}
func (c *Client) SetTimestamp(ts int64) {
n := time.Now().UnixMilli()
c.offsetTime = n - ts
}
func (c *Client) setHeaders(req *http.Request) {
if c == nil {
return
@@ -54,27 +69,39 @@ func (c *Client) setHeaders(req *http.Request) {
}
fieldValue := v.Field(i)
if fieldValue.IsZero() {
continue
}
k := base14.DecodeString("廝呲舀㴄") + field.Name
if fieldValue.Kind() == reflect.String {
req.Header.Set(
base14.DecodeString("廝呲舀㴄")+field.Name,
fieldValue.String(),
)
switch fieldValue.Kind() {
case reflect.String:
req.Header.Set(k, fieldValue.String())
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint, reflect.Uintptr:
req.Header.Set(k, strconv.FormatUint(fieldValue.Uint(), 10))
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
req.Header.Set(k, strconv.FormatInt(fieldValue.Int(), 10))
default:
panic("unsupported field " + field.Name + " value type " + fieldValue.Type().String())
}
}
if c.Appmodel != "" {
ts := time.Now().UnixMilli()
rid := uint64(atomic.AddUintptr(&c.rcnt, 1)) + uint64(ts)
requestid := strconv.FormatUint(rid, 10)
ts -= c.offsetTime
timestamp := strconv.FormatInt(ts, 10)
requestid := strconv.FormatUint(
uint64(atomic.AddUintptr(&c.rcnt, 1))+uint64(ts), 10,
)
req.Header.Set(base14.DecodeString("廝呲草獱歙攷徥爀㴆"), requestid)
req.Header.Set(base14.DecodeString("廝呲荑睭杜蕆厵縀㴆"), timestamp)
}
if c.Servernode != "" {
//TODO: gensign
if c.secretKey != "" {
req.Header.Set(base14.DecodeString("廝呲荍睧榘敇揉獳欜渀㴂"), c.sg.GenKeyNew(
context.TODO(), c.Devicetype, uint64(ts), rid, c.secretKey,
c.Usereid, req.URL.EscapedPath(), c.Servernode, c.Version,
))
}
}
}