123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package commonutil
- import (
- cryptoRand "crypto/rand"
- "fmt"
- "io"
- "log"
- "math/big"
- "runtime"
- "strconv"
- )
- // ObjToString obj转string类型
- func ObjToString(old interface{}) string {
- if nil == old {
- return ""
- } else {
- r, _ := old.(string)
- return r
- }
- }
- // IntAll obj转int
- func IntAll(num interface{}) int {
- return IntAllDef(num, 0)
- }
- // Int64All obj转int64
- func Int64All(num interface{}) int64 {
- if i, ok := num.(int64); ok {
- return int64(i)
- } else if i0, ok0 := num.(int32); ok0 {
- return int64(i0)
- } else if i1, ok1 := num.(float64); ok1 {
- return int64(i1)
- } else if i2, ok2 := num.(int); ok2 {
- return int64(i2)
- } else if i3, ok3 := num.(float32); ok3 {
- return int64(i3)
- } else if i4, ok4 := num.(string); ok4 {
- i64, _ := strconv.ParseInt(i4, 10, 64)
- //in, _ := strconv.Atoi(i4)
- return i64
- } else if i5, ok5 := num.(int16); ok5 {
- return int64(i5)
- } else if i6, ok6 := num.(int8); ok6 {
- return int64(i6)
- } else if i7, ok7 := num.(*big.Int); ok7 {
- in, _ := strconv.ParseInt(fmt.Sprint(i7), 10, 64)
- return int64(in)
- } else if i8, ok8 := num.(*big.Float); ok8 {
- in, _ := strconv.ParseInt(fmt.Sprint(i8), 10, 64)
- return int64(in)
- } else {
- return 0
- }
- }
- func IntAllDef(num interface{}, defaultNum int) int {
- if i, ok := num.(int); ok {
- return int(i)
- } else if i0, ok0 := num.(int32); ok0 {
- return int(i0)
- } else if i1, ok1 := num.(float64); ok1 {
- return int(i1)
- } else if i2, ok2 := num.(int64); ok2 {
- return int(i2)
- } else if i3, ok3 := num.(float32); ok3 {
- return int(i3)
- } else if i4, ok4 := num.(string); ok4 {
- in, _ := strconv.Atoi(i4)
- return int(in)
- } else if i5, ok5 := num.(int16); ok5 {
- return int(i5)
- } else if i6, ok6 := num.(int8); ok6 {
- return int(i6)
- } else if i7, ok7 := num.(*big.Int); ok7 {
- in, _ := strconv.Atoi(fmt.Sprint(i7))
- return int(in)
- } else if i8, ok8 := num.(*big.Float); ok8 {
- in, _ := strconv.Atoi(fmt.Sprint(i8))
- return int(in)
- } else {
- return defaultNum
- }
- }
- // Float64All obj转float64
- func Float64All(num interface{}) float64 {
- if i, ok := num.(float64); ok {
- return float64(i)
- } else if i0, ok0 := num.(int32); ok0 {
- return float64(i0)
- } else if i1, ok1 := num.(int64); ok1 {
- return float64(i1)
- } else if i2, ok2 := num.(int); ok2 {
- return float64(i2)
- } else if i3, ok3 := num.(float32); ok3 {
- return float64(i3)
- } else if i4, ok4 := num.(string); ok4 {
- in, _ := strconv.ParseFloat(i4, 64)
- return in
- } else if i5, ok5 := num.(int16); ok5 {
- return float64(i5)
- } else if i6, ok6 := num.(int8); ok6 {
- return float64(i6)
- } else if i6, ok6 := num.(uint); ok6 {
- return float64(i6)
- } else if i6, ok6 := num.(uint8); ok6 {
- return float64(i6)
- } else if i6, ok6 := num.(uint16); ok6 {
- return float64(i6)
- } else if i6, ok6 := num.(uint32); ok6 {
- return float64(i6)
- } else if i6, ok6 := num.(uint64); ok6 {
- return float64(i6)
- } else if i7, ok7 := num.(*big.Float); ok7 {
- in, _ := strconv.ParseFloat(fmt.Sprint(i7), 64)
- return float64(in)
- } else if i8, ok8 := num.(*big.Int); ok8 {
- in, _ := strconv.ParseFloat(fmt.Sprint(i8), 64)
- return float64(in)
- } else {
- return 0
- }
- }
- // GetLetterRandom 获取复杂的随机数
- func GetLetterRandom(length int, flag ...bool) string {
- var idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
- var mod byte = 52
- if len(flag) > 0 && flag[0] {
- idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- mod = 26
- }
- b := make([]byte, length)
- maxrb := byte(256 - (256 % int(mod)))
- i := 0
- EXIT:
- for {
- r := make([]byte, length+(length/4))
- if _, err := io.ReadFull(cryptoRand.Reader, r); err != nil {
- panic("captcha: error reading random source: " + err.Error())
- }
- for _, c := range r {
- if c > maxrb {
- continue
- }
- b[i] = c % mod
- i++
- if i == length {
- break EXIT
- }
- }
- }
- for i, c := range b {
- b[i] = idChars[c]
- }
- return string(b)
- }
- // Catch 出错拦截
- func Catch() {
- if r := recover(); r != nil {
- log.Println(r)
- for skip := 0; ; skip++ {
- _, file, line, ok := runtime.Caller(skip)
- if !ok {
- break
- }
- go log.Printf("%v,%v\n", file, line)
- }
- }
- }
|