头像更新,完善用户信息

This commit is contained in:
杨号敬 2024-12-13 15:27:51 +08:00
parent 6e7020034b
commit e6b74ae6e3
5 changed files with 120 additions and 26 deletions

View File

@ -5,15 +5,19 @@ 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()
{
@ -26,9 +30,10 @@ public class EditPanel : Base
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()
@ -49,33 +54,100 @@ public class EditPanel : Base
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));
// Assign texture to a temporary quad and destroy it after 5 seconds
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
quad.transform.forward = Camera.main.transform.forward;
quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);
Material material = quad.GetComponent<Renderer>().material;
if (!material.shader.isSupported) // happens when Standard shader is not included in the build
material.shader = Shader.Find("Legacy Shaders/Diffuse");
material.mainTexture = texture;
Destroy(quad, 5f);
// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
Destroy(texture, 5f);
// 设置 AvaImage 的 sprite
//AvaImage.sprite = sprite;
byte[] fileBytes = File.ReadAllBytes(path);
SetAvater(path);
}
});
Debug.Log("Permission result: " + permission);
}
// Update is called once per frame
void Update()
// 设置头像的方法
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()
{
@ -84,7 +156,14 @@ public class EditPanel : Base
{ "Authorization", Global.global.serverResponse.data.token }
};
ChangeDetailbody body= new ChangeDetailbody();
body.nickName = UserInputField.text;
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;
@ -107,7 +186,19 @@ public class EditPanel : Base
transform.gameObject.SetActive(false);
}
}
public class ChangeDetailbody//µÇ¼ºÍ×¢²áÓÃ
public class AvaterRe : Response
{
public string data;
}
public class Avaterdata
{
public string path;
}
public class ChangeDetailbody
{
public string nickName = "";
public string headImg = "";

View File

@ -17,6 +17,7 @@ public class PerSonalCenterPanel : MonoBehaviour
public Text id;
public TextMeshProUGUI bindtime;
public Image AvaterImage;
//UI逻辑相关组件
public Transform trans;
public Button editBtn;//编辑按钮
@ -150,7 +151,7 @@ public class PerSonalCenterPanel : MonoBehaviour
nickName.text = userInfomation14.data.nickName;
id.text = "ID"+ userInfomation14.data.cuteNo;
DateTime bindDateTime = DateTime.Parse(userInfomation14.data.bindTime);
AvaterImage.sprite= await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(userInfomation14.data.headImg);
// 获取当前时间
DateTime currentDateTime = DateTime.Now;

View File

@ -52,7 +52,7 @@ public class Scene_main_jiekou :Base
/*Debug.Log(_realPlayerInfo.data.nickName);
Debug.Log(_realPlayerInfo.data.voluteCoin);
Debug.Log(_realPlayerInfo.data.beansCoin);*/
_mainBTN.UpDatePlayer(_realPlayerInfo.data.nickName, _realPlayerInfo.data.voluteCoin.ToString("f1") , _realPlayerInfo.data.beansCoin.ToString("f1"));
_mainBTN.UpDatePlayer(_realPlayerInfo.data.headImg, _realPlayerInfo.data.nickName, _realPlayerInfo.data.voluteCoin.ToString("f1") , _realPlayerInfo.data.beansCoin.ToString("f1"));
}
async Task PlayerInfoAsync()

View File

@ -2151,6 +2151,7 @@ MonoBehaviour:
nickName: {fileID: 6137192365456531787}
id: {fileID: 7440098880401058131}
bindtime: {fileID: 8608830216116307115}
AvaterImage: {fileID: 8197978441176617509}
trans: {fileID: 6661102261700243289}
editBtn: {fileID: 3317031098697572181}
myorderBtn: {fileID: 2761721410439307699}

View File

@ -469,6 +469,7 @@ MonoBehaviour:
username: {fileID: 8468291165135306074}
perSonalCenterPanel: {fileID: 0}
avatar: {fileID: 2339787961763677450}
AvaImage: {fileID: 8996637367871962010}
--- !u!1 &6960989442929760420
GameObject:
m_ObjectHideFlags: 0