actionService.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "designs/global"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. )
  14. func GetActiveGid() []string {
  15. var activeGidList []string
  16. key := "activeGidList"
  17. activeGid, _ := global.App.Redis.Get(context.Background(), key).Result()
  18. activeGidList = strings.Split(activeGid, ",")
  19. if len(activeGidList) <= 2 {
  20. //if true {
  21. var gidList []string
  22. //
  23. ////重新读取数据
  24. //var dir string
  25. //if config.Get("app.local") == "local" {
  26. // //url = "mongodb://localhost:27017"
  27. // dir = "storage"
  28. //} else {
  29. // dir = "/www/wwwroot/chunhao_receive/storage"
  30. //}
  31. //
  32. //now := time.Now()
  33. //
  34. //for i := 0; i <= 7; i++ {
  35. // date := now.AddDate(0, 0, -i).Format("2006-01-02")
  36. // //读取对应的文件夹
  37. // dirPath := filepath.Join(dir, date)
  38. // dateDir, _ := os.ReadDir(dirPath)
  39. // //fmt.Println(dirPath, dateDir)
  40. //
  41. // for _, v := range dateDir {
  42. //
  43. // fileName := v.Name()
  44. //
  45. // fileNameSplit := strings.Split(fileName, "_")
  46. //
  47. // if len(fileNameSplit) < 2 {
  48. // continue
  49. // }
  50. // last := fileNameSplit[len(fileNameSplit)-1]
  51. // gid := strings.TrimRight(fileName, "_"+last)
  52. //
  53. // if !utils.InArray(gid, gidList) {
  54. // gidList = append(gidList, gid)
  55. // }
  56. // }
  57. //}
  58. //
  59. global.App.DB.Table("user").
  60. Where("createdAt", ">=", time.Now().AddDate(0, 0, -3)).
  61. Distinct("gid").Pluck("gid", &gidList)
  62. if len(gidList) > 0 {
  63. var gidString string
  64. for _, gid := range gidList {
  65. gidString = gid + ","
  66. }
  67. global.App.Redis.Set(context.Background(), key, gidString, time.Second*3600)
  68. activeGidList = gidList
  69. }
  70. }
  71. return activeGidList
  72. }
  73. func CurPost(requestUrl string, requestBody interface{}, headerMap map[string]string) (string, error) {
  74. //转换json
  75. jsonBytes, err := json.Marshal(requestBody)
  76. if err != nil {
  77. return "", err
  78. }
  79. //创建请求
  80. req, err := http.NewRequest("POST", requestUrl, bytes.NewReader(jsonBytes))
  81. if err != nil {
  82. return "", err
  83. }
  84. //设置请求头
  85. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  86. for k, v := range headerMap {
  87. //req.Header.Set("Accept-Encoding", "gzip, deflate, br")
  88. req.Header.Set(k, v)
  89. }
  90. //发送请求
  91. clt := http.Client{Timeout: 30 * time.Second}
  92. res, err := clt.Do(req)
  93. if err != nil {
  94. return "", err
  95. }
  96. //获取结果
  97. body, err := io.ReadAll(res.Body)
  98. data := string(body)
  99. return data, err
  100. }
  101. func CurlGet(url string, values url.Values, header http.Header) (string, error) {
  102. req, reqErr := http.NewRequest(http.MethodGet, url, nil)
  103. if reqErr != nil {
  104. return "", reqErr
  105. }
  106. if header != nil {
  107. req.Header = header
  108. }
  109. if values != nil {
  110. req.URL.RawQuery = values.Encode()
  111. }
  112. fmt.Println(req.URL.Host, req.URL.RawQuery)
  113. resp, resErr := http.DefaultClient.Do(req)
  114. if resErr != nil {
  115. return "", resErr
  116. }
  117. defer func() { _ = resp.Body.Close() }()
  118. content, readErr := io.ReadAll(resp.Body)
  119. if readErr != nil {
  120. return "", readErr
  121. }
  122. //if decodeErr := json.Unmarshal(content, result); decodeErr != nil {
  123. // return "", decodeErr
  124. //}
  125. return string(content), nil
  126. }