_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs

216 lines
6.6 KiB
C#
Raw Normal View History

2024-12-10 12:01:29 +08:00
using Newtonsoft.Json;
2024-11-25 22:18:57 +08:00
using System.Collections;
using System.Collections.Generic;
2024-12-10 12:01:29 +08:00
using TMPro;
2024-11-25 22:18:57 +08:00
using UnityEngine;
2024-12-10 12:01:29 +08:00
using UnityEngine.SceneManagement;
2024-11-25 22:18:57 +08:00
using UnityEngine.UI;
2024-12-13 15:27:51 +08:00
using System.IO;
using System.Threading.Tasks;
using UnityEngine.Networking;
2024-12-10 12:01:29 +08:00
public class EditPanel : Base
2024-11-25 22:18:57 +08:00
{
2024-12-10 12:01:29 +08:00
public UserInfomation14 userInfomation14;
public InputField UserInputField;
public Text username;
public PerSonalCenterPanel perSonalCenterPanel;
2024-12-13 15:27:51 +08:00
public Avaterdata avaterdata;
2024-12-13 09:59:41 +08:00
public Button avatar;
2024-12-13 15:27:51 +08:00
public Image AvaImage;
2024-11-25 22:18:57 +08:00
// Start is called before the first frame update
2024-12-10 12:01:29 +08:00
async void Start()
2024-11-25 22:18:57 +08:00
{
2024-12-10 12:01:29 +08:00
Dictionary<string, string> head14 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
// <20><EFBFBD><ECB2BD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14);
Debug.Log("1.4<EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ" + response14);
userInfomation14 = JsonConvert.DeserializeObject<UserInfomation14>(response14);
2024-12-13 15:27:51 +08:00
UserInputField.text = "" + userInfomation14.data.nickName;
2024-12-10 12:01:29 +08:00
username.text = "" + userInfomation14.data.nickName;
2024-12-13 09:59:41 +08:00
avatar.onClick.AddListener(selectavatar);
2024-12-13 15:27:51 +08:00
AvaImage.sprite = await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(userInfomation14.data.headImg);
2024-12-13 09:59:41 +08:00
}
void selectavatar()
{
PickImage(512);
2024-11-25 22:18:57 +08:00
}
2024-12-13 09:59:41 +08:00
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;
}
2024-12-13 15:27:51 +08:00
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
2024-11-25 22:18:57 +08:00
2024-12-13 15:27:51 +08:00
// <20><><EFBFBD><EFBFBD> AvaImage <20><> sprite
//AvaImage.sprite = sprite;
byte[] fileBytes = File.ReadAllBytes(path);
SetAvater(path);
2024-12-13 09:59:41 +08:00
}
});
Debug.Log("Permission result: " + permission);
}
2024-11-25 22:18:57 +08:00
// Update is called once per frame
2024-12-13 15:27:51 +08:00
// <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
public async void SetAvater(string filepath)
2024-11-25 22:18:57 +08:00
{
2024-12-13 15:27:51 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (!File.Exists(filepath))
{
Debug.LogError("<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>" + filepath);
return;
}
// <20><>ȡ<EFBFBD>ļ<EFBFBD>Ϊ<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
byte[] fileBytes = File.ReadAllBytes(filepath);
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
Dictionary<string, string> head13 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
Debug.Log("<22><><EFBFBD><EFBFBD>-----------------------------1.17");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UnityWebRequest request = CreateFileUploadRequest(filepath, fileBytes, head13);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󲢵ȴ<F3B2A2B5><C8B4><EFBFBD>Ӧ
await SendFileUploadRequest(request);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ϴ<EFBFBD><CFB4><EFBFBD> UnityWebRequest
private UnityWebRequest CreateFileUploadRequest(string filepath, byte[] fileBytes, Dictionary<string, string> headers)
{
string url = web.URL + "/snail/file/upload"; // <20>ļ<EFBFBD><C4BC>ϴ<EFBFBD><CFB4>Ľӿ<C4BD> URL
// <20><><EFBFBD><EFBFBD> WWWForm <20><><EFBFBD><EFBFBD>
WWWForm form = new WWWForm();
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"file" Ϊ<>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
string fileName = Path.GetFileName(filepath);
form.AddBinaryData("file", fileBytes, fileName, "image/jpeg"); // <20><><EFBFBD><EFBFBD> MIME <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> jpeg ͼƬ
// <20><><EFBFBD><EFBFBD> UnityWebRequest
UnityWebRequest request = UnityWebRequest.Post(url, form);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Authorization<6F><6E>
foreach (var header in headers)
{
request.SetRequestHeader(header.Key, header.Value);
}
return request;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
private async Task SendFileUploadRequest(UnityWebRequest request)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󲢵ȴ<F3B2A2B5><C8B4><EFBFBD>Ӧ
var asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
await Task.Yield(); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (request.result == UnityWebRequest.Result.Success)
{
string responseText = request.downloadHandler.text;
Debug.Log("<22><>Ӧ: " + responseText);
// <20><><EFBFBD><EFBFBD> JSON <20><>Ӧ
AvaterRe response = JsonUtility.FromJson<AvaterRe>(responseText);
//avaterdata.path = response.data; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
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("<22><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>: " + request.error);
}
2024-11-25 22:18:57 +08:00
}
2024-12-10 12:01:29 +08:00
public async void SetEditPanel()
2024-11-25 22:18:57 +08:00
{
2024-12-10 12:01:29 +08:00
Dictionary<string, string> head13 = new Dictionary<string, string>
{
{ "Authorization", Global.global.serverResponse.data.token }
};
ChangeDetailbody body= new ChangeDetailbody();
2024-12-13 15:27:51 +08:00
if(UserInputField.text == null)
{
body.nickName = userInfomation14.data.nickName;
}
else
{
body.nickName = UserInputField.text;
}
2024-12-10 12:01:29 +08:00
Debug.Log(UserInputField.text);
body.birthday = userInfomation14.data.birthday;
body.gender= userInfomation14.data.gender;
body.headImg = userInfomation14.data.headImg;
Debug.Log("<22><><EFBFBD><EFBFBD>"+ 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<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>Ϣ"+ loginResponse);
perSonalCenterPanel.GetPlayerInfo();
2024-12-10 12:16:31 +08:00
Global.global.scene_Main_Jiekou.getPlayerInfo();
2024-12-10 12:01:29 +08:00
}
else
{
addEventPopUp(response.message);
}
Debug.Log(response);
2024-11-25 22:18:57 +08:00
transform.gameObject.SetActive(false);
}
}
2024-12-13 15:27:51 +08:00
public class AvaterRe : Response
{
public string data;
}
public class Avaterdata
{
public string path;
}
public class ChangeDetailbody
2024-12-10 12:01:29 +08:00
{
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;
}