This commit is contained in:
2022-10-31 17:22:49 +08:00
parent 63c8b46474
commit 9f29f019ad
11 changed files with 96 additions and 99 deletions

View File

@@ -1,6 +1,6 @@
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/26 5:00 下午
* @Desc: TODO
*/
@@ -68,20 +68,20 @@ func String(any interface{}) string {
if v == nil {
return ""
}
if i, ok := v.(stringInterface); ok {
return i.String()
}
if i, ok := v.(errorInterface); ok {
return i.Error()
}
var (
rv = reflect.ValueOf(v)
kind = rv.Kind()
)
switch kind {
case reflect.Chan,
reflect.Map,
@@ -96,11 +96,11 @@ func String(any interface{}) string {
case reflect.String:
return rv.String()
}
if kind == reflect.Ptr {
return String(rv.Elem().Interface())
}
if b, e := json.Marshal(v); e != nil {
return fmt.Sprint(v)
} else {
@@ -211,16 +211,16 @@ func Scan(b []byte, any interface{}) error {
rv = reflect.ValueOf(v)
kind = rv.Kind()
)
if kind != reflect.Ptr {
return fmt.Errorf("can't unmarshal %T", v)
}
switch kind = rv.Elem().Kind(); kind {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
return json.Unmarshal(b, v)
}
return fmt.Errorf("can't unmarshal %T", v)
}
}
@@ -231,4 +231,4 @@ func UnsafeStringToBytes(s string) []byte {
func UnsafeBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
}

View File

@@ -1,6 +1,6 @@
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/26 4:59 下午
* @Desc: TODO
*/
@@ -29,19 +29,19 @@ func SaveToFile(path string, data []byte) error {
return err
}
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0666))
if err != nil {
return err
}
defer f.Close()
if n, err := f.Write(data); err != nil {
return err
} else if n < len(data) {
return io.ErrShortWrite
}
return nil
}

View File

@@ -1,6 +1,6 @@
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/26 4:51 下午
* @Desc: TODO
*/
@@ -17,12 +17,12 @@ var seedStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// RandStr generate a string of specified length.
func RandStr(length int) (lastStr string) {
rand.Seed(time.Now().UnixNano())
pos, seedLen := 0, len(seedStr)
for i := 0; i < length; i++ {
pos = rand.Intn(seedLen)
lastStr += seedStr[pos : pos+1]
}
return lastStr
}

View File

@@ -1,6 +1,6 @@
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/26 6:21 下午
* @Desc: TODO
*/
@@ -81,21 +81,21 @@ func bytesToHexString(stream []byte) string {
if stream == nil || len(stream) <= 0 {
return ""
}
var (
hv string
res = bytes.Buffer{}
temp = make([]byte, 0)
)
for _, v := range stream {
if hv = hex.EncodeToString(append(temp, v&0xFF)); len(hv) < 2 {
res.WriteString(strconv.FormatInt(int64(0), 10))
}
res.WriteString(hv)
}
return res.String()
}
@@ -105,16 +105,16 @@ func GetFileType(stream []byte) string {
fileType string
fileCode = bytesToHexString(stream)
)
fileTypeMap.Range(func(key, value interface{}) bool {
if strings.HasPrefix(fileCode, strings.ToLower(key.(string))) ||
strings.HasPrefix(key.(string), strings.ToLower(fileCode)) {
fileType = value.(string)
return false
}
return true
})
return fileType
}

View File

@@ -1,6 +1,6 @@
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/16 3:47 下午
* @Desc: TODO
*/
@@ -28,9 +28,9 @@ func BuildParams(params interface{}) string {
params = nil
}
}
m := make(map[string]interface{})
if params != nil {
if b, err := json.Marshal(params); err != nil {
return String(params)
@@ -40,25 +40,25 @@ func BuildParams(params interface{}) string {
} else {
return ""
}
urlEncode := true
if len(m) == 0 {
return String(params)
}
for k, v := range m {
if strings.Contains(k, fileUploadingKey) || strings.Contains(String(v), fileUploadingKey) {
urlEncode = false
break
}
}
var (
s = ""
str = ""
)
for k, v := range m {
if len(str) > 0 {
str += "&"
@@ -69,6 +69,6 @@ func BuildParams(params interface{}) string {
}
str += k + "=" + s
}
return str
}