91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using Kill.Managers;
|
||
|
|
using Kill.Network;
|
||
|
|
using Kill.UI.Components;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
namespace Kill.UI.Pages
|
||
|
|
{
|
||
|
|
public class ShareDevicePage : MonoBehaviour
|
||
|
|
{
|
||
|
|
public InputField userIdInput;
|
||
|
|
public Button shareButton;
|
||
|
|
public ScanQRcode scanQRcode;
|
||
|
|
public Button backButton;
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
UIManager.Instance.RegisterBackAction(Back);
|
||
|
|
backButton.onClick.RemoveAllListeners();
|
||
|
|
backButton.onClick.AddListener(Back);
|
||
|
|
userIdInput.text = "";
|
||
|
|
shareButton.onClick.RemoveAllListeners();
|
||
|
|
shareButton.onClick.AddListener(async () => await Share());
|
||
|
|
scanQRcode.OnQRCodeScanned += ScanOver;
|
||
|
|
}
|
||
|
|
public void BeginScan()
|
||
|
|
{
|
||
|
|
scanQRcode.scanType = ScanQRcode.ScanType.Other;
|
||
|
|
|
||
|
|
scanQRcode.gameObject.SetActive(true);
|
||
|
|
|
||
|
|
}
|
||
|
|
public void ScanOver(string scannedData)
|
||
|
|
{
|
||
|
|
userIdInput.text = scannedData;
|
||
|
|
scanQRcode.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
public void Back()
|
||
|
|
{
|
||
|
|
|
||
|
|
UIManager.Instance.RegisterBackAction(GetComponentInParent<DeviceInfoPage>().Back);
|
||
|
|
Destroy(gameObject);
|
||
|
|
}
|
||
|
|
public async Task Share()
|
||
|
|
{
|
||
|
|
string targetUserId = userIdInput.text.Trim();
|
||
|
|
if (string.IsNullOrEmpty(targetUserId))
|
||
|
|
{
|
||
|
|
ToastUI.Show("100204");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// 调用后端接口共享设备
|
||
|
|
LoadingUI.Show();
|
||
|
|
|
||
|
|
var requestData = new ShareDeviceRequest
|
||
|
|
{
|
||
|
|
device_sn = DataManager.Instance.selectedDevice.ble_mac,
|
||
|
|
target_user_id = targetUserId,
|
||
|
|
owner_id = DataManager.Instance.userInfo.id
|
||
|
|
};
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/device/share", requestData);
|
||
|
|
|
||
|
|
LoadingUI.Hide();
|
||
|
|
|
||
|
|
ResponseCodeHandler.HandleResponse(response,
|
||
|
|
onSuccess: (data) =>
|
||
|
|
{
|
||
|
|
Debug.Log("设备共享成功");
|
||
|
|
ToastUI.Show("100205");
|
||
|
|
Back();
|
||
|
|
},
|
||
|
|
onError: (code, msg) =>
|
||
|
|
{
|
||
|
|
Debug.LogError($"设备共享失败: {code} - {msg}");
|
||
|
|
ToastUI.Show("100282");
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
LoadingUI.Hide();
|
||
|
|
Debug.LogError("设备共享失败: " + ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|