tools.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "reflect"
  8. "regexp"
  9. "sort"
  10. "strconv"
  11. "time"
  12. )
  13. // IsInStringArray 判断数组中是否存在字符串
  14. func IsInStringArray(str string, arr []string) bool {
  15. // 先对字符串数组进行排序
  16. sort.Strings(arr)
  17. // 使用二分查找算法查找字符串
  18. pos := sort.SearchStrings(arr, str)
  19. // 如果找到了则返回 true,否则返回 false
  20. return pos < len(arr) && arr[pos] == str
  21. }
  22. // structToMap 结构体转map
  23. func structToMap(obj interface{}) map[string]interface{} {
  24. result := make(map[string]interface{})
  25. v := reflect.ValueOf(obj)
  26. t := reflect.TypeOf(obj)
  27. // Ensure the input is a struct
  28. if t.Kind() == reflect.Ptr {
  29. t = t.Elem()
  30. v = v.Elem()
  31. }
  32. if t.Kind() != reflect.Struct {
  33. return nil
  34. }
  35. for i := 0; i < t.NumField(); i++ {
  36. field := t.Field(i)
  37. value := v.Field(i)
  38. // Use the JSON tag if available
  39. tag := field.Tag.Get("json")
  40. if tag == "" {
  41. tag = field.Name
  42. }
  43. result[tag] = value.Interface()
  44. }
  45. return result
  46. }
  47. // GetAreaInfo 调用抽取接口,获取省市区
  48. func GetAreaInfo(data map[string]interface{}) map[string]interface{} {
  49. info := map[string]interface{}{}
  50. client := &http.Client{Timeout: 2 * time.Second}
  51. jsonStr, _ := json.Marshal(data)
  52. resp, err := client.Post("http://127.0.0.1:9996/service/region", "application/json", bytes.NewBuffer(jsonStr))
  53. if err != nil {
  54. return info
  55. }
  56. res, err := io.ReadAll(resp.Body)
  57. if err != nil {
  58. return info
  59. }
  60. err = json.Unmarshal(res, &info)
  61. if err != nil {
  62. return info
  63. }
  64. return info
  65. }
  66. // getTTMethod 时间格式转换
  67. func getTTMethod(str string) int64 {
  68. TimeV1 := regexp.MustCompile("^(\\d{4})[年.]?$")
  69. TimeV2 := regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?$")
  70. TimeV3 := regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?(\\d{1,2})[日]?$")
  71. // Handle "YYYY" format
  72. if TimeV1.MatchString(str) {
  73. arr := TimeV1.FindStringSubmatch(str)
  74. st := arr[1] + "0000"
  75. parseInt, err := strconv.ParseInt(st, 10, 64)
  76. if err == nil {
  77. return parseInt
  78. }
  79. }
  80. // Handle "YYYYMM" or "YYYY/MM" or "YYYY-MM" or "YYYY.MM" format
  81. if TimeV2.MatchString(str) {
  82. arr := TimeV2.FindStringSubmatch(str)
  83. year := arr[1]
  84. month := arr[2]
  85. if len(month) == 1 {
  86. month = "0" + month
  87. }
  88. str2 := year + month + "00"
  89. parseInt, err := strconv.ParseInt(str2, 10, 64)
  90. if err == nil {
  91. return parseInt
  92. }
  93. }
  94. // Handle "YYYYMMDD" or "YYYY/MM/DD" or "YYYY-MM-DD" or "YYYY.MM.DD" format
  95. if TimeV3.MatchString(str) {
  96. match := TimeV3.FindStringSubmatch(str)
  97. if len(match) >= 4 {
  98. year := match[1]
  99. month := match[2]
  100. day := match[3]
  101. if len(month) == 1 {
  102. month = "0" + month
  103. }
  104. if len(day) == 1 {
  105. day = "0" + day
  106. }
  107. dateStr := year + month + day
  108. parseInt, err := strconv.ParseInt(dateStr, 10, 64)
  109. if err == nil {
  110. return parseInt
  111. }
  112. }
  113. }
  114. return 0
  115. }