libString.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package lib
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "math"
  7. "math/rand"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // #取得随机字符串:通过打乱slice来操作
  13. func GetRandstring(length int, char string, rand_x int64) string {
  14. if length < 1 {
  15. return ""
  16. }
  17. if len(char) <= 6 || len(char) <= length {
  18. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  19. }
  20. charArr := strings.Split(char, "")
  21. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  22. l := len(charArr)
  23. for i := l - 1; i > 0; i-- {
  24. r := ran.Intn(i)
  25. charArr[r], charArr[i] = charArr[i], charArr[r]
  26. }
  27. rchar := charArr[:length]
  28. return strings.Join(rchar, "")
  29. }
  30. // 返回一个32位md5加密后的字符串
  31. func Md5(str string) string {
  32. h := md5.New()
  33. h.Write([]byte(str))
  34. return hex.EncodeToString(h.Sum(nil))
  35. }
  36. // 转化
  37. func TimeStrToTime(T_time string) (time.Time, bool) {
  38. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local)
  39. if err != nil {
  40. return time.Time{}, false
  41. }
  42. return stamp, true
  43. }
  44. // 转化
  45. func DateStrToDate(T_date string) (time.Time, bool) {
  46. stamp, err := time.ParseInLocation("2006-01-02", T_date, time.Local)
  47. if err != nil {
  48. return time.Time{}, false
  49. }
  50. return stamp, true
  51. }
  52. func SplitStringIds(str string, prefix string) (r []string) {
  53. if len(str) == 0 {
  54. return
  55. }
  56. Ids_str := strings.TrimRight(str, "|")
  57. Ids := strings.Split(Ids_str, "|")
  58. for _, v := range Ids {
  59. r = append(r, strings.TrimLeft(v, prefix))
  60. }
  61. return r
  62. }
  63. func SplitStringToDotStr(str string, prefix string) (r string) {
  64. Ids_str := strings.TrimRight(str, "|")
  65. Ids := strings.Split(Ids_str, "|")
  66. for _, v := range Ids {
  67. r += strings.TrimLeft(v, prefix) + ","
  68. }
  69. r = strings.TrimRight(r, ",")
  70. return r
  71. }
  72. func SplitStringToIntIds(str string, prefix string) (r []int) {
  73. Ids_str := strings.TrimRight(str, "|")
  74. Ids := strings.Split(Ids_str, "|")
  75. for _, v := range Ids {
  76. id, _ := strconv.Atoi(strings.TrimLeft(v, prefix))
  77. r = append(r, id)
  78. }
  79. return r
  80. }
  81. func IntIdsDistinct(Ids []int) (result []int) {
  82. distinctMap := make(map[int]int, len(Ids))
  83. for _, Id := range Ids {
  84. if _, ok := distinctMap[Id]; !ok {
  85. distinctMap[Id] = 1
  86. result = append(result, Id)
  87. }
  88. }
  89. return result
  90. }
  91. func StringListToDotStr(str []string) (r string) {
  92. //for _, v := range str {
  93. // r += v + ","
  94. //}
  95. //r = strings.TrimRight(r, ",")
  96. return strings.Join(str, ",")
  97. }
  98. func IntListToDotStr(list []int) (r string) {
  99. for _, v := range list {
  100. r += fmt.Sprintf("%d,", v)
  101. }
  102. r = strings.TrimRight(r, ",")
  103. return r
  104. }
  105. // ["a","b","c"] ==> "'a','b','c'"
  106. func StringListToQuotesDotStr(str []string) (r string) {
  107. for _, v := range str {
  108. r += fmt.Sprintf("'%s',", v)
  109. }
  110. r = strings.TrimRight(r, ",")
  111. return r
  112. }
  113. // 四舍五入保留小数点后几位
  114. func RoundToDecimal(num float64, decimalPlaces int) float64 {
  115. shift := math.Pow(10, float64(decimalPlaces))
  116. return math.Round(num*shift) / shift
  117. }
  118. // 检查sn是否都相同
  119. // 2023103074298131,0|2023143741715643,0|
  120. func IsSNAllSame(T_snid string) (string, []int, bool) {
  121. entries := strings.Split(strings.Trim(T_snid, "|"), "|")
  122. if len(entries) == 1 {
  123. fields := strings.Split(entries[0], ",")
  124. if len(fields) == 2 {
  125. return fields[0], []int{To_int(fields[1])}, true // 只有一个条目时,认为全相同
  126. } else {
  127. return "", []int{}, false
  128. }
  129. }
  130. firstSN := ""
  131. ids := []int{}
  132. for _, entry := range entries {
  133. fields := strings.Split(entry, ",")
  134. if len(fields) == 2 {
  135. currSN := fields[0]
  136. ids = append(ids, To_int(fields[1]))
  137. if firstSN == "" {
  138. firstSN = currSN
  139. } else if currSN != firstSN {
  140. return "", []int{}, false // 发现不相同的 SN 值,直接返回 false
  141. }
  142. }
  143. }
  144. return firstSN, ids, true
  145. }
  146. func MaskStr(s string) string {
  147. if len([]rune(s)) > 1 {
  148. firstChar := string([]rune(s)[0])
  149. remainingStars := strings.Repeat("*", len([]rune(s))-1)
  150. result := fmt.Sprintf("%s%s", firstChar, remainingStars)
  151. return result
  152. }
  153. return s
  154. }
  155. func MaskPhoneNumber(phoneNumber string) string {
  156. if len(phoneNumber) != 11 {
  157. return phoneNumber // 11位手机号码
  158. }
  159. masked := phoneNumber[:3] + "****" + phoneNumber[7:]
  160. return masked
  161. }