| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package service
- import (
- "bytes"
- "context"
- "designs/global"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "strings"
- "time"
- )
- func GetActiveGid() []string {
- var activeGidList []string
- key := "activeGidList"
- activeGid, _ := global.App.Redis.Get(context.Background(), key).Result()
- activeGidList = strings.Split(activeGid, ",")
- if len(activeGidList) <= 2 {
- //if true {
- var gidList []string
- //
- ////重新读取数据
- //var dir string
- //if config.Get("app.local") == "local" {
- // //url = "mongodb://localhost:27017"
- // dir = "storage"
- //} else {
- // dir = "/www/wwwroot/chunhao_receive/storage"
- //}
- //
- //now := time.Now()
- //
- //for i := 0; i <= 7; i++ {
- // date := now.AddDate(0, 0, -i).Format("2006-01-02")
- // //读取对应的文件夹
- // dirPath := filepath.Join(dir, date)
- // dateDir, _ := os.ReadDir(dirPath)
- // //fmt.Println(dirPath, dateDir)
- //
- // for _, v := range dateDir {
- //
- // fileName := v.Name()
- //
- // fileNameSplit := strings.Split(fileName, "_")
- //
- // if len(fileNameSplit) < 2 {
- // continue
- // }
- // last := fileNameSplit[len(fileNameSplit)-1]
- // gid := strings.TrimRight(fileName, "_"+last)
- //
- // if !utils.InArray(gid, gidList) {
- // gidList = append(gidList, gid)
- // }
- // }
- //}
- //
- global.App.DB.Table("user").
- Where("createdAt", ">=", time.Now().AddDate(0, 0, -3)).
- Distinct("gid").Pluck("gid", &gidList)
- if len(gidList) > 0 {
- var gidString string
- for _, gid := range gidList {
- gidString = gid + ","
- }
- global.App.Redis.Set(context.Background(), key, gidString, time.Second*3600)
- activeGidList = gidList
- }
- }
- return activeGidList
- }
- func CurPost(requestUrl string, requestBody interface{}, headerMap map[string]string) (string, error) {
- //转换json
- jsonBytes, err := json.Marshal(requestBody)
- if err != nil {
- return "", err
- }
- //创建请求
- req, err := http.NewRequest("POST", requestUrl, bytes.NewReader(jsonBytes))
- if err != nil {
- return "", err
- }
- //设置请求头
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- for k, v := range headerMap {
- //req.Header.Set("Accept-Encoding", "gzip, deflate, br")
- req.Header.Set(k, v)
- }
- //发送请求
- clt := http.Client{Timeout: 30 * time.Second}
- res, err := clt.Do(req)
- if err != nil {
- return "", err
- }
- //获取结果
- body, err := io.ReadAll(res.Body)
- data := string(body)
- return data, err
- }
- func CurlGet(url string, values url.Values, header http.Header) (string, error) {
- req, reqErr := http.NewRequest(http.MethodGet, url, nil)
- if reqErr != nil {
- return "", reqErr
- }
- if header != nil {
- req.Header = header
- }
- if values != nil {
- req.URL.RawQuery = values.Encode()
- }
- fmt.Println(req.URL.Host, req.URL.RawQuery)
- resp, resErr := http.DefaultClient.Do(req)
- if resErr != nil {
- return "", resErr
- }
- defer func() { _ = resp.Body.Close() }()
- content, readErr := io.ReadAll(resp.Body)
- if readErr != nil {
- return "", readErr
- }
- //if decodeErr := json.Unmarshal(content, result); decodeErr != nil {
- // return "", decodeErr
- //}
- return string(content), nil
- }
|