TalkingDataTransaction.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using UnityEngine;
  2. #if TD_FINANCE
  3. public class TalkingDataTransaction
  4. {
  5. #if UNITY_ANDROID
  6. public AndroidJavaObject javaObj;
  7. #endif
  8. #if UNITY_IPHONE
  9. private string transactionId;
  10. private string category;
  11. private int amount;
  12. private string personA;
  13. private string personB;
  14. private long startDate;
  15. private long endDate;
  16. private string currencyType;
  17. private string content;
  18. #endif
  19. public static TalkingDataTransaction CreateTransaction()
  20. {
  21. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  22. {
  23. TalkingDataTransaction transaction = new TalkingDataTransaction();
  24. #if UNITY_ANDROID
  25. AndroidJavaClass javaClass = new AndroidJavaClass("com.tendcloud.tenddata.TalkingDataTransaction");
  26. transaction.javaObj = javaClass.CallStatic<AndroidJavaObject>("createTransaction");
  27. #endif
  28. return transaction;
  29. }
  30. return null;
  31. }
  32. // 交易ID
  33. public TalkingDataTransaction SetTransactionId(string transactionId)
  34. {
  35. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  36. {
  37. #if UNITY_ANDROID
  38. if (javaObj != null)
  39. {
  40. javaObj.Call<AndroidJavaObject>("setTransactionId", transactionId);
  41. }
  42. #endif
  43. #if UNITY_IPHONE
  44. this.transactionId = transactionId;
  45. #endif
  46. }
  47. return this;
  48. }
  49. // 交易分类
  50. public TalkingDataTransaction SetCategory(string category)
  51. {
  52. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  53. {
  54. #if UNITY_ANDROID
  55. if (javaObj != null)
  56. {
  57. javaObj.Call<AndroidJavaObject>("setCategory", category);
  58. }
  59. #endif
  60. #if UNITY_IPHONE
  61. this.category = category;
  62. #endif
  63. }
  64. return this;
  65. }
  66. // 交易额
  67. public TalkingDataTransaction SetAmount(int amount)
  68. {
  69. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  70. {
  71. #if UNITY_ANDROID
  72. if (javaObj != null)
  73. {
  74. javaObj.Call<AndroidJavaObject>("setAmount", amount);
  75. }
  76. #endif
  77. #if UNITY_IPHONE
  78. this.amount = amount;
  79. #endif
  80. }
  81. return this;
  82. }
  83. // 交易甲方
  84. public TalkingDataTransaction SetPersonA(string personA)
  85. {
  86. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  87. {
  88. #if UNITY_ANDROID
  89. if (javaObj != null)
  90. {
  91. javaObj.Call<AndroidJavaObject>("setPersonA", personA);
  92. }
  93. #endif
  94. #if UNITY_IPHONE
  95. this.personA = personA;
  96. #endif
  97. }
  98. return this;
  99. }
  100. // 交易乙方
  101. public TalkingDataTransaction SetPersonB(string personB)
  102. {
  103. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  104. {
  105. #if UNITY_ANDROID
  106. if (javaObj != null)
  107. {
  108. javaObj.Call<AndroidJavaObject>("setPersonB", personB);
  109. }
  110. #endif
  111. #if UNITY_IPHONE
  112. this.personB = personB;
  113. #endif
  114. }
  115. return this;
  116. }
  117. // 交易起始Unix时间戳。单位:毫秒
  118. public TalkingDataTransaction SetStartDate(long startDate)
  119. {
  120. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  121. {
  122. #if UNITY_ANDROID
  123. if (javaObj != null)
  124. {
  125. javaObj.Call<AndroidJavaObject>("setStartDate", startDate);
  126. }
  127. #endif
  128. #if UNITY_IPHONE
  129. this.startDate = startDate;
  130. #endif
  131. }
  132. return this;
  133. }
  134. // 交易终止Unix时间戳。单位:毫秒
  135. public TalkingDataTransaction SetEndDate(long endDate)
  136. {
  137. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  138. {
  139. #if UNITY_ANDROID
  140. if (javaObj != null)
  141. {
  142. javaObj.Call<AndroidJavaObject>("setEndDate", endDate);
  143. }
  144. #endif
  145. #if UNITY_IPHONE
  146. this.endDate = endDate;
  147. #endif
  148. }
  149. return this;
  150. }
  151. // 货币类型
  152. public TalkingDataTransaction SetCurrencyType(string currencyType)
  153. {
  154. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  155. {
  156. #if UNITY_ANDROID
  157. if (javaObj != null)
  158. {
  159. javaObj.Call<AndroidJavaObject>("setCurrencyType", currencyType);
  160. }
  161. #endif
  162. #if UNITY_IPHONE
  163. this.currencyType = currencyType;
  164. #endif
  165. }
  166. return this;
  167. }
  168. // 交易详情
  169. public TalkingDataTransaction SetContent(string content)
  170. {
  171. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  172. {
  173. #if UNITY_ANDROID
  174. if (javaObj != null)
  175. {
  176. javaObj.Call<AndroidJavaObject>("setContent", content);
  177. }
  178. #endif
  179. #if UNITY_IPHONE
  180. this.content = content;
  181. #endif
  182. }
  183. return this;
  184. }
  185. #if UNITY_IPHONE
  186. public override string ToString()
  187. {
  188. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  189. {
  190. string transactionStr = "{\"transactionId\":\"" + transactionId + "\""
  191. + ",\"category\":\"" + category + "\""
  192. + ",\"amount\":" + amount
  193. + ",\"personA\":\"" + personA + "\""
  194. + ",\"personB\":\"" + personB + "\""
  195. + ",\"startDate\":" + startDate
  196. + ",\"endDate\":" + endDate
  197. + ",\"currencyType\":\"" + currencyType + "\""
  198. + ",\"content\":\"" + content + "\""
  199. + "}";
  200. return transactionStr;
  201. }
  202. return null;
  203. }
  204. #endif
  205. }
  206. #endif