NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Localization/LinkText.cs

230 lines
8.4 KiB
C#
Raw Normal View History

2024-08-13 15:19:31 +08:00
using Cysharp.Threading.Tasks;
using Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
2024-08-13 15:19:31 +08:00
using UnityEngine.Networking;
[RequireComponent(typeof(TMP_Text))]
public class LinkText : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
TMP_Text pTextMeshPro = GetComponent<TMP_Text>();
int linkIndex =
TMP_TextUtilities.FindIntersectingLink(pTextMeshPro, eventData.position, eventData.pressEventCamera);
// If you are not in a Canvas using Screen Overlay, put your camera instead of null
if (linkIndex != -1)
{
TMP_LinkInfo linkInfo = pTextMeshPro.textInfo.linkInfo[linkIndex];
DebugUtil.Log("OnPointerClick called.");
DebugUtil.Log("Link ID: \"" + linkInfo.GetLinkID() + "\" Link Text: \"" + linkInfo.GetLinkText());
2024-08-13 15:19:31 +08:00
//Application.OpenURL(linkInfo.GetLinkID());
Action(linkInfo.GetLinkID());
}
}
void Action(string linkID)
{
DebugUtil.Log("Action called.");
DebugUtil.Log("Link ID: " + linkID);
if(linkID == "api_01")
{
var headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
{
{"timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
});
UnityWebRequest www = new UnityWebRequest("http://192.168.3.90:19999/api/v1/agreements", UnityWebRequest.kHttpVerbPOST)
{
downloadHandler = new DownloadHandlerBuffer(),
};
www.SetRequestHeader("signature", headData["signature"]);
www.SetRequestHeader("timestamp", headData["timestamp"]);
try
{
//await www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
DebugUtil.LogError(www.downloadHandler.text);
}
else
{
DebugUtil.Log("http消息请求成功\n返回内容" + www.downloadHandler.text);
//WebRequestResult result = JsonConvert.DeserializeObject<WebRequestResult>(www.downloadHandler.text);
//if (result.code == 0)
//{
// //send success msg
// DebugUtil.Log("短信验证码请求成功");
//}
//else
//{
// //send fail msg
// EventManager.Instance.Send(EventManager.EventName.PhoneCodeRequsetFail, "请求失败!" + result.message);
// DebugUtil.LogError("请求失败!" + result.message);
//}
//callback?.Invoke(result.code, result.message);
}
}
catch (UnityWebRequestException e)
{
DebugUtil.LogError(e);
EventManager.Instance.Send(EventManager.EventName.PhoneCodeRequsetFail, "请求失败!");
//callback?.Invoke(-1, e.Message);
}
}
else if(linkID == "api_02")
{
var headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
{
{"timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
});
//post data
//var jsonData = JsonUtility.ToJson(sendData);
//byte[] bytes = Encoding.UTF8.GetBytes(jsonData);
UnityWebRequest www = new UnityWebRequest("http://192.168.3.90:19999/api/v1/agreements", UnityWebRequest.kHttpVerbPOST)
{
//uploadHandler = new UploadHandlerRaw(bytes),
downloadHandler = new DownloadHandlerBuffer(),
};
//www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");//为了保证客户端发送的json格式在后端能解析
www.SetRequestHeader("signature", headData["signature"]);
www.SetRequestHeader("timestamp", headData["timestamp"]);
try
{
//await www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
DebugUtil.LogError(www.downloadHandler);
//DebugUtil.LogError(www.downloadHandler.text);
}
else
{
DebugUtil.Log("http消息请求成功\n返回内容" + www.downloadHandler.text);
//WebRequestResult result = JsonConvert.DeserializeObject<WebRequestResult>(www.downloadHandler.text);
//if (result.code == 0)
//{
// //send success msg
// DebugUtil.Log("短信验证码请求成功");
//}
//else
//{
// //send fail msg
// EventManager.Instance.Send(EventManager.EventName.PhoneCodeRequsetFail, "请求失败!" + result.message);
// DebugUtil.LogError("请求失败!" + result.message);
//}
//callback?.Invoke(result.code, result.message);
}
}
catch (UnityWebRequestException e)
{
DebugUtil.LogError(e);
EventManager.Instance.Send(EventManager.EventName.PhoneCodeRequsetFail, "请求失败!");
//callback?.Invoke(-1, e.Message);
}
}
else
{
DebugUtil.Log("Unknown link clicked.");
}
}
}
2024-08-13 15:19:31 +08:00
/// <summary>
/// 头部加密
/// </summary>
public class PostHeader
{
/// <summary>
/// 加密拼接密钥
/// </summary>
static string key = "OfqCL-_jJa9Sm1Nl-TPXvm3drI9td1RPoWn3eLjTe18=";
/// <summary>
/// 头部加密信息
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public static Dictionary<string, string> PostHeaderInfo(Dictionary<string, string> param)
{
DateTimeOffset now = DateTimeOffset.UtcNow;
string timeStamp = now.ToUnixTimeSeconds().ToString();
var encodeData = new EncodeData(param);
string encodeStr = encodeData.GetEncodeData();
var md5 = EncodeData.GetMd5Hash(encodeStr);
string linkStr = md5 + key;
string sign = EncodeData.GetSha256Hash(linkStr);
Dictionary<string, string> header = new();
header["signature"] = sign;
header["timestamp"] = timeStamp;
return header;
}
public class EncodeData
{
Dictionary<string, string> data = new Dictionary<string, string>();
public EncodeData(Dictionary<string, string> data)
{
this.data = data;
}
public string GetEncodeData()
{
List<string> key = data.Keys.Where(k => k != "signature").ToList();
key.Sort();
List<string> keyValuePairs = key.Select(k => k + "=" + data[k]).ToList();
string concatenate = string.Join("&", keyValuePairs);
//DebugUtil.Log("排序结果:" + concatenate);
return concatenate;
}
public static string GetMd5Hash(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
public static string GetSha256Hash(string input)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = sha256.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
}
}