蛋蛋星球 后台端
 
 
 
 

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