package service import ( "WorldEpcho/src/config" "WorldEpcho/src/models" "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" ) // 请求体结构 type WorldFindRequest struct { Auth string `json:"auth"` ISLIUid string `json:"ISLIUid"` } // 响应体结构 type WorldFindResponse struct { Code int `json:"code"` // 根据实际返回情况,可能需要更多字段 } // 请求世界流查询的函数 func FindWorld(url string, request WorldFindRequest) (*WorldFindResponse, error) { //将请求体编码为 JSON requestBytes, err := json.Marshal(request) if err != nil { return nil, err } // 发送POST请求 resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBytes)) if err != nil { return nil, err } defer resp.Body.Close() // 读取响应体 body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } // 解析返回的JSON到响应结构体 var response WorldFindResponse if err = json.Unmarshal(body, &response); err != nil { return nil, err } return &response, nil } func FindWorldISLIU(uid int64, worldName string) (bool, error) { //获取服务商凭证 auth := config.Conf.SoulAuth worldConversation, _, err := models.GetWorldConversationByUidAndWorldName(uid, worldName) if err != nil { log.Println("查询世界意识流id失败") return false, err } fmt.Println("世界意识流id :", worldConversation.IsLiuId) // 构造请求体 WorldFindRequest := WorldFindRequest{ Auth: auth, ISLIUid: worldConversation.IsLiuId, } fmt.Println("创建世界的参数请求体:", WorldFindRequest) //请求url quest_url := config.Conf.SoulUrl + "/world/find" //quest_url := "http://192.168.2.239:13679/world/find" response, err := FindWorld(quest_url, WorldFindRequest) if err != nil { fmt.Println("查询意识流id是否有效出错:", err) return false, err } fmt.Println(config.ColorBlue, "Find world API Code:", response.Code, "code为1成功 2流不存在 0服务失效 3信息格式有误 4非法的服务商", config.ColorReset) if response.Code == 1 { return true, nil } return false, nil }