powerfun-unity/Assets/Scripts/Apis/PfHttpClientHandler.cs

51 lines
1.6 KiB
C#
Raw Normal View History

2021-11-02 17:55:49 +08:00
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Apis
{
public class PfHttpClientHandler: HttpClientHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
//{"result":false,"errMsg":"已在其他设备登录","code":401}
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (request.RequestUri.ToString().Contains(App.Host) && result.Contains(@"{""result"":false,"))
{
var json = ParseJson<JsonResult<object>>(result);
if (json.code == 401)
{
App.is401 = json.errMsg;
UIManager.Instance.StartCoroutine(LoadLogin(json.errMsg));
}
}
else
{
App.is401 = null;
}
return response;
}
private T ParseJson<T>(string result)
{
return JsonConvert.DeserializeObject<T>(result, new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Local
});
}
IEnumerator LoadLogin(string msg)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
yield return null;
}
}
}