184 lines
5.8 KiB
C#
184 lines
5.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using Cysharp.Threading.Tasks;
|
||
using Gameplay.Net;
|
||
using Newtonsoft.Json;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
|
||
public class NoticeDataForDisplay
|
||
{
|
||
public int ID;
|
||
public string Title;
|
||
public int Type;
|
||
public Sprite Bannersprite;
|
||
public Sprite ContentSprite;
|
||
public string Content;
|
||
}
|
||
|
||
public class NoticeManager : Singlenton<NoticeManager>
|
||
{
|
||
private const string NoticePost = "notices";
|
||
|
||
/// <summary>
|
||
/// 公告请求结果
|
||
/// </summary>
|
||
private class NoticeGetResult
|
||
{
|
||
public int code;
|
||
public string message;
|
||
public List<NoticeData> data;
|
||
}
|
||
|
||
private class NoticeData
|
||
{
|
||
public int id;
|
||
public string title;
|
||
public int type;
|
||
public string url;
|
||
public string cover;
|
||
public string content;
|
||
public int application_id;
|
||
public string created_at;
|
||
public string update_at;
|
||
}
|
||
|
||
|
||
private List<NoticeData> curNoticeData;
|
||
|
||
/// <summary>
|
||
/// 公告数据 UI
|
||
/// </summary>
|
||
private Dictionary<int, NoticeDataForDisplay> noticeDataForDisplays;
|
||
|
||
public Dictionary<int, NoticeDataForDisplay> NoticeDataForDisplays => noticeDataForDisplays;
|
||
|
||
public async void GetNotice()
|
||
{
|
||
var requestUrl = AppConfig.Instance.LoginURL + NoticePost;
|
||
DebugUtil.Log("拉起公告请求地址: " + requestUrl);
|
||
try
|
||
{
|
||
var headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
|
||
{
|
||
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
|
||
});
|
||
|
||
var request = new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbGET)
|
||
{
|
||
downloadHandler = new DownloadHandlerBuffer(),
|
||
};
|
||
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); //为了保证客户端发送的json格式在后端能解析
|
||
request.SetRequestHeader("signature", headData["signature"]);
|
||
request.SetRequestHeader("timestamp", headData["timestamp"]);
|
||
request.SetRequestHeader("accessKey", headData["accessKey"]);
|
||
|
||
// 异步等待请求完成
|
||
await request.SendWebRequest();
|
||
|
||
if (request.result != UnityWebRequest.Result.Success)
|
||
{
|
||
DebugUtil.LogError(request.downloadHandler.text);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.Log("http消息请求成功!返回内容:\n" + request.downloadHandler.text);
|
||
var result = JsonConvert.DeserializeObject<NoticeGetResult>(request.downloadHandler.text);
|
||
if (result.code == 0)
|
||
{
|
||
curNoticeData = result.data;
|
||
ConvertNotice();
|
||
DebugUtil.Log("获取公告成功");
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("获取公告失败");
|
||
}
|
||
}
|
||
}
|
||
catch (UnityWebRequestException e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
var pattern = @"\{.*\}";
|
||
var match = Regex.Match(e.Message, pattern);
|
||
if (match.Success)
|
||
{
|
||
var json = match.Value;
|
||
NoticeGetResult res = JsonConvert.DeserializeObject<NoticeGetResult>(json);
|
||
DebugUtil.LogError("请求失败, 服务器有错误响应: " + res.message);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("请求失败!");
|
||
}
|
||
}
|
||
}
|
||
|
||
private async void ConvertNotice()
|
||
{
|
||
noticeDataForDisplays = new Dictionary<int, NoticeDataForDisplay>();
|
||
var index = 0;
|
||
foreach (var data in curNoticeData)
|
||
{
|
||
DebugUtil.Log("公告{0},下载链接:{1}", data.id, data.cover);
|
||
DebugUtil.Log("公告内容:{0}", data.content);
|
||
var sprite = await DownLoadImg(data.cover);
|
||
var noticeDataForDisplay = new NoticeDataForDisplay
|
||
{
|
||
ID = data.id,
|
||
Content = data.content,
|
||
Bannersprite = sprite,
|
||
ContentSprite = sprite,
|
||
Title = data.title,
|
||
Type = data.type,
|
||
};
|
||
noticeDataForDisplays[index] = noticeDataForDisplay;
|
||
index++;
|
||
}
|
||
}
|
||
|
||
private async UniTask<Sprite> DownLoadImg(string url)
|
||
{
|
||
Sprite sprite = null;
|
||
try
|
||
{
|
||
using var request = UnityWebRequestTexture.GetTexture(url);
|
||
var operation = request.SendWebRequest();
|
||
|
||
await operation.ToUniTask();
|
||
if (request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
var texture = DownloadHandlerTexture.GetContent(request);
|
||
if (texture != null)
|
||
{
|
||
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
|
||
new Vector2(0.5f, 0.5f));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"公告图片下载失败: {request.error}");
|
||
}
|
||
|
||
return sprite;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("NoticeManager.DownLoadImg error: {0}", e);
|
||
return sprite;
|
||
}
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
foreach (var noticeData in noticeDataForDisplays)
|
||
{
|
||
noticeData.Value.ContentSprite = null;
|
||
noticeData.Value.Bannersprite = null;
|
||
}
|
||
|
||
noticeDataForDisplays = null;
|
||
}
|
||
} |