-
Notifications
You must be signed in to change notification settings - Fork 303
/
common.go
79 lines (72 loc) · 1.54 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package utils
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"reflect"
"strconv"
)
// @Title 生成密码
// @Description create AccountAccount
// @Param body body models.AccountAccount true "body for AccountAccount content"
// @Success 201 {int} models.AccountAccount
// @Failure 403 body is empty
func PasswordMD5(passwd, salt string) string {
h := md5.New()
// 后面增加一个无意义字符串
h.Write([]byte(passwd + salt + "@.YnO-"))
cipherStr := h.Sum(nil)
result := hex.EncodeToString(cipherStr)
return result
}
// ToString 类型转换,获得string
func ToString(v interface{}) (re string) {
re = v.(string)
return
}
// StringsJoin 字符串拼接
func StringsJoin(strs ...string) string {
var str string
var b bytes.Buffer
strsLen := len(strs)
if strsLen == 0 {
return str
}
for i := 0; i < strsLen; i++ {
b.WriteString(strs[i])
}
str = b.String()
return str
}
// ToInt64 类型转换,获得int64
func ToInt64(v interface{}) (re int64, err error) {
switch v.(type) {
case string:
re, err = strconv.ParseInt(v.(string), 10, 64)
case float64:
re = int64(v.(float64))
case float32:
re = int64(v.(float32))
case int64:
re = v.(int64)
case int32:
re = v.(int64)
default:
err = errors.New("不能转换")
}
return
}
// ToSlice 转换为数组
func ToSlice(arr interface{}) []interface{} {
v := reflect.ValueOf(arr)
if v.Kind() != reflect.Slice {
panic("toslice arr not slice")
}
l := v.Len()
ret := make([]interface{}, l)
for i := 0; i < l; i++ {
ret[i] = v.Index(i).Interface()
}
return ret
}