package utils import ( "fmt" "github.com/charmbracelet/log" jsoniter "github.com/json-iterator/go" "github.com/json-iterator/go/extra" "io" "net/http" "os" "path/filepath" "strings" ) func init() { // 在init中设置一次即可 extra.RegisterFuzzyDecoders() } func CovertJsonAndDownload(path string, dest string, ossUrl string) (err error) { dataFile := dest + "/someData.json" var json = jsoniter.ConfigCompatibleWithStandardLibrary // Get the data resp, err := http.Get(path) if err != nil { return err } defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) content := string(bodyBytes) //以字串过滤所有url到本地 hasManage := ossUrl + "720yun_fd_manage/" hasEditData := ossUrl + "scene_edit_data/" if strings.Contains(content, hasManage) { content = strings.ReplaceAll(content, "https://4dkk.4dage.com/720yun_fd_manage/", "data/") } if strings.Contains(content, hasEditData) { content = strings.ReplaceAll(content, "https://4dkk.4dage.com/", "data/") } target := &ScenesDataType{} err = json.NewDecoder(strings.NewReader(content)).Decode(&target) if err != nil { fmt.Println(err) return err } //补数据default for _, scene := range target.Scenes { if scene.InitVisual.Vlookatmin == nil { scene.InitVisual.Vlookatmin = -90 } if scene.InitVisual.Vlookatmax == nil { scene.InitVisual.Vlookatmax = 90 } //for _, hotspot := range scene.SomeData.Hotspots { // //} } file, err := json.Marshal(target) if err != nil { panic(err) } //log.Info("data json:", string(file)) // Writer the body to file err = os.WriteFile(dataFile, file, 0644) if err != nil { return err } return nil } func DownloadMainXml(path string, dest string) (err error) { log.Info("DownloadMainXml", "url", fmt.Sprintf("%s", path)) dataFile := dest + "/tour.xml" dir := filepath.Dir(dataFile) err = os.MkdirAll(dir, 0644) if err != nil { return err } // Get the data file, err := os.Create(dataFile) if err != nil { return err } resp, err := http.Get(path) if err != nil { return err } defer resp.Body.Close() _, err = io.Copy(file, resp.Body) if err != nil { return err } return nil }