ImportRequest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Design;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Windows.Data.Json;
  11. namespace KdanCommon.Mixpanel.Data
  12. {
  13. public class EventData
  14. {
  15. [JsonProperty("event")]
  16. public string Event { get; }
  17. [JsonProperty("properties")]
  18. public Dictionary<string, object> Properties
  19. {
  20. get
  21. {
  22. return GetDictionary();
  23. }
  24. }
  25. private string _distinctId { get; }
  26. private Dictionary<string, object> _customProperties { get; }
  27. public EventData(string eventName, string distinctId, Dictionary<string, object> customProperties = null)
  28. {
  29. Event = eventName;
  30. _distinctId = distinctId;
  31. _customProperties = customProperties;
  32. }
  33. private Dictionary<string, object> GetDictionary()
  34. {
  35. DateTime gtm = new DateTime(1970, 1, 1);
  36. long timeStamp = Convert.ToInt64(((TimeSpan)DateTime.UtcNow.Subtract(gtm)).TotalSeconds);
  37. var properties = new Dictionary<string, object>
  38. {
  39. { "time", timeStamp },
  40. { "distinct_id", _distinctId },
  41. { "$insert_id", GetInsertId(_distinctId, timeStamp, _customProperties) },
  42. { "$city", "台南"}
  43. };
  44. if (_customProperties != null)
  45. {
  46. foreach (var kvp in _customProperties)
  47. properties.Add(kvp.Key, kvp.Value);
  48. }
  49. return properties;
  50. }
  51. private string GetInsertId(string distinctId, long timestamp, Dictionary<string, object> properties)
  52. {
  53. return Guid.NewGuid().ToString("N");
  54. /*
  55. try
  56. {
  57. var str = Guid.NewGuid();
  58. var str2 = Guid.NewGuid().ToString("N");
  59. string semanticallyUniqueStr = $"{distinctId}{timestamp}";
  60. if (properties != null)
  61. {
  62. foreach (var value in properties.Values)
  63. semanticallyUniqueStr += value;
  64. }
  65. byte[] semanticallyUniqueBytes = Encoding.UTF8.GetBytes(semanticallyUniqueStr);
  66. using (var sha256 = SHA256.Create())
  67. {
  68. byte[] hashBytes = sha256.ComputeHash(semanticallyUniqueBytes);
  69. string hashStr = Convert.ToBase64String(hashBytes);
  70. return hashStr.Substring(0, 36);
  71. }
  72. }
  73. catch
  74. {
  75. return "";
  76. //return Guid.NewGuid().ToString("N").Substring(0, 36);
  77. }
  78. */
  79. }
  80. }
  81. public class ProfileData
  82. {
  83. [JsonProperty("token")]
  84. public string Token { get; }
  85. [JsonProperty("$distinct_id")]
  86. public string DistinctId { get; }
  87. [JsonProperty("$set")]
  88. public Dictionary<string, object> Set
  89. {
  90. get
  91. {
  92. return GetDictionary();
  93. }
  94. }
  95. private string _name { get; }
  96. private string _mail { get; }
  97. private Dictionary<string, object> _customProperties { get; }
  98. public ProfileData(string projectToken, string distinctId, string name, string mail, Dictionary<string, object> customProperties = null)
  99. {
  100. Token = projectToken;
  101. DistinctId = distinctId;
  102. _name = name;
  103. _mail = mail;
  104. _customProperties = customProperties;
  105. }
  106. private Dictionary<string, object> GetDictionary()
  107. {
  108. var properties = new Dictionary<string, object>
  109. {
  110. { "$name", _name },
  111. { "$email", _mail }
  112. };
  113. if (_customProperties != null)
  114. {
  115. foreach (var kvp in _customProperties)
  116. properties.Add(kvp.Key, kvp.Value);
  117. }
  118. return properties;
  119. }
  120. }
  121. public class AliasData
  122. {
  123. [JsonProperty("event")]
  124. public string Event { get; set; }
  125. [JsonProperty("properties")]
  126. public Properties Properties { get; set; }
  127. }
  128. public class Properties
  129. {
  130. [JsonProperty("distinct_id")]
  131. public string DistinctId { get; set; }
  132. [JsonProperty("alias")]
  133. public string Alias { get; set; }
  134. [JsonProperty("token")]
  135. public string Token { get; set; }
  136. }
  137. }