智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 年之前
2 月之前
1 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package utils
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. const CODE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11. const CODE_LENTH = 62
  12. var EDOC = map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16, "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23, "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30, "v": 31, "w": 32, "x": 33, "y": 34, "z": 35, "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42, "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49, "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56, "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61}
  13. func Base62Encode(number int) string {
  14. if number == 0 {
  15. return "0"
  16. }
  17. result := make([]byte, 0)
  18. for number > 0 {
  19. round := number / CODE_LENTH
  20. remain := number % CODE_LENTH
  21. result = append(result, CODE62[remain])
  22. number = round
  23. }
  24. return string(result)
  25. }
  26. /**
  27. * 解码字符串为整数
  28. */
  29. func Base62Decode(str string) int {
  30. str = strings.TrimSpace(str)
  31. var result int = 0
  32. for index, char := range []byte(str) {
  33. result += EDOC[string(char)] * int(math.Pow(CODE_LENTH, float64(index)))
  34. }
  35. return result
  36. }
  37. func ToString(raw interface{}, e error) (res string) {
  38. if e != nil {
  39. return ""
  40. }
  41. return AnyToString(raw)
  42. }
  43. func ToInt64(raw interface{}, e error) int64 {
  44. if e != nil {
  45. return 0
  46. }
  47. return AnyToInt64(raw)
  48. }
  49. func AnyToBool(raw interface{}) bool {
  50. switch i := raw.(type) {
  51. case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
  52. return i != 0
  53. case []byte:
  54. return i != nil
  55. case string:
  56. if i == "false" {
  57. return false
  58. }
  59. return i != ""
  60. case error:
  61. return false
  62. case nil:
  63. return true
  64. }
  65. val := fmt.Sprint(raw)
  66. val = strings.TrimLeft(val, "&")
  67. if strings.TrimLeft(val, "{}") == "" {
  68. return false
  69. }
  70. if strings.TrimLeft(val, "[]") == "" {
  71. return false
  72. }
  73. // ptr type
  74. b, err := json.Marshal(raw)
  75. if err != nil {
  76. return false
  77. }
  78. if strings.TrimLeft(string(b), "\"\"") == "" {
  79. return false
  80. }
  81. if strings.TrimLeft(string(b), "{}") == "" {
  82. return false
  83. }
  84. return true
  85. }
  86. func AnyToInt64(raw interface{}) int64 {
  87. switch i := raw.(type) {
  88. case string:
  89. res, _ := strconv.ParseInt(i, 10, 64)
  90. return res
  91. case []byte:
  92. return BytesToInt64(i)
  93. case int:
  94. return int64(i)
  95. case int64:
  96. return i
  97. case uint:
  98. return int64(i)
  99. case uint8:
  100. return int64(i)
  101. case uint16:
  102. return int64(i)
  103. case uint32:
  104. return int64(i)
  105. case uint64:
  106. return int64(i)
  107. case int8:
  108. return int64(i)
  109. case int16:
  110. return int64(i)
  111. case int32:
  112. return int64(i)
  113. case float32:
  114. return int64(i)
  115. case float64:
  116. return int64(i)
  117. case error:
  118. return 0
  119. case bool:
  120. if i {
  121. return 1
  122. }
  123. return 0
  124. }
  125. return 0
  126. }
  127. func AnyToString(raw interface{}) string {
  128. switch i := raw.(type) {
  129. case []byte:
  130. return string(i)
  131. case int:
  132. return strconv.FormatInt(int64(i), 10)
  133. case int64:
  134. return strconv.FormatInt(i, 10)
  135. case float32:
  136. return Float64ToStr(float64(i))
  137. case float64:
  138. return Float64ToStr(i)
  139. case uint:
  140. return strconv.FormatInt(int64(i), 10)
  141. case uint8:
  142. return strconv.FormatInt(int64(i), 10)
  143. case uint16:
  144. return strconv.FormatInt(int64(i), 10)
  145. case uint32:
  146. return strconv.FormatInt(int64(i), 10)
  147. case uint64:
  148. return strconv.FormatInt(int64(i), 10)
  149. case int8:
  150. return strconv.FormatInt(int64(i), 10)
  151. case int16:
  152. return strconv.FormatInt(int64(i), 10)
  153. case int32:
  154. return strconv.FormatInt(int64(i), 10)
  155. case string:
  156. return i
  157. case error:
  158. return i.Error()
  159. case bool:
  160. return strconv.FormatBool(i)
  161. }
  162. return fmt.Sprintf("%#v", raw)
  163. }
  164. func AnyToFloat64(raw interface{}) float64 {
  165. switch i := raw.(type) {
  166. case []byte:
  167. f, _ := strconv.ParseFloat(string(i), 64)
  168. return f
  169. case int:
  170. return float64(i)
  171. case int64:
  172. return float64(i)
  173. case float32:
  174. return float64(i)
  175. case float64:
  176. return i
  177. case uint:
  178. return float64(i)
  179. case uint8:
  180. return float64(i)
  181. case uint16:
  182. return float64(i)
  183. case uint32:
  184. return float64(i)
  185. case uint64:
  186. return float64(i)
  187. case int8:
  188. return float64(i)
  189. case int16:
  190. return float64(i)
  191. case int32:
  192. return float64(i)
  193. case string:
  194. f, _ := strconv.ParseFloat(i, 64)
  195. return f
  196. case bool:
  197. if i {
  198. return 1
  199. }
  200. }
  201. return 0
  202. }
  203. func ToByte(raw interface{}, e error) []byte {
  204. if e != nil {
  205. return []byte{}
  206. }
  207. switch i := raw.(type) {
  208. case string:
  209. return []byte(i)
  210. case int:
  211. return Int64ToBytes(int64(i))
  212. case int64:
  213. return Int64ToBytes(i)
  214. case float32:
  215. return Float32ToByte(i)
  216. case float64:
  217. return Float64ToByte(i)
  218. case uint:
  219. return Int64ToBytes(int64(i))
  220. case uint8:
  221. return Int64ToBytes(int64(i))
  222. case uint16:
  223. return Int64ToBytes(int64(i))
  224. case uint32:
  225. return Int64ToBytes(int64(i))
  226. case uint64:
  227. return Int64ToBytes(int64(i))
  228. case int8:
  229. return Int64ToBytes(int64(i))
  230. case int16:
  231. return Int64ToBytes(int64(i))
  232. case int32:
  233. return Int64ToBytes(int64(i))
  234. case []byte:
  235. return i
  236. case error:
  237. return []byte(i.Error())
  238. case bool:
  239. if i {
  240. return []byte("true")
  241. }
  242. return []byte("false")
  243. }
  244. return []byte(fmt.Sprintf("%#v", raw))
  245. }
  246. func Int64ToBytes(i int64) []byte {
  247. var buf = make([]byte, 8)
  248. binary.BigEndian.PutUint64(buf, uint64(i))
  249. return buf
  250. }
  251. func BytesToInt64(buf []byte) int64 {
  252. return int64(binary.BigEndian.Uint64(buf))
  253. }
  254. func StrToInt(s string) int {
  255. res, _ := strconv.Atoi(s)
  256. return res
  257. }
  258. func StrToInt64(s string) int64 {
  259. res, _ := strconv.ParseInt(s, 10, 64)
  260. return res
  261. }
  262. func Float32ToByte(float float32) []byte {
  263. bits := math.Float32bits(float)
  264. bytes := make([]byte, 4)
  265. binary.LittleEndian.PutUint32(bytes, bits)
  266. return bytes
  267. }
  268. func ByteToFloat32(bytes []byte) float32 {
  269. bits := binary.LittleEndian.Uint32(bytes)
  270. return math.Float32frombits(bits)
  271. }
  272. func Float64ToByte(float float64) []byte {
  273. bits := math.Float64bits(float)
  274. bytes := make([]byte, 8)
  275. binary.LittleEndian.PutUint64(bytes, bits)
  276. return bytes
  277. }
  278. func ByteToFloat64(bytes []byte) float64 {
  279. bits := binary.LittleEndian.Uint64(bytes)
  280. return math.Float64frombits(bits)
  281. }
  282. func Float64ToStr(f float64) string {
  283. return strconv.FormatFloat(f, 'f', 2, 64)
  284. }
  285. func Float64ToStrPrec1(f float64) string {
  286. return strconv.FormatFloat(f, 'f', 1, 64)
  287. }
  288. func Float32ToStr(f float32) string {
  289. return Float64ToStr(float64(f))
  290. }
  291. func StrToFloat64(s string) float64 {
  292. res, err := strconv.ParseFloat(s, 64)
  293. if err != nil {
  294. return 0
  295. }
  296. return res
  297. }
  298. func StrToFloat32(s string) float32 {
  299. res, err := strconv.ParseFloat(s, 32)
  300. if err != nil {
  301. return 0
  302. }
  303. return float32(res)
  304. }
  305. func StrToBool(s string) bool {
  306. b, _ := strconv.ParseBool(s)
  307. return b
  308. }
  309. func BoolToStr(b bool) string {
  310. if b {
  311. return "true"
  312. }
  313. return "false"
  314. }
  315. func FloatToInt64(f float64) int64 {
  316. return int64(f)
  317. }
  318. func IntToStr(i int) string {
  319. return strconv.Itoa(i)
  320. }
  321. func Int64ToStr(i int64) string {
  322. return strconv.FormatInt(i, 10)
  323. }