_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/MallSystem2/productInfo22.cs
2024-11-27 19:08:46 +08:00

65 lines
2.4 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class productInfo22 : MonoBehaviour
{
//public async Task<MallResponsePro> getMallList(int productType, int pageNo, int pageSize)
public async Task<getProductResponse> productInfo(int productId)
{
// 准备请求的头部信息,包含授权令牌
Dictionary<string, string> head22 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
// 请求体
getProdectBody getProdectBody = new getProdectBody
{
productId = productId,
};
Debug.Log("====入参====" + JsonConvert.SerializeObject(getProdectBody));
// 异步发送请求
string response22 = await web.SendRequest(web.URL + "/snail/product/page", "POST", JsonConvert.SerializeObject(getProdectBody), head22);
// 调试输出接收到的响应
Debug.Log("2.1商城列表:=============== " + response22);
getProductResponse getProductResponse1 = new getProductResponse();
try
{
// 将 JSON 字符串反序列化为 MallResponse 对象
getProductResponse1 = JsonConvert.DeserializeObject<getProductResponse>(response22);
// 判空:首先检查 getProductResponse 和 getProductResponse.data 是否为 null
if (getProductResponse1 != null && getProductResponse1.Data != null && getProductResponse1.Data.DataList != null)
{
Debug.Log($"Total Products: {getProductResponse1.Data.DataList.Count}");
// 遍历商品列表
foreach (var product in getProductResponse1.Data.DataList)
{
// 这里输出每个商品的信息
Debug.Log($"Product ID: {product.ProductId}");
Debug.Log($"Product Name: {product.ProductName}");
Debug.Log($"Product Price: {product.Price}");
Debug.Log($"Product Pic: {product.ProductPic}");
Debug.Log($"Product Cover: {product.ProductCover}");
}
}
else
{
Debug.LogWarning("响应数据为空或格式错误");
}
}
catch (Exception ex)
{
Debug.LogError("反序列化响应失败: " + ex.Message);
}
return getProductResponse1;
}
}