328 lines
11 KiB
C#
328 lines
11 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Threading.Tasks;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine.UI;
|
||
/*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;
|
||
}
|
||
}*/
|
||
|
||
|
||
|
||
|
||
public class LoadMall : MonoBehaviour
|
||
{
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
private string lastMallResponse = null; // 保存最新的商城操作响应
|
||
|
||
// 假设有一个手动加载商城数据的按钮========================================================================================
|
||
public Button loadMallButton;
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated
|
||
selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
|
||
|
||
// 假设按钮被点击时调用 OnLoadMallButtonClicked========================================================================
|
||
if (loadMallButton != null)
|
||
{
|
||
loadMallButton.onClick.AddListener(OnLoadMallButtonClicked);
|
||
}
|
||
}
|
||
|
||
// 当接收到 token 时触发此方法,仅保存 token
|
||
public void HandleTokenReceived(string receivedToken)
|
||
{
|
||
token = receivedToken; // 保存最新的 token
|
||
Debug.Log("接收到新的 token: " + token);
|
||
|
||
// 首次调用加载初始数据
|
||
LoadInitialData();
|
||
}
|
||
|
||
// 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId
|
||
public async void HandleGameEscapeIdUpdated(int newGameEscapeId)
|
||
{
|
||
escapeId = newGameEscapeId; // 保存最新的 escapeId
|
||
Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
|
||
// 在接收到 escapeId 更新时自动调用加载商城数据
|
||
lastMallResponse = await LoadMallData();
|
||
Debug.Log("HandleGameEscapeIdUpdated 处理的商城数据响应: " + lastMallResponse);
|
||
}
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用加载商城数据方法
|
||
lastMallResponse = await LoadMallData();
|
||
Debug.Log("初始加载的商城数据响应: " + lastMallResponse);
|
||
}
|
||
|
||
// 按钮点击后触发商城数据加载操作
|
||
public async void OnLoadMallButtonClicked()
|
||
{
|
||
// 检查 token 是否已正确设置
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载商城数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用加载商城数据方法
|
||
lastMallResponse = await LoadMallData();
|
||
Debug.Log("用户按钮触发的商城数据加载响应: " + lastMallResponse);
|
||
|
||
// 根据响应做进一步的处理
|
||
HandleMallResponse(lastMallResponse);
|
||
}
|
||
|
||
// 加载商城数据
|
||
public async Task<string> LoadMallData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载商城数据,token 未设置。");
|
||
return null;
|
||
}
|
||
|
||
var headers = CreateHeaders();
|
||
|
||
// 查询商城虚拟商品列表
|
||
await QueryMallList(0);
|
||
// 查询商城实体商品列表
|
||
await QueryMallList(1);
|
||
// 获取商品详情
|
||
await GetMallProductDetails(1);
|
||
// 购买商品
|
||
await BuyMallProduct(106, 1);
|
||
|
||
Debug.Log("商城数据加载完成");
|
||
return "商城数据加载完成";
|
||
}
|
||
|
||
// 查询商城列表
|
||
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 }
|
||
};
|
||
}
|
||
|
||
// 处理商城数据的响应
|
||
private void HandleMallResponse(string response)
|
||
{
|
||
if (response.Contains("完成"))
|
||
{
|
||
Debug.Log("商城数据加载成功!");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("商城数据加载失败,响应: " + response);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
|
||
// 取消按钮的点击监听==============================================================================================
|
||
if (loadMallButton != null)
|
||
{
|
||
loadMallButton.onClick.RemoveListener(OnLoadMallButtonClicked);
|
||
}
|
||
}
|
||
}
|