sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package allocaterequest
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
var validID = regexp.MustCompile(`^[A-Za-z0-9_-]{16,96}$`)
|
||||
|
||||
func Key(tenant string, user int64, requestID string) (string, error) {
|
||||
requestID = strings.TrimSpace(requestID)
|
||||
if tenant == "" || user <= 0 || !validID.MatchString(requestID) {
|
||||
return "", gerror.New("划拨请求标识无效")
|
||||
}
|
||||
value, err := json.Marshal([]any{tenant, user, "allocate", requestID})
|
||||
if err != nil {
|
||||
return "", gerror.Wrap(err, "生成划拨请求标识失败")
|
||||
}
|
||||
digest := sha256.Sum256(value)
|
||||
return hex.EncodeToString(digest[:]), nil
|
||||
}
|
||||
|
||||
func Fingerprint(cards []string, source, target int64, mode int, remark string) string {
|
||||
sorted := append([]string(nil), cards...)
|
||||
sort.Strings(sorted)
|
||||
value, _ := json.Marshal([]any{sorted, source, target, mode, strings.TrimSpace(remark)})
|
||||
digest := sha256.Sum256(value)
|
||||
return hex.EncodeToString(digest[:])
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package allocaterequest
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRequestIdentity(t *testing.T) {
|
||||
requestID := "alloc_0123456789abcdef"
|
||||
first, err := Key("A", 7, requestID)
|
||||
if err != nil || len(first) != 64 {
|
||||
t.Fatal(first, err)
|
||||
}
|
||||
same, _ := Key("A", 7, requestID)
|
||||
if first != same {
|
||||
t.Fatal("unstable identity")
|
||||
}
|
||||
for _, entry := range []struct {
|
||||
tenant string
|
||||
user int64
|
||||
id string
|
||||
}{{"B", 7, requestID}, {"A", 8, requestID}, {"A", 7, requestID + "2"}} {
|
||||
other, err := Key(entry.tenant, entry.user, entry.id)
|
||||
if err != nil || other == first {
|
||||
t.Fatal("identity scope collision", err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{"", "short", "0123456789abcdef/"} {
|
||||
if _, err := Key("A", 7, id); err == nil {
|
||||
t.Fatal("invalid identity accepted", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFingerprint(t *testing.T) {
|
||||
first := Fingerprint([]string{"B", "A"}, 1, 2, 2, " remark ")
|
||||
if first != Fingerprint([]string{"A", "B"}, 1, 2, 2, "remark") {
|
||||
t.Fatal("card order changed identity")
|
||||
}
|
||||
for _, other := range []string{Fingerprint([]string{"A"}, 1, 2, 2, "remark"), Fingerprint([]string{"A", "B"}, 2, 2, 2, "remark"), Fingerprint([]string{"A", "B"}, 1, 3, 2, "remark"), Fingerprint([]string{"A", "B"}, 1, 2, 1, "remark"), Fingerprint([]string{"A", "B"}, 1, 2, 2, "other")} {
|
||||
if first == other {
|
||||
t.Fatal("changed parameters accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package allocaterequest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
|
||||
)
|
||||
|
||||
func Find(ctx context.Context, tenant string, user int64, requestID string) (*entity.BatchMain, error) {
|
||||
key, err := Key(tenant, user, requestID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var batch *entity.BatchMain
|
||||
columns := dao.BatchMain.Columns()
|
||||
scope := dao.BatchMain.Ctx(ctx).Where(columns.TenantId, tenant).Where(columns.CreatedBy, user)
|
||||
if err = scope.Clone().Where(columns.BatchNo, "ALC"+key[:60]).Scan(&batch); err != nil {
|
||||
return nil, gerror.Wrap(err, "查询历史划拨请求失败")
|
||||
}
|
||||
if batch != nil {
|
||||
return batch, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user