123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package main
- import (
- "bytes"
- "encoding/json"
- "io"
- "net/http"
- "reflect"
- "regexp"
- "sort"
- "strconv"
- "time"
- )
- // IsInStringArray 判断数组中是否存在字符串
- func IsInStringArray(str string, arr []string) bool {
- // 先对字符串数组进行排序
- sort.Strings(arr)
- // 使用二分查找算法查找字符串
- pos := sort.SearchStrings(arr, str)
- // 如果找到了则返回 true,否则返回 false
- return pos < len(arr) && arr[pos] == str
- }
- // structToMap 结构体转map
- func structToMap(obj interface{}) map[string]interface{} {
- result := make(map[string]interface{})
- v := reflect.ValueOf(obj)
- t := reflect.TypeOf(obj)
- // Ensure the input is a struct
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- v = v.Elem()
- }
- if t.Kind() != reflect.Struct {
- return nil
- }
- for i := 0; i < t.NumField(); i++ {
- field := t.Field(i)
- value := v.Field(i)
- // Use the JSON tag if available
- tag := field.Tag.Get("json")
- if tag == "" {
- tag = field.Name
- }
- result[tag] = value.Interface()
- }
- return result
- }
- // GetAreaInfo 调用抽取接口,获取省市区
- func GetAreaInfo(data map[string]interface{}) map[string]interface{} {
- info := map[string]interface{}{}
- client := &http.Client{Timeout: 2 * time.Second}
- jsonStr, _ := json.Marshal(data)
- resp, err := client.Post("http://127.0.0.1:9996/service/region", "application/json", bytes.NewBuffer(jsonStr))
- if err != nil {
- return info
- }
- res, err := io.ReadAll(resp.Body)
- if err != nil {
- return info
- }
- err = json.Unmarshal(res, &info)
- if err != nil {
- return info
- }
- return info
- }
- // getTTMethod 时间格式转换
- func getTTMethod(str string) int64 {
- TimeV1 := regexp.MustCompile("^(\\d{4})[年.]?$")
- TimeV2 := regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?$")
- TimeV3 := regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?(\\d{1,2})[日]?$")
- // Handle "YYYY" format
- if TimeV1.MatchString(str) {
- arr := TimeV1.FindStringSubmatch(str)
- st := arr[1] + "0000"
- parseInt, err := strconv.ParseInt(st, 10, 64)
- if err == nil {
- return parseInt
- }
- }
- // Handle "YYYYMM" or "YYYY/MM" or "YYYY-MM" or "YYYY.MM" format
- if TimeV2.MatchString(str) {
- arr := TimeV2.FindStringSubmatch(str)
- year := arr[1]
- month := arr[2]
- if len(month) == 1 {
- month = "0" + month
- }
- str2 := year + month + "00"
- parseInt, err := strconv.ParseInt(str2, 10, 64)
- if err == nil {
- return parseInt
- }
- }
- // Handle "YYYYMMDD" or "YYYY/MM/DD" or "YYYY-MM-DD" or "YYYY.MM.DD" format
- if TimeV3.MatchString(str) {
- match := TimeV3.FindStringSubmatch(str)
- if len(match) >= 4 {
- year := match[1]
- month := match[2]
- day := match[3]
- if len(month) == 1 {
- month = "0" + month
- }
- if len(day) == 1 {
- day = "0" + day
- }
- dateStr := year + month + day
- parseInt, err := strconv.ParseInt(dateStr, 10, 64)
- if err == nil {
- return parseInt
- }
- }
- }
- return 0
- }
|