工具包
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.

182 line
4.3 KiB

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