_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs
2024-12-13 15:27:51 +08:00

216 lines
6.6 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 Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.IO;
using System.Threading.Tasks;
using UnityEngine.Networking;
public class EditPanel : Base
{
public UserInfomation14 userInfomation14;
public InputField UserInputField;
public Text username;
public PerSonalCenterPanel perSonalCenterPanel;
public Avaterdata avaterdata;
public Button avatar;
public Image AvaImage;
// Start is called before the first frame update
async void Start()
{
Dictionary<string, string> head14 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
// 异步查询玩家信息
string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14);
Debug.Log("1.4查询玩家信息" + response14);
userInfomation14 = JsonConvert.DeserializeObject<UserInfomation14>(response14);
UserInputField.text = "" + userInfomation14.data.nickName;
username.text = "" + userInfomation14.data.nickName;
avatar.onClick.AddListener(selectavatar);
AvaImage.sprite = await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(userInfomation14.data.headImg);
}
void selectavatar()
{
PickImage(512);
}
private void PickImage(int maxSize)
{
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create Texture from selected image
Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
if (texture == null)
{
Debug.Log("Couldn't load texture from " + path);
return;
}
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
// 设置 AvaImage 的 sprite
//AvaImage.sprite = sprite;
byte[] fileBytes = File.ReadAllBytes(path);
SetAvater(path);
}
});
Debug.Log("Permission result: " + permission);
}
// Update is called once per frame
// 设置头像的方法
public async void SetAvater(string filepath)
{
// 检查文件是否存在
if (!File.Exists(filepath))
{
Debug.LogError("文件不存在:" + filepath);
return;
}
// 读取文件为字节数组
byte[] fileBytes = File.ReadAllBytes(filepath);
// 创建 HTTP 请求头部
Dictionary<string, string> head13 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
Debug.Log("入参-----------------------------1.17");
// 构建请求
UnityWebRequest request = CreateFileUploadRequest(filepath, fileBytes, head13);
// 发送请求并等待响应
await SendFileUploadRequest(request);
}
// 创建文件上传的 UnityWebRequest
private UnityWebRequest CreateFileUploadRequest(string filepath, byte[] fileBytes, Dictionary<string, string> headers)
{
string url = web.URL + "/snail/file/upload"; // 文件上传的接口 URL
// 创建 WWWForm 表单
WWWForm form = new WWWForm();
// 获取文件名并添加到表单("file" 为字段名)
string fileName = Path.GetFileName(filepath);
form.AddBinaryData("file", fileBytes, fileName, "image/jpeg"); // 这里 MIME 类型是 jpeg 图片
// 创建 UnityWebRequest
UnityWebRequest request = UnityWebRequest.Post(url, form);
// 添加请求头部Authorization
foreach (var header in headers)
{
request.SetRequestHeader(header.Key, header.Value);
}
return request;
}
// 发送文件上传请求
private async Task SendFileUploadRequest(UnityWebRequest request)
{
// 发送请求并等待响应
var asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
await Task.Yield(); // 等待请求完成
}
// 处理请求结果
if (request.result == UnityWebRequest.Result.Success)
{
string responseText = request.downloadHandler.text;
Debug.Log("响应: " + responseText);
// 解析 JSON 响应
AvaterRe response = JsonUtility.FromJson<AvaterRe>(responseText);
//avaterdata.path = response.data; // 处理返回的数据
Debug.Log(response.code);
Debug.Log(response.data);
AvaImage.sprite= await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(response.data);
userInfomation14.data.headImg = response.data;
}
else
{
Debug.LogError("请求失败: " + request.error);
}
}
public async void SetEditPanel()
{
Dictionary<string, string> head13 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
ChangeDetailbody body= new ChangeDetailbody();
if(UserInputField.text == null)
{
body.nickName = userInfomation14.data.nickName;
}
else
{
body.nickName = UserInputField.text;
}
Debug.Log(UserInputField.text);
body.birthday = userInfomation14.data.birthday;
body.gender= userInfomation14.data.gender;
body.headImg = userInfomation14.data.headImg;
Debug.Log("入参"+ JsonUtility.ToJson(body));
string loginResponse = await web.SendRequest(web.URL + "/snail/user/update", "POST", JsonUtility.ToJson(body), head13);
ChangeReturn response = JsonUtility.FromJson<ChangeReturn>(loginResponse);
if (response.code==200)
{
Debug.Log("1.3完善用户信息"+ loginResponse);
perSonalCenterPanel.GetPlayerInfo();
Global.global.scene_Main_Jiekou.getPlayerInfo();
}
else
{
addEventPopUp(response.message);
}
Debug.Log(response);
transform.gameObject.SetActive(false);
}
}
public class AvaterRe : Response
{
public string data;
}
public class Avaterdata
{
public string path;
}
public class ChangeDetailbody
{
public string nickName = "";
public string headImg = "";
public int gender = 0;
public string birthday = "";
}
public class ChangeReturn
{
public int code;
public string message;
public bool data;
}