附近小店
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.
 
 
 

224 regels
5.4 KiB

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