第三方api接口
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

261 righe
5.8 KiB

  1. package zhios_third_party_utils
  2. import (
  3. "fmt"
  4. "github.com/syyongx/php2go"
  5. "math/rand"
  6. "reflect"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "time"
  11. "unicode"
  12. "unicode/utf8"
  13. )
  14. func Implode(glue string, args ...interface{}) string {
  15. data := make([]string, len(args))
  16. for i, s := range args {
  17. data[i] = fmt.Sprint(s)
  18. }
  19. return strings.Join(data, glue)
  20. }
  21. //字符串是否在数组里
  22. func InArr(target string, str_array []string) bool {
  23. for _, element := range str_array {
  24. if target == element {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. func InArrToInt(target int, str_array []int) bool {
  31. for _, element := range str_array {
  32. if target == element {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. //是否有中文
  39. func IsChineseChar(str string) bool {
  40. for _, r := range str {
  41. if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
  42. return true
  43. }
  44. }
  45. return false
  46. }
  47. func FilterEmoji(content string) string {
  48. new_content := ""
  49. for _, value := range content {
  50. _, size := utf8.DecodeRuneInString(string(value))
  51. if size <= 3 {
  52. new_content += string(value)
  53. }
  54. }
  55. return new_content
  56. }
  57. func IsAllChinese(str string) bool {
  58. str = FilterEmoji(str)
  59. arr := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "/", ":"}
  60. for _, v := range arr {
  61. str = strings.ReplaceAll(str, v, "")
  62. }
  63. strNew := str
  64. for _, c := range strNew {
  65. if !unicode.Is(unicode.Scripts["Han"], c) {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. //把数组的值放到key里
  72. func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
  73. result = make(map[string]interface{})
  74. t := reflect.TypeOf(array)
  75. v := reflect.ValueOf(array)
  76. if t.Kind() != reflect.Slice {
  77. return nil, nil
  78. }
  79. if v.Len() == 0 {
  80. return nil, nil
  81. }
  82. for i := 0; i < v.Len(); i++ {
  83. indexv := v.Index(i)
  84. if indexv.Type().Kind() != reflect.Struct {
  85. return nil, nil
  86. }
  87. mapKeyInterface := indexv.FieldByName(key)
  88. if mapKeyInterface.Kind() == reflect.Invalid {
  89. return nil, nil
  90. }
  91. mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
  92. if err != nil {
  93. return nil, err
  94. }
  95. result[mapKeyString] = indexv.Interface()
  96. }
  97. return result, err
  98. }
  99. //转string
  100. func InterfaceToString(v interface{}) (result string, err error) {
  101. switch reflect.TypeOf(v).Kind() {
  102. case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
  103. result = fmt.Sprintf("%v", v)
  104. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  105. result = fmt.Sprintf("%v", v)
  106. case reflect.String:
  107. result = v.(string)
  108. default:
  109. err = nil
  110. }
  111. return result, err
  112. }
  113. func HideTrueName(name string) string {
  114. res := "**"
  115. if name != "" {
  116. runs := []rune(name)
  117. leng := len(runs)
  118. if leng <= 3 {
  119. res = string(runs[0:1]) + res
  120. } else if leng < 5 {
  121. res = string(runs[0:2]) + res
  122. } else if leng < 10 {
  123. res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
  124. } else if leng < 16 {
  125. res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
  126. } else {
  127. res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
  128. }
  129. }
  130. return res
  131. }
  132. func GetQueryParam(uri string) map[string]string {
  133. //根据问号分割路由还是query参数
  134. uriList := strings.Split(uri, "?")
  135. var query = make(map[string]string, 0)
  136. //有参数才处理
  137. if len(uriList) == 2 {
  138. //分割query参数
  139. var queryList = strings.Split(uriList[1], "&")
  140. if len(queryList) > 0 {
  141. //key value 分别赋值
  142. for _, v := range queryList {
  143. var valueList = strings.Split(v, "=")
  144. if len(valueList) == 2 {
  145. value, _ := php2go.URLDecode(valueList[1])
  146. if value == "" {
  147. value = valueList[1]
  148. }
  149. query[valueList[0]] = value
  150. }
  151. }
  152. }
  153. }
  154. return query
  155. }
  156. //JoinStringsInASCII 按照规则,参数名ASCII码从小到大排序后拼接
  157. //data 待拼接的数据
  158. //sep 连接符
  159. //onlyValues 是否只包含参数值,true则不包含参数名,否则参数名和参数值均有
  160. //includeEmpty 是否包含空值,true则包含空值,否则不包含,注意此参数不影响参数名的存在
  161. //exceptKeys 被排除的参数名,不参与排序及拼接
  162. func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
  163. var list []string
  164. var keyList []string
  165. m := make(map[string]int)
  166. if len(exceptKeys) > 0 {
  167. for _, except := range exceptKeys {
  168. m[except] = 1
  169. }
  170. }
  171. for k := range data {
  172. if _, ok := m[k]; ok {
  173. continue
  174. }
  175. value := data[k]
  176. if !includeEmpty && value == "" {
  177. continue
  178. }
  179. if onlyValues {
  180. keyList = append(keyList, k)
  181. } else {
  182. list = append(list, fmt.Sprintf("%s=%s", k, value))
  183. }
  184. }
  185. if onlyValues {
  186. sort.Strings(keyList)
  187. for _, v := range keyList {
  188. list = append(list, AnyToString(data[v]))
  189. }
  190. } else {
  191. sort.Strings(list)
  192. }
  193. return strings.Join(list, sep)
  194. }
  195. //x的y次方
  196. func RandPow(l int) string {
  197. var i = "1"
  198. for j := 0; j < l; j++ {
  199. i += "0"
  200. }
  201. k := StrToInt64(i)
  202. n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
  203. ls := "%0" + IntToStr(l) + "v"
  204. str := fmt.Sprintf(ls, n)
  205. //min := int(math.Pow10(l - 1))
  206. //max := int(math.Pow10(l) - 1)
  207. return str
  208. }
  209. //根据显示长度截取字符串
  210. func ShowSubstr(s string, l int) string {
  211. if len(s) <= l {
  212. return s
  213. }
  214. ss, sl, rl, rs := "", 0, 0, []rune(s)
  215. for _, r := range rs {
  216. rint := int(r)
  217. if rint < 128 {
  218. rl = 1
  219. } else {
  220. rl = 2
  221. }
  222. if sl+rl > l {
  223. break
  224. }
  225. sl += rl
  226. ss += string(r)
  227. }
  228. return ss
  229. }
  230. func KsortToStr(params map[string]string) []string {
  231. keys := make([]string, len(params))
  232. i := 0
  233. for k, _ := range params {
  234. keys[i] = k
  235. i++
  236. }
  237. sort.Strings(keys)
  238. return keys
  239. }
  240. func UderscoreToUpperCamelCase(s string) string {
  241. split := strings.Split(s, "_")
  242. if len(split) == 2 {
  243. s = split[0] + strings.Title(split[1])
  244. }
  245. return s
  246. }