智盟项目
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.

353 lines
6.3 KiB

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