package nowcar import ( "bytes" "film/dao" "film/model" "fmt" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "io" "net/http" "net/url" "time" ) func Sync() { devIds, err := getDevId() if err != nil { return } for _, devId := range devIds { if devId == "" { continue } getInfo(devId) } } func getInfo(devId string) ([]string, error) { reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/getInfo" params := make(url.Values) params.Set("user_lng", "106.55072784423828") params.Set("user_lat", "29.56471061706543") params.Set("dev_id", devId) params.Set("lt-id", "1811") params.Set("lt-token", "c5e139a7e118b346149750489c57560b") resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(params.Encode()))) if err != nil { log.Error("http poset error :%s", err.Error()) return nil, err } defer resp.Body.Close() rspbody, _ := io.ReadAll(resp.Body) code := gjson.GetBytes(rspbody, "code").Int() if code != 0 { log.Errorf("devId:%s rspbody error :%s", devId, string(rspbody)) return nil, fmt.Errorf("code is :%d", code) } data := gjson.GetBytes(rspbody, "data") shopName := data.Get("addr_name").String() doors := data.Get("doors").Array() m := new(model.Nowcar) m.ShopName = shopName for _, door := range doors { workstation := door.Get("number").String() status := door.Get("status").String() if status != "空闲" { startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", status, time.Local) m1, err := dao.NewNowCarDao().GetStart(shopName, workstation, startTime.Unix()) if err != nil { log.Errorf("db error :%s", err.Error()) continue } if m1 != nil { continue } m.Id = 0 m.Day = time.Now().Format("2006-01-02") m.Workstation = workstation m.StartTime = startTime.Unix() dao.NewNowCarDao().Create(m) } else { m2, err := dao.NewNowCarDao().GetBusy(shopName, workstation) if err != nil { log.Errorf("db error :%s", err.Error()) continue } if m2 == nil || m2.EndTime != 0 { continue } m2.EndTime = time.Now().Unix() err = dao.NewNowCarDao().Update(m2) if err != nil { log.Errorf("db error :%s", err.Error()) } } } return nil, nil } func getDevId() ([]string, error) { reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/nearSite" params := make(url.Values) params.Set("user_lng", "106.55072784423828") params.Set("user_lat", "29.56471061706543") resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(params.Encode()))) if err != nil { log.Error("http poset error :%s", err.Error()) return nil, err } defer resp.Body.Close() rspbody, _ := io.ReadAll(resp.Body) code := gjson.GetBytes(rspbody, "code").Int() if code != 0 { log.Errorf("rspbody error :%s", string(rspbody)) return nil, fmt.Errorf("code is :%d", code) } devids := make([]string, 0) results := gjson.GetBytes(rspbody, "data").Array() for _, res := range results { devId := res.Get("dev_id").String() shopName := res.Get("addr_name").String() log.Infof("shop=%s devid=%s", shopName, devId) devids = append(devids, devId) } return devids, nil }