定制每日抽奖
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.

386 line
6.9 KiB

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