package service import ( "crypto/tls" "encoding/json" "fmt" "strings" "time" "weather_go/models" "github.com/astaxie/beego/httplib" ) type HttpService struct { } func (s *HttpService) GetActivityTyhoon() { now := time.Now() //获取当前时间 timestamp1 := now.Unix() * 1000 //时间戳 fmt.Println("current timestamp1:%l", timestamp1) //time.Sleep(10) //等待1秒 now1 := time.Now() //获取当前时间 timestamp2 := now1.Unix() * 1000 //时间戳 now3 := time.Now() //获取当前时间 timestamp3 := now3.Unix() //时间戳 fmt.Println("current timestamp2:%l", timestamp3) // 两参数格式化 url := fmt.Sprintf("https://typhoon.slt.zj.gov.cn/Api/TyhoonActivity?callback=jQuery1830914871%v_%v_=%v", timestamp3, timestamp1, timestamp2) fmt.Println(url) req := httplib.Get(url) req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) str, err := req.String() if err != nil { fmt.Println(err) } //fmt.Println(str) comLeft := strings.Index(str, "[") pos := str[comLeft:] posLeft := strings.Index(pos, "]") jsonTyhoonActivity := pos[0 : posLeft+1] fmt.Println(jsonTyhoonActivity) var activitys []models.TyhoonActivity json.Unmarshal([]byte(jsonTyhoonActivity), &activitys) fmt.Println(activitys) fmt.Println(len(activitys)) for _, item := range activitys { if models.GetTyhoonActivity(&item) { //存在 fmt.Println("存在") } else { //不存在 models.TyhoonActivityAdd(&item) fmt.Println("不存在,插入数据库") } } activitys = make([]models.TyhoonActivity, 0) //不存在活跃的台风 if len(activitys) == 0 { items := models.GetTyhoonActivityList() if items != nil && len(items) > 0 { for _, item := range items { models.TyhoonActivityDel(item) } } } s.GetTyhoonList(now.Year()) //data, err := json.Marshal(activitys) //if err == nil { // fmt.Println(string(data)) //} } func (s *HttpService) GetTyhoonList(year int) { now := time.Now() //获取当前时间 timestamp1 := now.Unix() * 1000 //时间戳 fmt.Println("current timestamp1:%l", timestamp1) //time.Sleep(10) //等待1秒 now1 := time.Now() //获取当前时间 timestamp2 := now1.Unix() * 1000 //时间戳 now3 := time.Now() //获取当前时间 timestamp3 := now3.Unix() //时间戳 fmt.Println("current timestamp2:%l", timestamp3) // 两参数格式化 url := fmt.Sprintf("https://typhoon.slt.zj.gov.cn/Api/TyphoonList/%v?callback=jQuery18303639632%v_%v_=%v", year, timestamp3, timestamp1, timestamp2) fmt.Println(url) req := httplib.Get(url) req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) str, err := req.String() if err != nil { fmt.Println(err) } //fmt.Println(str) comLeft := strings.Index(str, "[") pos := str[comLeft:] posLeft := strings.Index(pos, "]") jsonTyhoonListItem := pos[0 : posLeft+1] fmt.Println(jsonTyhoonListItem) var tyhoonListItem []models.TyhoonListItem json.Unmarshal([]byte(jsonTyhoonListItem), &tyhoonListItem) fmt.Println(tyhoonListItem) fmt.Println(len(tyhoonListItem)) for _, item := range tyhoonListItem { itemDB := models.GetTyhoonListItem(&item) if itemDB != nil { //存在 fmt.Println("存在") if itemDB.Isactive == "1" && item.Isactive == "0" { //需要更新 itemDB.Isactive = "0" if models.TyhoonListItemUpdate(itemDB) { fmt.Println("update 成功") s.GetTyphoonInfo(item.Tfid) } } if item.Isactive == "1" { //需要更新 s.GetTyphoonInfo(item.Tfid) } } else { //不存在 models.TyhoonListItemAdd(&item) fmt.Println("不存在,插入数据库") s.GetTyphoonInfo(item.Tfid) } } } func (s *HttpService) GetTyphoonInfo(tfid string) { now := time.Now() //获取当前时间 timestamp1 := now.Unix() * 1000 //时间戳 fmt.Println("current timestamp1:%l", timestamp1) //time.Sleep(10) //等待1秒 now1 := time.Now() //获取当前时间 timestamp2 := now1.Unix() * 1000 //时间戳 now3 := time.Now() //获取当前时间 timestamp3 := now3.Unix() //时间戳 fmt.Println("current timestamp2:%l", timestamp3) // 两参数格式化 url := fmt.Sprintf("https://typhoon.slt.zj.gov.cn/Api/TyphoonInfo/%v?callback=jQuery18300926577%v_%v_=%v", tfid, timestamp3, timestamp1, timestamp2) fmt.Println(url) req := httplib.Get(url) req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) str, err := req.String() if err != nil { fmt.Println(err) } //fmt.Println(str) comLeft := strings.Index(str, "[") pos := str[comLeft:] posLeft := strings.LastIndex(pos, "]") jsonTyhoonListItem := pos[0 : posLeft+1] fmt.Println(jsonTyhoonListItem) fmt.Println(posLeft) typhoonInfo := new(models.TyphoonInfo) typhoonInfo.Tfid = tfid typhoonInfoDB := models.GetTyphoonInfo(typhoonInfo) if typhoonInfoDB == nil { typhoonInfo.Typhoon = jsonTyhoonListItem models.TyphoonInfoAdd(typhoonInfo) fmt.Println("不存在,插入数据库") } else { typhoonInfoDB.Typhoon = jsonTyhoonListItem models.TyphoonInfoUpdate(typhoonInfo) fmt.Println("已存在,更新数据库") } }