_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Login/11/loadMall.cs

552 lines
17 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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但不执行其他方法
selectLatest511.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;
selectLatest511.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
selectLatest511.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;
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
// 取消按钮的点击监听==============================================================================================
if (loadMallButton != null)
{
loadMallButton.onClick.RemoveListener(OnLoadMallButtonClicked);
}
}
}*/
//解析+单例
public class LoadMall : MonoBehaviour
{
// 单例模式实现
public static LoadMall Instance { get; private set; }
// 全局变量,用于存储服务器返回的具体字段
public string token = null; // 保存最新的 token初始为 null
public int escapeId = -1; // 保存最新的 escapeId初始为 -1 表示未设置
private string lastMallResponse = null; // 保存最新的商城操作响应
// 服务器返回的字段,作为全局变量
public int code; // 返回状态码,例如 200 表示成功
public string message; // 提示信息
public int pageNo; // 商品分页信息中的起始页
public int pageSize; // 商品分页信息中的每页展示
public int totalCount; // 商品分页信息中的总记录数
public List<Product> productList; // 商品数据列表
// 假设有一个手动加载商城数据的按钮
public Button loadMallButton;
void Awake()
{
// 实现单例模式
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // 保证此实例在场景切换时不会被销毁
}
else
{
Destroy(gameObject);
}
}
void Start()
{
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated
selectLatest511.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);
// 解析商城数据响应
ParseMallResponse(lastMallResponse);
}
// 加载初始数据,使用最新的 token
public async void LoadInitialData()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载初始数据token 未设置。");
return;
}
// 调用加载商城数据方法
lastMallResponse = await LoadMallData();
Debug.Log("初始加载的商城数据响应: " + lastMallResponse);
// 解析商城数据响应
ParseMallResponse(lastMallResponse);
}
// 按钮点击后触发商城数据加载操作
public async void OnLoadMallButtonClicked()
{
// 检查 token 是否已正确设置
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载商城数据token 未设置。");
return;
}
// 调用加载商城数据方法
lastMallResponse = await LoadMallData();
Debug.Log("用户按钮触发的商城数据加载响应: " + lastMallResponse);
// 解析商城数据响应
ParseMallResponse(lastMallResponse);
}
// 加载商城数据
public async Task<string> LoadMallData()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载商城数据token 未设置。");
return null;
}
var headers = CreateHeaders();
string body = @"{}"; // 这里可以根据需要修改请求参数
Debug.Log("正在加载商城数据...");
string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/page", "POST", body, headers);
Debug.Log("商城数据响应: " + response);
return 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 ParseMallResponse(string response)
{
if (string.IsNullOrEmpty(response))
{
Debug.LogWarning("无法解析商城数据响应,响应为空。");
return;
}
try
{
// 使用 Newtonsoft.Json 解析响应
MallResponse mallResponse = JsonConvert.DeserializeObject<MallResponse>(response);
// 将解析后的数据赋值给全局变量
code = mallResponse.code;
message = mallResponse.message;
if (mallResponse.data != null)
{
pageNo = mallResponse.data.pageNo;
pageSize = mallResponse.data.pageSize;
totalCount = mallResponse.data.totalCount;
productList = mallResponse.data.dataList;
// 打印商品列表信息
foreach (var product in productList)
{
Debug.Log($"商品 ID: {product.productId}, 名称: {product.name}, 价格: {product.price}");
}
}
else
{
Debug.LogWarning("商城数据对象为空,无法获取详细信息");
}
}
catch (System.Exception e)
{
Debug.LogWarning("解析商城数据响应时出错: " + e.Message);
}
}
void OnDestroy()
{
// 注销监听事件,避免内存泄漏
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
// 取消按钮的点击监听
if (loadMallButton != null)
{
loadMallButton.onClick.RemoveListener(OnLoadMallButtonClicked);
}
}
}
// 商城数据响应类,用于存储解析后的响应数据
public class MallResponse
{
public int code { get; set; } // 返回状态码,例如 200 表示成功
public string message { get; set; } // 提示信息
public MallData data { get; set; } // 商城分页信息
}
// 商城分页数据类,用于存储分页详细信息
public class MallData
{
public int pageNo { get; set; } // 起始页
public int pageSize { get; set; } // 每页展示
public int totalCount { get; set; } // 总记录数
public List<Product> dataList { get; set; } // 商品数据列表
}
// 商品类,用于存储商品的详细信息
public class Product
{
public int productId { get; set; } // 商品ID
public string name { get; set; } // 商品名称
public string cover { get; set; } // 商品封面
public string pic { get; set; } // 商品图片
public float price { get; set; } // 商品价格
}