2026-05-18 08:42:33 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Kill.Managers;
|
|
|
|
|
using Kill.Network;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using Kill.UI.Pages;
|
|
|
|
|
using Kill.UI.Components;
|
|
|
|
|
namespace Kill.UI.Pages
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备重命名页面
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RenamePage : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("输入组件")]
|
|
|
|
|
public InputField nameInput;
|
|
|
|
|
|
|
|
|
|
[Header("按钮")]
|
|
|
|
|
public Button confirmBtn;
|
|
|
|
|
public Button cancelBtn;
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
private string currentName;
|
2026-05-18 08:42:33 +08:00
|
|
|
private Action<string> onConfirmCallback;
|
2026-06-08 08:55:10 +08:00
|
|
|
private int type = 0;//0设备重命名 1用户名重命名
|
2026-05-18 08:42:33 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化页面
|
|
|
|
|
/// </summary>
|
2026-06-08 08:55:10 +08:00
|
|
|
/// <param name="deviceName">当前名称</param>
|
|
|
|
|
/// <param name="onConfirm">确认回调,返回名称</param>
|
|
|
|
|
/// <param name="type">类型 0 重命名设备 1 用户名重命名</param>
|
|
|
|
|
public void Init(string name, Action<string> onConfirm, int type = 0)
|
2026-05-18 08:42:33 +08:00
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
currentName = name;
|
|
|
|
|
onConfirmCallback = onConfirm;
|
|
|
|
|
this.type = type;
|
2026-05-18 08:42:33 +08:00
|
|
|
|
|
|
|
|
// 初始化输入框
|
|
|
|
|
if (nameInput != null)
|
|
|
|
|
{
|
2026-06-08 08:55:10 +08:00
|
|
|
nameInput.text = name;
|
2026-05-18 08:42:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 绑定按钮事件
|
|
|
|
|
if (confirmBtn != null)
|
|
|
|
|
confirmBtn.onClick.AddListener(OnConfirmClick);
|
|
|
|
|
|
|
|
|
|
if (cancelBtn != null)
|
|
|
|
|
cancelBtn.onClick.AddListener(OnCancelClick);
|
|
|
|
|
|
|
|
|
|
// 注册返回事件
|
|
|
|
|
UIManager.Instance.RegisterBackAction(Back);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 点击确认按钮
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async void OnConfirmClick()
|
|
|
|
|
{
|
|
|
|
|
string newName = nameInput != null ? nameInput.text.Trim() : "";
|
|
|
|
|
|
|
|
|
|
// 如果为空或名称没有变化,直接返回
|
2026-06-08 08:55:10 +08:00
|
|
|
if (string.IsNullOrEmpty(newName) || newName == currentName)
|
2026-05-18 08:42:33 +08:00
|
|
|
{
|
|
|
|
|
Back();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示加载中
|
|
|
|
|
LoadingUI.Show();
|
2026-06-08 08:55:10 +08:00
|
|
|
if (type == 0)
|
|
|
|
|
{
|
|
|
|
|
// 调用服务器接口修改设备名称
|
|
|
|
|
UpdateDeviceNameAsync(newName);
|
|
|
|
|
}
|
|
|
|
|
else if (type == 1)
|
|
|
|
|
{
|
|
|
|
|
// 调用服务器接口修改用户名
|
|
|
|
|
UpdateUserNameAsync(newName);
|
|
|
|
|
}
|
2026-05-18 08:42:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 调用服务器接口更新设备名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async void UpdateDeviceNameAsync(string newName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 构建请求数据
|
|
|
|
|
var requestData = new DeviceNameRequest
|
|
|
|
|
{
|
|
|
|
|
device_name = newName,
|
|
|
|
|
user_id = DataManager.Instance.userInfo.id,
|
2026-06-08 08:55:10 +08:00
|
|
|
device_sn = DataManager.Instance.selectedDevice.ble_mac
|
2026-05-18 08:42:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 发送PUT请求
|
|
|
|
|
var response = await HttpRequestManager.Instance.Put<string>("/api/v1/device/name", requestData);
|
|
|
|
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
{
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 回调新名称
|
|
|
|
|
onConfirmCallback?.Invoke(newName);
|
|
|
|
|
Back();
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
onError: null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"更新设备名称失败: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 08:55:10 +08:00
|
|
|
private async void UpdateUserNameAsync(string newName)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 构建请求数据
|
|
|
|
|
var requestData = new UserNameRequest
|
|
|
|
|
{
|
|
|
|
|
username = newName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 发送PUT请求
|
|
|
|
|
var response = await HttpRequestManager.Instance.Put<string>("/api/v1/user/username", requestData);
|
|
|
|
|
|
|
|
|
|
ResponseCodeHandler.HandleResponse(response,
|
|
|
|
|
onSuccess: (data) =>
|
|
|
|
|
{
|
|
|
|
|
LoadingUI.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 回调新名称
|
|
|
|
|
onConfirmCallback?.Invoke(newName);
|
|
|
|
|
Back();
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
onError: null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"更新设备名称失败: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 08:42:33 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 点击取消按钮
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnCancelClick()
|
|
|
|
|
{
|
|
|
|
|
Back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 返回/关闭页面
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Back()
|
|
|
|
|
{
|
|
|
|
|
UIManager.Instance.ClearBackAction();
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
// 移除事件监听
|
|
|
|
|
if (confirmBtn != null)
|
|
|
|
|
confirmBtn.onClick.RemoveListener(OnConfirmClick);
|
|
|
|
|
|
|
|
|
|
if (cancelBtn != null)
|
|
|
|
|
cancelBtn.onClick.RemoveListener(OnCancelClick);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备名称请求数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class DeviceNameRequest
|
|
|
|
|
{
|
|
|
|
|
public string device_name;
|
|
|
|
|
public string user_id;
|
|
|
|
|
public string device_sn;
|
|
|
|
|
}
|
2026-06-08 08:55:10 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 更新用户名请求数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class UserNameRequest
|
|
|
|
|
{
|
|
|
|
|
public string username;
|
|
|
|
|
}
|
2026-05-18 08:42:33 +08:00
|
|
|
}
|