蛋蛋星球RabbitMq消费项目
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.
 
 
 
 
 
 

164 lines
3.9 KiB

  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/syyongx/php2go"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. )
  9. func Implode(glue string, args ...interface{}) string {
  10. data := make([]string, len(args))
  11. for i, s := range args {
  12. data[i] = fmt.Sprint(s)
  13. }
  14. return strings.Join(data, glue)
  15. }
  16. // 字符串是否在数组里
  17. func InArr(target string, str_array []string) bool {
  18. for _, element := range str_array {
  19. if target == element {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. func InArrInt64(target int64, str_array []int64) bool {
  26. for _, element := range str_array {
  27. if target == element {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. // 把数组的值放到key里
  34. func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
  35. result = make(map[string]interface{})
  36. t := reflect.TypeOf(array)
  37. v := reflect.ValueOf(array)
  38. if t.Kind() != reflect.Slice {
  39. return nil, nil
  40. }
  41. if v.Len() == 0 {
  42. return nil, nil
  43. }
  44. for i := 0; i < v.Len(); i++ {
  45. indexv := v.Index(i)
  46. if indexv.Type().Kind() != reflect.Struct {
  47. return nil, nil
  48. }
  49. mapKeyInterface := indexv.FieldByName(key)
  50. if mapKeyInterface.Kind() == reflect.Invalid {
  51. return nil, nil
  52. }
  53. mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
  54. if err != nil {
  55. return nil, err
  56. }
  57. result[mapKeyString] = indexv.Interface()
  58. }
  59. return result, err
  60. }
  61. // 转string
  62. func InterfaceToString(v interface{}) (result string, err error) {
  63. switch reflect.TypeOf(v).Kind() {
  64. case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
  65. result = fmt.Sprintf("%v", v)
  66. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  67. result = fmt.Sprintf("%v", v)
  68. case reflect.String:
  69. result = v.(string)
  70. default:
  71. err = nil
  72. }
  73. return result, err
  74. }
  75. func HideTrueName(name string) string {
  76. res := "**"
  77. if name != "" {
  78. runs := []rune(name)
  79. leng := len(runs)
  80. if leng <= 3 {
  81. res = string(runs[0:1]) + res
  82. } else if leng < 5 {
  83. res = string(runs[0:2]) + res
  84. } else if leng < 10 {
  85. res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
  86. } else if leng < 16 {
  87. res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
  88. } else {
  89. res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
  90. }
  91. }
  92. return res
  93. }
  94. func GetQueryParam(uri string) map[string]string {
  95. //根据问号分割路由还是query参数
  96. uriList := strings.Split(uri, "?")
  97. var query = make(map[string]string, 0)
  98. //有参数才处理
  99. if len(uriList) == 2 {
  100. //分割query参数
  101. var queryList = strings.Split(uriList[1], "&")
  102. if len(queryList) > 0 {
  103. //key value 分别赋值
  104. for _, v := range queryList {
  105. var valueList = strings.Split(v, "=")
  106. if len(valueList) == 2 {
  107. value, _ := php2go.URLDecode(valueList[1])
  108. if value == "" {
  109. value = valueList[1]
  110. }
  111. query[valueList[0]] = value
  112. }
  113. }
  114. }
  115. }
  116. return query
  117. }
  118. // JoinStringsInASCII 按照规则,参数名ASCII码从小到大排序后拼接
  119. // data 待拼接的数据
  120. // sep 连接符
  121. // onlyValues 是否只包含参数值,true则不包含参数名,否则参数名和参数值均有
  122. // includeEmpty 是否包含空值,true则包含空值,否则不包含,注意此参数不影响参数名的存在
  123. // exceptKeys 被排除的参数名,不参与排序及拼接
  124. func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
  125. var list []string
  126. var keyList []string
  127. m := make(map[string]int)
  128. if len(exceptKeys) > 0 {
  129. for _, except := range exceptKeys {
  130. m[except] = 1
  131. }
  132. }
  133. for k := range data {
  134. if _, ok := m[k]; ok {
  135. continue
  136. }
  137. value := data[k]
  138. if !includeEmpty && value == "" {
  139. continue
  140. }
  141. if onlyValues {
  142. keyList = append(keyList, k)
  143. } else {
  144. list = append(list, fmt.Sprintf("%s=%s", k, value))
  145. }
  146. }
  147. if onlyValues {
  148. sort.Strings(keyList)
  149. for _, v := range keyList {
  150. list = append(list, AnyToString(data[v]))
  151. }
  152. } else {
  153. sort.Strings(list)
  154. }
  155. return strings.Join(list, sep)
  156. }