TODO: 反射
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@@ -181,6 +183,10 @@ func getJwtClaims(AuthKey string, AccessSecret *string) (jwt.MapClaims, error) {
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
||||
|
||||
func PasswordHash(pwd string) string {
|
||||
return base64.URLEncoding.EncodeToString(sha256.New().Sum([]byte(pwd)))
|
||||
}
|
||||
|
||||
func CheckValueRange[T comparable](v T, rangevalues ...T) bool {
|
||||
for _, rv := range rangevalues {
|
||||
if v == rv {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -29,6 +32,33 @@ func TestGenJwt(t *testing.T) {
|
||||
// log.Println(claims)
|
||||
}
|
||||
|
||||
func TestGenBackendJwt(t *testing.T) {
|
||||
now := time.Now().Unix()
|
||||
secret := "fusen_backend_2023"
|
||||
a, err := GenerateBackendJwtToken(&secret, 3600*24*7, now, 1, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
log.Println(a)
|
||||
|
||||
claims, err := getJwtClaims(a, &secret)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
userinfo, err := GetBackendUserInfoFormMapClaims(claims)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if userinfo.UserId != 1 || userinfo.DepartmentId != 1 {
|
||||
t.Error(userinfo)
|
||||
}
|
||||
// log.Println(claims)
|
||||
}
|
||||
|
||||
func TestCase1(t *testing.T) {
|
||||
log.Println(base64.URLEncoding.EncodeToString(sha256.New().Sum([]byte("fusen_backend_2023"))))
|
||||
}
|
||||
|
||||
func TestCheckValueRange(t *testing.T) {
|
||||
v := 1
|
||||
rv1 := []int{1, 3, 4}
|
||||
|
||||
@@ -17,10 +17,12 @@ var (
|
||||
CodeUserIdNotFoundErr = &StatusResponse{5051, "user not found"} // 未找到用户
|
||||
CodePasswordErr = &StatusResponse{5052, "invalid password"} // 无效密码
|
||||
|
||||
CodeSafeValueRangeErr = &StatusResponse{5040, "value not in range"} // 值不在范围内
|
||||
CodeSafeValueRangeErr = &StatusResponse{5040, "value not in range"} // 值不在范围内
|
||||
CodeTemplateErr = &StatusResponse{5040, "template parsed error"} // 模板解析错误
|
||||
|
||||
CodeOrderNotFoundErr = &StatusResponse{5030, "order not found"} //未找到订单
|
||||
CodeOrderNotCancelledErr = &StatusResponse{5031, "current order cannot be cancelled"} // 当前订单无法取消
|
||||
CodeOrderNotFoundErr = &StatusResponse{5030, "order not found"} //未找到订单
|
||||
CodeCloudOrderNotFoundErr = &StatusResponse{5031, "cloud order not found"} //未找到云仓订单
|
||||
CodeOrderNotCancelledErr = &StatusResponse{5032, "current order cannot be cancelled"} // 当前订单无法取消
|
||||
|
||||
CodePayNotFoundErr = &StatusResponse{5020, "pay info not found"} // 支付信息无法查询
|
||||
CodePayCancelOk = &StatusResponse{5021, "cancellation successful"} // 支付取消成功
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
@@ -111,7 +112,13 @@ func Array2MapByKey[KEY comparable, VALUE any](arrSrc []VALUE, fieldName string)
|
||||
|
||||
for i := 0; i < arr.Len(); i++ {
|
||||
srcv := arr.Index(i)
|
||||
fv := srcv.Elem().FieldByName(fieldName)
|
||||
if srcv.Kind() == reflect.Ptr {
|
||||
if srcv.IsNil() {
|
||||
continue
|
||||
}
|
||||
srcv = srcv.Elem()
|
||||
}
|
||||
fv := srcv.FieldByName(fieldName)
|
||||
k := fv.Interface().(KEY)
|
||||
result[k] = srcv.Interface().(VALUE)
|
||||
}
|
||||
@@ -119,6 +126,7 @@ func Array2MapByKey[KEY comparable, VALUE any](arrSrc []VALUE, fieldName string)
|
||||
return result
|
||||
}
|
||||
|
||||
// 一个数组slice转换成 map[tag]slice ,以slice元素的某个tag为map
|
||||
func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (result map[KEY]VALUE) {
|
||||
|
||||
defer func() {
|
||||
@@ -147,6 +155,9 @@ func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (r
|
||||
srcv := arr.Index(i)
|
||||
var fv reflect.Value
|
||||
if srcv.Kind() == reflect.Ptr {
|
||||
if srcv.IsNil() {
|
||||
continue
|
||||
}
|
||||
fv = srcv.Elem().Field(j)
|
||||
} else {
|
||||
fv = srcv.Field(j)
|
||||
@@ -169,19 +180,20 @@ func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (r
|
||||
func StructJson2Map(s interface{}) map[string]interface{} {
|
||||
t := reflect.TypeOf(s)
|
||||
v := reflect.ValueOf(s)
|
||||
if v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
var data = make(map[string]interface{})
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if field.Tag == "" {
|
||||
continue
|
||||
if tag, ok := field.Tag.Lookup("json"); ok {
|
||||
val := v.Field(i)
|
||||
if val.Kind() == reflect.Ptr && !val.IsNil() {
|
||||
val = val.Elem()
|
||||
}
|
||||
data[tag] = val.Interface()
|
||||
}
|
||||
tag := field.Tag.Get("json")
|
||||
val := v.Field(i)
|
||||
if val.Kind() == reflect.Ptr {
|
||||
val = val.Elem()
|
||||
}
|
||||
data[tag] = val.Interface()
|
||||
}
|
||||
return data
|
||||
}
|
||||
@@ -193,7 +205,7 @@ func StructSliceJson2Maps(s interface{}) []map[string]interface{} {
|
||||
for i := 0; i < slice.Len(); i++ {
|
||||
s := slice.Index(i)
|
||||
|
||||
if s.Kind() == reflect.Ptr {
|
||||
if s.Kind() == reflect.Ptr && !s.IsNil() {
|
||||
s = s.Elem()
|
||||
}
|
||||
structValue := s.Interface()
|
||||
@@ -202,3 +214,59 @@ func StructSliceJson2Maps(s interface{}) []map[string]interface{} {
|
||||
}
|
||||
return maps
|
||||
}
|
||||
|
||||
func LoadJsonTag(v interface{}, loaded interface{}) {
|
||||
vtype := reflect.TypeOf(v)
|
||||
if vtype.Kind() == reflect.Ptr {
|
||||
vtype = vtype.Elem()
|
||||
}
|
||||
vvalue := reflect.ValueOf(v)
|
||||
if vvalue.Kind() == reflect.Ptr {
|
||||
vvalue = vvalue.Elem()
|
||||
}
|
||||
|
||||
ltype := reflect.TypeOf(loaded)
|
||||
if ltype.Kind() == reflect.Ptr {
|
||||
ltype = ltype.Elem()
|
||||
}
|
||||
lvalue := reflect.ValueOf(loaded)
|
||||
if lvalue.Kind() == reflect.Ptr {
|
||||
lvalue = lvalue.Elem()
|
||||
}
|
||||
|
||||
for i := 0; i < vtype.NumField(); i++ {
|
||||
vfield := vtype.Field(i)
|
||||
|
||||
if vtag, ok := vfield.Tag.Lookup("json"); ok {
|
||||
|
||||
for j := 0; j < ltype.NumField(); j++ {
|
||||
lfield := ltype.Field(j)
|
||||
if ltag, ok := lfield.Tag.Lookup("json"); ok && vtag == ltag {
|
||||
vv := vvalue.Field(i)
|
||||
lv := lvalue.Field(j)
|
||||
log.Println(vv.Kind(), vv.Type().Elem(), lv.Kind())
|
||||
if vv.Kind() == reflect.Ptr {
|
||||
if lv.Kind() == reflect.Ptr {
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
vv = reflect.New(vv.Type().Elem())
|
||||
log.Println(vv.Type().Kind(), vv.Elem().Kind(), lv, reflect.Indirect(vv))
|
||||
reflect.Indirect(vv.Addr()).Set(lv.Addr())
|
||||
vv = reflect.New(vv.Type().Elem())
|
||||
|
||||
vv.Set(lv.Addr())
|
||||
}
|
||||
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
if lv.Kind() != reflect.Ptr {
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
vv.Set(lv.Elem())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ func GetSesssion() *requests.Session {
|
||||
return ses
|
||||
}
|
||||
|
||||
func GetSesssionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
||||
func GetSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
||||
ses := requests.NewSession()
|
||||
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", Host, Port))
|
||||
tp.SetBodyJson(map[string]interface{}{
|
||||
@@ -38,6 +38,32 @@ func GetSesssionWithUserToken(t *testing.T, server requests.ITestServer, Host st
|
||||
return ses
|
||||
}
|
||||
|
||||
func GetBackendSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
||||
ses := requests.NewSession()
|
||||
tp := ses.Post(fmt.Sprintf("http://%s:%d/backend-user/login", Host, Port))
|
||||
tp.SetBodyJson(map[string]interface{}{
|
||||
"name": "admin@admin.com",
|
||||
"pwd": "ZnVzZW5fYmFja2VuZF8yMDIz47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU=",
|
||||
})
|
||||
resp, err := tp.TestExecute(server)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
result := resp.Json()
|
||||
code := result.Get("code").Int()
|
||||
if code != 200 {
|
||||
t.Error("code is not 200")
|
||||
}
|
||||
|
||||
token := result.Get("data.token")
|
||||
if !token.Exists() {
|
||||
t.Error("data.token is not exists")
|
||||
}
|
||||
ses.Header.Add("Authorization", token.String())
|
||||
|
||||
return ses
|
||||
}
|
||||
|
||||
func GetSesssionWithGuestToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
||||
ses := requests.NewSession()
|
||||
tp := ses.Post(fmt.Sprintf("http://%s:%d/accept/cookie", Host, Port))
|
||||
|
||||
Reference in New Issue
Block a user