143 lines
5.0 KiB
C#
143 lines
5.0 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Threading.Tasks;
|
||
using Newtonsoft.Json;
|
||
public class loadMall : MonoBehaviour
|
||
{
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法
|
||
selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
|
||
}
|
||
|
||
// 当接收到 token 时触发此方法,仅保存 token
|
||
public void HandleTokenReceived(string receivedToken)
|
||
{
|
||
token = receivedToken; // 保存最新的 token
|
||
// 首次调用加载初始数据
|
||
LoadInitialData();
|
||
//LoadGameEscapeData();
|
||
//Debug.Log("接收到新的 token: " + token);
|
||
}
|
||
|
||
// 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId
|
||
public async void HandleGameEscapeIdUpdated(int newGameEscapeId)
|
||
{
|
||
escapeId = newGameEscapeId; // 保存最新的 escapeId
|
||
await LoadMallData();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器
|
||
//Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
}
|
||
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
await LoadMallData();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要
|
||
}
|
||
|
||
// 加载游戏逃亡数据,使用最新的 escapeId 和 token
|
||
public async void selectQueryKill1()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
if (escapeId == -1)
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。");
|
||
return;
|
||
}
|
||
|
||
await LoadMallData();//==========================================================================================================================================================================
|
||
|
||
}
|
||
|
||
//===============================================================================================================================================================================================================================
|
||
// 加载商城数据
|
||
public async Task LoadMallData()
|
||
{
|
||
// 查询商城虚拟商品列表
|
||
await QueryMallList(0);
|
||
// 查询商城实体商品列表
|
||
await QueryMallList(1);
|
||
// 获取商品详情
|
||
await GetMallProductDetails(1);
|
||
// 购买商品
|
||
await BuyMallProduct(106, 1);
|
||
}
|
||
|
||
// 查询商城列表
|
||
public async Task QueryMallList(int productType)
|
||
{
|
||
var headers = CreateHeaders();
|
||
Mall_List mallList = new Mall_List
|
||
{
|
||
productType = productType
|
||
};
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/page", "POST", JsonUtility.ToJson(mallList), headers);
|
||
Debug.Log($"商城列表 (productType: {productType}) 响应: " + response);
|
||
}
|
||
|
||
// 获取商城商品详情
|
||
public async Task GetMallProductDetails(int productId)
|
||
{
|
||
var headers = CreateHeaders();
|
||
Product_Details productDetails = new Product_Details
|
||
{
|
||
productId = productId
|
||
};
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/info", "POST", JsonUtility.ToJson(productDetails), headers);
|
||
Debug.Log("获取商品详情响应: " + response);
|
||
}
|
||
|
||
// 购买商品
|
||
public async Task BuyMallProduct(int userId, int productId)
|
||
{
|
||
var headers = CreateHeaders();
|
||
Mall_buy mallBuy = new Mall_buy
|
||
{
|
||
userId = userId,
|
||
productId = productId
|
||
};
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/buy", "POST", JsonUtility.ToJson(mallBuy), headers);
|
||
Debug.Log("购买商品响应: " + response);
|
||
}
|
||
//===============================================================================================================================================================================================================================
|
||
|
||
// 创建请求头,使用最新的 token
|
||
public Dictionary<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
}
|
||
} |