支付模块
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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