killapp/Assets/Scripts/Managers/DataManager.cs

228 lines
8.0 KiB
C#
Raw Normal View History

2026-04-24 16:57:44 +08:00
using System;
2026-04-16 14:57:19 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kill.Network;
2026-04-24 16:57:44 +08:00
using Kill.UI.Components;
2026-04-16 14:57:19 +08:00
using UnityEngine;
namespace Kill.Managers
{
public class DataManager : MonoBehaviour
{
public string selectedDeviceMac="";
2026-04-16 14:57:19 +08:00
public static DataManager Instance { get; private set; }
public DeviceInfo selectedDevice;
2026-06-10 15:04:14 +08:00
public List<DeviceInfo> OwnedDevices = new List<DeviceInfo>();
public List<DeviceInfo> SharedDevices = new List<DeviceInfo>();
2026-05-18 08:42:33 +08:00
public bool isOwner;
2026-06-12 09:42:44 +08:00
public bool hasWifi;
2026-04-16 14:57:19 +08:00
private void Awake()
{
Instance = this;
}
public async Task Init()
{
InitUser();
GetSelectedDeviceMac();
2026-04-16 14:57:19 +08:00
}
public string token = "";
public UserInfo userInfo = new UserInfo();
public void SetToken(string token,UserInfo userInfo)
{
this.token = token;
this.userInfo = userInfo;
string userData =JsonUtility.ToJson(userInfo);
2026-04-20 08:31:41 +08:00
Debug.Log(userData);
2026-04-16 14:57:19 +08:00
PlayerPrefs.SetString("token", token);
PlayerPrefs.SetString("userData", userData);
2026-04-24 16:57:44 +08:00
NetworkCtrl.Instance.RemoveGlobalHeader("token");
NetworkCtrl.Instance.AddGlobalHeader("token", token);
2026-04-16 14:57:19 +08:00
}
public void SavaSelectedDeviceMac(string mac)
{
selectedDeviceMac=mac;
PlayerPrefs.SetString("selectedDeviceMac", selectedDeviceMac);
}
public void GetSelectedDeviceMac()
{
selectedDeviceMac=PlayerPrefs.GetString("selectedDeviceMac", "");
}
2026-04-16 14:57:19 +08:00
public void InitUser()
{
token = PlayerPrefs.GetString("token", "");
string userData = PlayerPrefs.GetString("userData", "");
userInfo = JsonUtility.FromJson<UserInfo>(userData);
2026-04-24 16:57:44 +08:00
NetworkCtrl.Instance.RemoveGlobalHeader("token");
NetworkCtrl.Instance.AddGlobalHeader("token",token);
Debug.Log(token);
2026-04-16 14:57:19 +08:00
}
public void ClearInfo()
{
token = "";
userInfo = new UserInfo();
PlayerPrefs.DeleteKey("token");
PlayerPrefs.DeleteKey("userData");
}
2026-04-28 16:35:51 +08:00
public async Task<bool> TokenLogin()
2026-04-24 16:57:44 +08:00
{
LoadingUI.Show();
try
{
// 获取用户详情
var response = await NetworkCtrl.Instance.Post<LoginResponse>("/api/v1/auth/token-login");
LoadingUI.Hide();
2026-04-28 16:35:51 +08:00
if(response.Data.code==200)
{
SetToken(response.Data.data.token,response.Data.data.user);
return true;
}
else
{
ClearInfo();
return false;
}
2026-04-24 16:57:44 +08:00
}
catch (Exception ex)
{
LoadingUI.Hide();
Debug.LogError($"TokenLogin 异常: {ex.Message}");
2026-04-28 16:35:51 +08:00
return false;
2026-04-24 16:57:44 +08:00
}
}
2026-06-12 09:42:44 +08:00
public DeviceConfig deviceConfig;
#region
/// <summary>
/// 获取设备配置
/// </summary>
public async Task GetDeviceConfig(string deviceSn)
{
var response = await NetworkCtrl.Instance.Get<DeviceConfigResponse>(
"/api/v1/device/config",
new Dictionary<string, string> { { "device_sn", deviceSn } }
);
DeviceConfig result = null;
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
result = data.data;
deviceConfig=result;
},
onError: (code, msg) =>
{
Debug.LogError($"[DataManager] 获取设备配置失败: {msg}");
}
);
}
/// <summary>
/// 修改设备配置(空值字段不会被修改)
/// </summary>
public async Task<bool> UpdateDeviceConfig(string deviceSn, DeviceConfig config)
{
config.device_sn = deviceSn;
// 构建JSON跳过null值字段
string json = BuildDeviceConfigJson(config);
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/device/config", json);
bool success = false;
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
success = true;
},
onError: (code, msg) =>
{
Debug.LogError($"[DataManager] 修改设备配置失败: {msg}");
}
);
return success;
}
/// <summary>
/// 构建设备配置JSON只包含非null字段
/// </summary>
private string BuildDeviceConfigJson(DeviceConfig config)
{
var sb = new System.Text.StringBuilder();
sb.Append("{");
AppendField(sb, "device_sn", config.device_sn);
AppendField(sb, "language", config.language);
AppendField(sb, "is_locked", config.is_locked);
AppendField(sb, "fov_angle", config.fov_angle);
AppendField(sb, "detection_distance", config.detection_distance);
AppendField(sb, "aim_distance", config.aim_distance);
AppendField(sb, "work_mode", config.work_mode);
AppendField(sb, "fill_light_enable", config.fill_light_enable);
AppendField(sb, "fill_light_type", config.fill_light_type);
AppendField(sb, "fill_light_intensity", config.fill_light_intensity);
AppendField(sb, "laser_visible_enable", config.laser_visible_enable);
AppendField(sb, "visual_detect_enable", config.visual_detect_enable);
AppendField(sb, "visual_sensitivity", config.visual_sensitivity);
AppendField(sb, "radar_enable", config.radar_enable);
AppendField(sb, "radar_sensitivity", config.radar_sensitivity);
AppendField(sb, "radar_safe_distance", config.radar_safe_distance);
AppendField(sb, "rgb_enable", config.rgb_enable);
AppendField(sb, "rgb_red", config.rgb_red);
AppendField(sb, "rgb_green", config.rgb_green);
AppendField(sb, "rgb_blue", config.rgb_blue);
AppendField(sb, "rgb_effect", config.rgb_effect);
AppendField(sb, "lcd_auto_brightness", config.lcd_auto_brightness);
AppendField(sb, "lcd_brightness", config.lcd_brightness);
AppendField(sb, "lcd_sleep_enable", config.lcd_sleep_enable);
AppendField(sb, "lcd_sleep_time", config.lcd_sleep_time);
AppendField(sb, "wifi_enable", config.wifi_enable);
AppendField(sb, "wifi_ssid", config.wifi_ssid);
AppendField(sb, "wifi_password", config.wifi_password);
AppendField(sb, "video_record_enable", config.video_record_enable);
AppendField(sb, "record_duration", config.record_duration);
AppendField(sb, "sound_enable", config.sound_enable);
AppendField(sb, "sound_type", config.sound_type);
AppendField(sb, "volume", config.volume);
// 移除末尾逗号
if (sb[sb.Length - 1] == ',')
sb.Length--;
sb.Append("}");
return sb.ToString();
}
private void AppendField(System.Text.StringBuilder sb, string key, object value)
{
if (value == null) return;
sb.Append("\"");
sb.Append(key);
sb.Append("\":");
if (value is string s)
{
sb.Append("\"");
sb.Append(s);
sb.Append("\"");
}
else if (value is bool b)
{
sb.Append(b ? "true" : "false");
}
else
{
sb.Append(value.ToString());
}
sb.Append(",");
}
#endregion
2026-04-16 14:57:19 +08:00
}
}