util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package gopay
  2. import (
  3. "math/rand"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type BodyMap map[string]interface{}
  10. //设置参数
  11. // value:仅支持类型 string,int,int64,float32,float64,ptr,struct,slice,map 类型,其他类型一律设置空字符串
  12. func (bm BodyMap) Set(key string, value interface{}) {
  13. //验证参数类型
  14. vKind := reflect.ValueOf(value).Kind()
  15. //fmt.Println("vKind:", vKind)
  16. switch vKind {
  17. case reflect.String:
  18. bm[key] = value.(string)
  19. case reflect.Int:
  20. bm[key] = Int2String(value.(int))
  21. case reflect.Int64:
  22. bm[key] = Int642String(value.(int64))
  23. case reflect.Float32:
  24. bm[key] = Float32ToString(value.(float32))
  25. case reflect.Float64:
  26. bm[key] = Float64ToString(value.(float64))
  27. case reflect.Ptr:
  28. bm[key] = value
  29. case reflect.Struct:
  30. bm[key] = value
  31. case reflect.Map:
  32. bm[key] = value
  33. case reflect.Slice:
  34. bm[key] = value
  35. default:
  36. bm[key] = ""
  37. }
  38. }
  39. //获取参数
  40. func (bm BodyMap) Get(key string) string {
  41. if bm == nil {
  42. return null
  43. }
  44. value, ok := bm[key]
  45. if !ok {
  46. return null
  47. }
  48. _, ok2 := value.(string)
  49. if ok2 {
  50. return value.(string)
  51. }
  52. return jsonToString(value)
  53. }
  54. //删除参数
  55. func (bm BodyMap) Remove(key string) {
  56. delete(bm, key)
  57. }
  58. //获取随机字符串
  59. // length:字符串长度
  60. func GetRandomString(length int) string {
  61. str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  62. b := []byte(str)
  63. var result []byte
  64. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  65. for i := 0; i < length; i++ {
  66. result = append(result, b[r.Intn(len(b))])
  67. }
  68. return string(result)
  69. }
  70. //func convert2String(value interface{}) (valueStr string) {
  71. // switch v := value.(type) {
  72. // case int:
  73. // valueStr = Int2String(v)
  74. // case int64:
  75. // valueStr = Int642String(v)
  76. // case float64:
  77. // valueStr = Float64ToString(v)
  78. // case float32:
  79. // valueStr = Float32ToString(v)
  80. // case string:
  81. // valueStr = v
  82. // default:
  83. // valueStr = null
  84. // }
  85. // return
  86. //}
  87. //解析时间
  88. func ParseDateTime(timeStr string) (datetime time.Time) {
  89. datetime, _ = time.ParseInLocation(TimeLayout, timeStr, time.Local)
  90. return
  91. }
  92. //格式化Datetime
  93. func FormatDateTime(timeStr string) (formatTime string) {
  94. //2019-01-04T15:40:00Z
  95. //2019-01-18 20:51:30+08:00
  96. if timeStr == null {
  97. return null
  98. }
  99. replace := strings.Replace(timeStr, "T", " ", 1)
  100. formatTime = replace[:19]
  101. return
  102. }
  103. //格式化
  104. func FormatDate(dateStr string) (formatDate string) {
  105. //2020-12-30T00:00:00+08:00
  106. if dateStr == null {
  107. return null
  108. }
  109. split := strings.Split(dateStr, "T")
  110. formatDate = split[0]
  111. return
  112. }
  113. //字符串转Float
  114. func String2Float(floatStr string) (floatNum float64) {
  115. floatNum, _ = strconv.ParseFloat(floatStr, 64)
  116. return
  117. }
  118. //Float64转字符串
  119. // floatNum:float64数字
  120. // prec:精度位数(不传则默认float数字精度)
  121. func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
  122. if len(prec) > 0 {
  123. floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
  124. return
  125. }
  126. floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
  127. return
  128. }
  129. //Float32转字符串
  130. // floatNum:float32数字
  131. // prec:精度位数(不传则默认float数字精度)
  132. func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
  133. if len(prec) > 0 {
  134. floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
  135. return
  136. }
  137. floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
  138. return
  139. }
  140. //字符串转Int
  141. func String2Int(intStr string) (intNum int) {
  142. intNum, _ = strconv.Atoi(intStr)
  143. return
  144. }
  145. //字符串转Int64
  146. func String2Int64(intStr string) (int64Num int64) {
  147. intNum, _ := strconv.Atoi(intStr)
  148. int64Num = int64(intNum)
  149. return
  150. }
  151. //Int转字符串
  152. func Int2String(intNum int) (intStr string) {
  153. intStr = strconv.Itoa(intNum)
  154. return
  155. }
  156. //Int64转字符串
  157. func Int642String(intNum int64) (int64Str string) {
  158. //10, 代表10进制
  159. int64Str = strconv.FormatInt(intNum, 10)
  160. return
  161. }
  162. //解密填充模式(去除补全码) PKCS7UnPadding
  163. //解密时,需要在最后面去掉加密时添加的填充byte
  164. func PKCS7UnPadding(plainText []byte) []byte {
  165. length := len(plainText)
  166. unpadding := int(plainText[length-1]) //找到Byte数组最后的填充byte
  167. return plainText[:(length - unpadding)] //只截取返回有效数字内的byte数组
  168. }
  169. //解密填充模式(去除补全码) PKCS5UnPadding
  170. //解密时,需要在最后面去掉加密时添加的填充byte
  171. func PKCS5UnPadding(origData []byte) []byte {
  172. length := len(origData)
  173. unpadding := int(origData[length-1]) //找到Byte数组最后的填充byte
  174. return origData[:(length - unpadding)] //只截取返回有效数字内的byte数组
  175. }