util.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package commonutil
  2. import (
  3. cryptoRand "crypto/rand"
  4. "fmt"
  5. "io"
  6. "log"
  7. "math/big"
  8. "runtime"
  9. "strconv"
  10. )
  11. // ObjToString obj转string类型
  12. func ObjToString(old interface{}) string {
  13. if nil == old {
  14. return ""
  15. } else {
  16. r, _ := old.(string)
  17. return r
  18. }
  19. }
  20. // IntAll obj转int
  21. func IntAll(num interface{}) int {
  22. return IntAllDef(num, 0)
  23. }
  24. // Int64All obj转int64
  25. func Int64All(num interface{}) int64 {
  26. if i, ok := num.(int64); ok {
  27. return int64(i)
  28. } else if i0, ok0 := num.(int32); ok0 {
  29. return int64(i0)
  30. } else if i1, ok1 := num.(float64); ok1 {
  31. return int64(i1)
  32. } else if i2, ok2 := num.(int); ok2 {
  33. return int64(i2)
  34. } else if i3, ok3 := num.(float32); ok3 {
  35. return int64(i3)
  36. } else if i4, ok4 := num.(string); ok4 {
  37. i64, _ := strconv.ParseInt(i4, 10, 64)
  38. //in, _ := strconv.Atoi(i4)
  39. return i64
  40. } else if i5, ok5 := num.(int16); ok5 {
  41. return int64(i5)
  42. } else if i6, ok6 := num.(int8); ok6 {
  43. return int64(i6)
  44. } else if i7, ok7 := num.(*big.Int); ok7 {
  45. in, _ := strconv.ParseInt(fmt.Sprint(i7), 10, 64)
  46. return int64(in)
  47. } else if i8, ok8 := num.(*big.Float); ok8 {
  48. in, _ := strconv.ParseInt(fmt.Sprint(i8), 10, 64)
  49. return int64(in)
  50. } else {
  51. return 0
  52. }
  53. }
  54. func IntAllDef(num interface{}, defaultNum int) int {
  55. if i, ok := num.(int); ok {
  56. return int(i)
  57. } else if i0, ok0 := num.(int32); ok0 {
  58. return int(i0)
  59. } else if i1, ok1 := num.(float64); ok1 {
  60. return int(i1)
  61. } else if i2, ok2 := num.(int64); ok2 {
  62. return int(i2)
  63. } else if i3, ok3 := num.(float32); ok3 {
  64. return int(i3)
  65. } else if i4, ok4 := num.(string); ok4 {
  66. in, _ := strconv.Atoi(i4)
  67. return int(in)
  68. } else if i5, ok5 := num.(int16); ok5 {
  69. return int(i5)
  70. } else if i6, ok6 := num.(int8); ok6 {
  71. return int(i6)
  72. } else if i7, ok7 := num.(*big.Int); ok7 {
  73. in, _ := strconv.Atoi(fmt.Sprint(i7))
  74. return int(in)
  75. } else if i8, ok8 := num.(*big.Float); ok8 {
  76. in, _ := strconv.Atoi(fmt.Sprint(i8))
  77. return int(in)
  78. } else {
  79. return defaultNum
  80. }
  81. }
  82. // Float64All obj转float64
  83. func Float64All(num interface{}) float64 {
  84. if i, ok := num.(float64); ok {
  85. return float64(i)
  86. } else if i0, ok0 := num.(int32); ok0 {
  87. return float64(i0)
  88. } else if i1, ok1 := num.(int64); ok1 {
  89. return float64(i1)
  90. } else if i2, ok2 := num.(int); ok2 {
  91. return float64(i2)
  92. } else if i3, ok3 := num.(float32); ok3 {
  93. return float64(i3)
  94. } else if i4, ok4 := num.(string); ok4 {
  95. in, _ := strconv.ParseFloat(i4, 64)
  96. return in
  97. } else if i5, ok5 := num.(int16); ok5 {
  98. return float64(i5)
  99. } else if i6, ok6 := num.(int8); ok6 {
  100. return float64(i6)
  101. } else if i6, ok6 := num.(uint); ok6 {
  102. return float64(i6)
  103. } else if i6, ok6 := num.(uint8); ok6 {
  104. return float64(i6)
  105. } else if i6, ok6 := num.(uint16); ok6 {
  106. return float64(i6)
  107. } else if i6, ok6 := num.(uint32); ok6 {
  108. return float64(i6)
  109. } else if i6, ok6 := num.(uint64); ok6 {
  110. return float64(i6)
  111. } else if i7, ok7 := num.(*big.Float); ok7 {
  112. in, _ := strconv.ParseFloat(fmt.Sprint(i7), 64)
  113. return float64(in)
  114. } else if i8, ok8 := num.(*big.Int); ok8 {
  115. in, _ := strconv.ParseFloat(fmt.Sprint(i8), 64)
  116. return float64(in)
  117. } else {
  118. return 0
  119. }
  120. }
  121. // GetLetterRandom 获取复杂的随机数
  122. func GetLetterRandom(length int, flag ...bool) string {
  123. var idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
  124. var mod byte = 52
  125. if len(flag) > 0 && flag[0] {
  126. idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  127. mod = 26
  128. }
  129. b := make([]byte, length)
  130. maxrb := byte(256 - (256 % int(mod)))
  131. i := 0
  132. EXIT:
  133. for {
  134. r := make([]byte, length+(length/4))
  135. if _, err := io.ReadFull(cryptoRand.Reader, r); err != nil {
  136. panic("captcha: error reading random source: " + err.Error())
  137. }
  138. for _, c := range r {
  139. if c > maxrb {
  140. continue
  141. }
  142. b[i] = c % mod
  143. i++
  144. if i == length {
  145. break EXIT
  146. }
  147. }
  148. }
  149. for i, c := range b {
  150. b[i] = idChars[c]
  151. }
  152. return string(b)
  153. }
  154. // Catch 出错拦截
  155. func Catch() {
  156. if r := recover(); r != nil {
  157. log.Println(r)
  158. for skip := 0; ; skip++ {
  159. _, file, line, ok := runtime.Caller(skip)
  160. if !ok {
  161. break
  162. }
  163. go log.Printf("%v,%v\n", file, line)
  164. }
  165. }
  166. }