WorldFind.go 2.08 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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
}