AuthenticationManager.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.Linq;
  6. using System;
  7. using LitJson;
  8. public class AuthenticationManager : SingletonMono<AuthenticationManager>
  9. {
  10. //存储包名和ChannelId
  11. private const string CHANNEL_SAVE_KEY = "CHANNEL_SAVE_KEY";
  12. //存储鉴权数据
  13. private const string AUTHENTICATION_SAVE_KEY = "AUTHENTICATION_SAVE_KEY";
  14. //存储上报数据
  15. private const string REPORT_SAVE_KEY = "REPORT_SAVE_KEY";
  16. //请求地址
  17. private const UrlName URL_NAME = UrlName.pro;
  18. //token过期时间
  19. private const int EXPIRE_DAYS = 6;
  20. //游戏时间
  21. private const int GAME_EXPIRE_MINUTES = 1440;
  22. public UrlConfig urlConfig;
  23. public Action<AuthenticationEvent, string> onAuthenticationEventCallback;
  24. //private string unionId = null;
  25. private AuthenticationData localAuthenticationData;
  26. //计时器
  27. private float timer = 0f;
  28. //查看频率
  29. private float checkPerSecond = 60f;
  30. //启动Id
  31. private string gameBootId = string.Empty;
  32. //启动时间
  33. private DateTime gameBootTime;
  34. private int alreadyPlayedMinutes = 0;
  35. private bool alreadyGetAuthentication = false;
  36. //[HideInInspector]
  37. public bool testMode = false;
  38. public string testUserId = "202";
  39. //private NetworkReachability previousNetworkReachAbility;
  40. //初始化,设置Binder监听,
  41. private void Awake()
  42. {
  43. NetworkReachability networkReachability = Application.internetReachability;
  44. Init();
  45. GetPackageInfoByChannel(GetChannelId());
  46. }
  47. private void OnApplicationPause(bool pause)
  48. {
  49. return;
  50. if (!pause && alreadyGetAuthentication)
  51. {
  52. GetAuthenticationInfo();
  53. }
  54. }
  55. private void Init()
  56. {
  57. SetAuthenticationUnionIdListener();
  58. }
  59. private void SetAuthenticationUnionIdListener()
  60. {
  61. #if !UNITY_EDITOR
  62. AndroidJavaClass loginManager = new AndroidJavaClass("com.xr.login.LoginManager");
  63. AndroidJavaObject loginManagerInstance = loginManager.CallStatic<AndroidJavaObject>("getInstance");
  64. loginManagerInstance.Call("setGetLoginInfoUnityListener", new AuthenticationLoginListener(OnGetLoginInfo, OnServiceConnectError));
  65. #endif
  66. }
  67. //获取UnionId
  68. private void GetLoginInfo(string packageName)
  69. {
  70. Debug.Log("getLoginInfoFromAppStore");
  71. if (testMode)
  72. {
  73. Debug.LogError("TestMode");
  74. OnGetLoginInfo(JsonUtility.ToJson(new LoginInfo()
  75. {
  76. unionId = testUserId
  77. }));
  78. return;
  79. }
  80. #if UNITY_EDITOR
  81. OnGetLoginInfo(JsonUtility.ToJson(new LoginInfo()
  82. {
  83. unionId = testUserId
  84. }));
  85. return;
  86. #endif
  87. AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  88. AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
  89. AndroidJavaClass loginManager = new AndroidJavaClass("com.xr.login.LoginManager");
  90. AndroidJavaObject loginManagerInstance = loginManager.CallStatic<AndroidJavaObject>("getInstance");
  91. loginManagerInstance.Call("getLoginInfoFromAppStore", unityActivity, packageName);
  92. }
  93. //获取渠道Id
  94. private string GetChannelId()
  95. {
  96. if (testMode)
  97. {
  98. return "0";
  99. }
  100. #if !UNITY_EDITOR
  101. AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  102. AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
  103. AndroidJavaClass loginManager = new AndroidJavaClass("com.xr.login.LoginManager");
  104. AndroidJavaObject loginManagerInstance = loginManager.CallStatic<AndroidJavaObject>("getInstance");
  105. return loginManagerInstance.Call<string>("getChannelId", unityActivity);
  106. #else
  107. return "0";
  108. #endif
  109. }
  110. //private string GetSerialno()
  111. //{
  112. // AndroidJavaClass loginManager = new AndroidJavaClass("com.xr.login.LoginManager");
  113. // AndroidJavaObject loginManagerInstance = loginManager.CallStatic<AndroidJavaObject>("getInstance");
  114. // string serialno = loginManagerInstance.Call<string>("getSerialno");
  115. // Debug.Log("Serialno:" + serialno);
  116. // return serialno;
  117. //}
  118. //获取包名
  119. private string GetChannelPackageName()
  120. {
  121. string channelId = GetChannelId();
  122. Debug.Log("GetChannelPackageName:" + channelId);
  123. ChannelDataList channelDataList = LoadChannelData();
  124. List<ChannelData> channelDatas = channelDataList.channelDatas.Where(item => item.channelId == channelId).ToList();
  125. if (channelDatas == null || channelDatas.Count == 0)
  126. {
  127. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.GetChannelPackageNameFail, channelId);
  128. return string.Empty;
  129. }
  130. return channelDatas[0].packageName;
  131. }
  132. //呼出登录界面
  133. private void ShowLoginDialog()
  134. {
  135. string packageName = GetChannelPackageName();
  136. if (string.IsNullOrEmpty(packageName))
  137. {
  138. return;
  139. }
  140. AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  141. AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
  142. AndroidJavaClass loginManager = new AndroidJavaClass("com.xr.login.LoginManager");
  143. AndroidJavaObject loginManagerInstance = loginManager.CallStatic<AndroidJavaObject>("getInstance");
  144. loginManagerInstance.Call("showLoginDialog", unityActivity, Application.identifier, packageName);
  145. }
  146. private void OnServiceConnectError()
  147. {
  148. if (Application.internetReachability == NetworkReachability.NotReachable)
  149. {
  150. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.CannotGetChannelUserId, "");
  151. return;
  152. }
  153. ShowLoginDialog();
  154. }
  155. private void OnGetLoginInfo(string loginInfoStr)
  156. {
  157. Debug.Log("OnGetLoginInfo:" + loginInfoStr);
  158. LoginInfo loginInfo = JsonUtility.FromJson<LoginInfo>(loginInfoStr);
  159. //无网状态下
  160. if (Application.internetReachability == NetworkReachability.NotReachable)
  161. {
  162. //无网状态无法获取个人中心用户信息
  163. if (loginInfo == null || string.IsNullOrEmpty(loginInfo.unionId))
  164. {
  165. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.CannotGetChannelUserId, "");
  166. return;
  167. }
  168. //无网状态本地鉴权信息与个人中心不一致,需要联网
  169. if (localAuthenticationData.channelUserId != loginInfo.unionId)
  170. {
  171. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.ChannelUserIdNotMatch, "channelUserId:" + localAuthenticationData.channelUserId + " " + " unionId:" + loginInfo.unionId);
  172. return;
  173. }
  174. //无网状态本地鉴权信息已过期
  175. if (CheckExpire())
  176. {
  177. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.LocalAuthenticationExpire, localAuthenticationData.refreshTime.ToString());
  178. return;
  179. }
  180. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.LocalAuthenticationOK, JsonUtility.ToJson(localAuthenticationData));
  181. return;
  182. }
  183. //有网状态无法获取个人中心用户信息
  184. if (loginInfo == null || string.IsNullOrEmpty(loginInfo.unionId))
  185. {
  186. ShowLoginDialog();
  187. return;
  188. }
  189. string channelId = GetChannelId();
  190. #if !UNITY_EDITOR
  191. string appPackageName = Application.identifier;
  192. #else
  193. string appPackageName = "com.Company.jianping";
  194. #endif
  195. string deviceId = API_Module_Device.Current.SN; //GetSerialno();//
  196. GetNetworkAuthenticationInfo(channelId, appPackageName, deviceId, loginInfo.unionId);
  197. }
  198. //判断token是否过期,判断游戏时间是否过期
  199. private bool CheckExpire()
  200. {
  201. if (localAuthenticationData != null)
  202. {
  203. TimeSpan expireTimeSpan = DateTime.Now - TimeUtils.ConvertToDateTime(localAuthenticationData.refreshTime);
  204. return expireTimeSpan.TotalDays > EXPIRE_DAYS || localAuthenticationData.playMinutes > GAME_EXPIRE_MINUTES || DateTime.Now < TimeUtils.ConvertToDateTime(localAuthenticationData.checkTime);
  205. }
  206. return true;
  207. }
  208. private void Update()
  209. {
  210. if (localAuthenticationData == null)
  211. {
  212. return;
  213. }
  214. if (string.IsNullOrEmpty(gameBootId))
  215. {
  216. gameBootId = Guid.NewGuid().ToString();
  217. gameBootTime = DateTime.Now;
  218. alreadyPlayedMinutes = localAuthenticationData.playMinutes;
  219. GenerateReportData(1);
  220. GenerateReportData(3);
  221. }
  222. timer += Time.deltaTime;
  223. if (timer > checkPerSecond)
  224. {
  225. timer = 0f;
  226. GenerateReportData(2);
  227. GenerateReportData(3);
  228. UpdateReportData();
  229. //在游戏中出现过期
  230. if (CheckExpire())
  231. {
  232. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.LocalAuthenticationExpire, localAuthenticationData.refreshTime.ToString());
  233. if (Application.internetReachability != NetworkReachability.NotReachable)
  234. {
  235. ShowLoginDialog();
  236. }
  237. }
  238. else
  239. {
  240. TimeSpan timeSpan = DateTime.Now - gameBootTime;
  241. Debug.Log("playMinutes:" + Mathf.RoundToInt((float)timeSpan.TotalMinutes) + " alreadyPlayMinutes:" + alreadyPlayedMinutes);
  242. localAuthenticationData.playMinutes = alreadyPlayedMinutes + Mathf.RoundToInt((float)timeSpan.TotalMinutes);
  243. localAuthenticationData.checkTime = TimeUtils.ConvertToTimeStamp(DateTime.Now);
  244. SaveAuthenticationData(localAuthenticationData);
  245. }
  246. }
  247. }
  248. public void GetAuthenticationInfo()
  249. {
  250. alreadyGetAuthentication = true;
  251. string channelPackageName = GetChannelPackageName();
  252. if (string.IsNullOrEmpty(channelPackageName))
  253. {
  254. return;
  255. }
  256. Debug.Log("ChannelPackageName:" + channelPackageName);
  257. if (Application.internetReachability == NetworkReachability.NotReachable)
  258. {
  259. GetLocalAuthenticationInfo(channelPackageName);
  260. return;
  261. }
  262. GetLoginInfo(channelPackageName);
  263. }
  264. public void GetPackageInfoByChannel(string channelId)
  265. {
  266. string requestUrl = GetConfigUrl() + NetworkApiConstant.SDK_PACKAGENAME;
  267. Dictionary<string, string> headerDic = new Dictionary<string, string>();
  268. headerDic.Add("Content-Type", "application/json");
  269. PackageInfoRequestData packageInfoRequestData = new PackageInfoRequestData();
  270. packageInfoRequestData.channelId = channelId;
  271. AuthenticationNetworkManager.Instance.Post<PackageInfoRequestData>(requestUrl, headerDic, packageInfoRequestData,
  272. (responseStr) =>
  273. {
  274. PackageInfoResponseData packageInfoResponseData = JsonUtility.FromJson<PackageInfoResponseData>(responseStr);
  275. if (packageInfoResponseData.code == 200)
  276. {
  277. Debug.Log("channelId:" + channelId + " packageName:" + packageInfoResponseData.data.packageName);
  278. RefreshChannelData(channelId, packageInfoResponseData.data.packageName);
  279. }
  280. },
  281. (errorStr) =>
  282. {
  283. //do nothing
  284. },
  285. EncryptUtils.AesEncrypt);
  286. }
  287. //联网获取鉴权信息
  288. private void GetNetworkAuthenticationInfo(string channelId, string appPackageName, string deviceId, string unionId)
  289. {
  290. string requestUrl = GetConfigUrl() + NetworkApiConstant.SDK_AUTHENTICATION; //"http://yapi.tech.shadowcreator.com/mock/205/v1/sdk/authentication?status=ok";
  291. Dictionary<string, string> headerDic = new Dictionary<string, string>();
  292. headerDic.Add("Content-Type", "application/json");
  293. AuthenticationRequestData authenticationRequestData = new AuthenticationRequestData();
  294. authenticationRequestData.channelId = channelId;
  295. authenticationRequestData.appPackageName = appPackageName;
  296. authenticationRequestData.deviceId = deviceId;
  297. authenticationRequestData.channelUserId = unionId;
  298. //Debug.LogError("RequestUrl:" + requestUrl);
  299. AuthenticationNetworkManager.Instance.Post<AuthenticationRequestData>(requestUrl, headerDic, authenticationRequestData,
  300. (responseStr) =>
  301. {
  302. //Debug.LogError("Response:" + responseStr);
  303. AuthenticationResponseData authenticationResponseData = JsonUtility.FromJson<AuthenticationResponseData>(responseStr);
  304. if (authenticationResponseData.code == 200)
  305. {
  306. authenticationResponseData.data.refreshTime = TimeUtils.ConvertToTimeStamp(DateTime.Now);
  307. authenticationResponseData.data.checkTime = TimeUtils.ConvertToTimeStamp(DateTime.Now);
  308. authenticationResponseData.data.playMinutes = 0;
  309. localAuthenticationData = authenticationResponseData.data;
  310. SaveAuthenticationData(authenticationResponseData.data);
  311. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.NetworkAuthenticationOK, JsonUtility.ToJson(authenticationResponseData.data));
  312. }
  313. else
  314. {
  315. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.NetworkAuthenticationFail, authenticationResponseData.code.ToString());
  316. //ShowLoginDialog();
  317. }
  318. },
  319. (errorStr) =>
  320. {
  321. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.NetworkAuthenticationFail, "NetworkError");
  322. //ShowLoginDialog();
  323. },
  324. EncryptUtils.AesEncrypt);
  325. }
  326. //获取本地鉴权信息
  327. private void GetLocalAuthenticationInfo(string packageName)
  328. {
  329. localAuthenticationData = LoadAuthenticationData();
  330. //判断本地是否保存鉴权信息
  331. if (localAuthenticationData == null)
  332. {
  333. onAuthenticationEventCallback?.Invoke(AuthenticationEvent.NoAuthenticationDataInLocal, "");
  334. return;
  335. }
  336. GetLoginInfo(packageName);
  337. }
  338. private string GetConfigUrl()
  339. {
  340. List<UrlData> urlDataList = urlConfig.urls;
  341. if (urlDataList == null)
  342. {
  343. Debug.LogError("urlConfig not config");
  344. return null;
  345. }
  346. List<UrlData> selectUrlDataList = urlDataList.Where((item) => item.urlName == URL_NAME).ToList();
  347. if (selectUrlDataList == null || selectUrlDataList.Count == 0)
  348. {
  349. Debug.LogError("selectUrlDataList is null or empty");
  350. return null;
  351. }
  352. return selectUrlDataList.First().url;
  353. }
  354. public void SaveAuthenticationData(AuthenticationData authenticationData)
  355. {
  356. PlayerPrefs.SetString(AUTHENTICATION_SAVE_KEY, JsonUtility.ToJson(authenticationData));
  357. PlayerPrefs.Save();
  358. }
  359. public AuthenticationData LoadAuthenticationData()
  360. {
  361. string json = PlayerPrefs.GetString(AUTHENTICATION_SAVE_KEY, string.Empty);
  362. return JsonUtility.FromJson<AuthenticationData>(json);
  363. }
  364. public void SaveReportData(ReportDataList reportDataList)
  365. {
  366. PlayerPrefs.SetString(REPORT_SAVE_KEY, JsonUtility.ToJson(reportDataList));
  367. PlayerPrefs.Save();
  368. }
  369. public ReportDataList LoadReportData()
  370. {
  371. string json = PlayerPrefs.GetString(REPORT_SAVE_KEY, string.Empty);
  372. return JsonUtility.FromJson<ReportDataList>(json);
  373. }
  374. public void SaveChannelData(ChannelDataList channelDataList)
  375. {
  376. PlayerPrefs.SetString(CHANNEL_SAVE_KEY, JsonUtility.ToJson(channelDataList));
  377. PlayerPrefs.Save();
  378. }
  379. public ChannelDataList LoadChannelData()
  380. {
  381. string json = PlayerPrefs.GetString(CHANNEL_SAVE_KEY, string.Empty);
  382. if (string.IsNullOrEmpty(json))
  383. {
  384. ChannelDataList channelDataList = new ChannelDataList();
  385. channelDataList.channelDatas = new List<ChannelData>();
  386. ChannelData defaultNoloData = new ChannelData();
  387. defaultNoloData.channelId = "4120948381";
  388. defaultNoloData.packageName = "com.nolo";
  389. ChannelData defaultMovisionxrData = new ChannelData();
  390. defaultMovisionxrData.channelId = "1335068101";
  391. defaultMovisionxrData.packageName = "com.movisionxr.unitysdk";
  392. ChannelData defaultDemoData = new ChannelData();
  393. defaultDemoData.channelId = "4938271003";
  394. defaultDemoData.packageName = "com.demo";
  395. ChannelData defaultTestData = new ChannelData();
  396. defaultDemoData.channelId = "0";
  397. defaultDemoData.packageName = "Test";
  398. channelDataList.channelDatas.Add(defaultNoloData);
  399. channelDataList.channelDatas.Add(defaultMovisionxrData);
  400. channelDataList.channelDatas.Add(defaultDemoData);
  401. channelDataList.channelDatas.Add(defaultTestData);
  402. return channelDataList;
  403. }
  404. return JsonUtility.FromJson<ChannelDataList>(json);
  405. }
  406. public void RefreshChannelData(string channelId, string packageName)
  407. {
  408. ChannelDataList channelDataList = LoadChannelData();
  409. for (int i = 0; i < channelDataList.channelDatas.Count; i++)
  410. {
  411. ChannelData channelData = channelDataList.channelDatas[i];
  412. if (channelData.channelId == channelId)
  413. {
  414. channelData.packageName = packageName;
  415. }
  416. }
  417. SaveChannelData(channelDataList);
  418. }
  419. #region 上报数据
  420. public void GenerateReportData(int status)
  421. {
  422. if (testMode) return;
  423. ReportDataList reportDataList = LoadReportData();
  424. if (reportDataList == null)
  425. {
  426. reportDataList = new ReportDataList();
  427. reportDataList.datas = new List<ReportData>();
  428. }
  429. if (status != 3)
  430. {
  431. ReportData reportData = new ReportData();
  432. reportData.gameBootId = gameBootId;
  433. reportData.reportId = Guid.NewGuid().ToString();
  434. reportData.status = status;
  435. reportData.startTime = GetTimeStampSeconds(gameBootTime);
  436. reportData.runTime = GetRunSeconds();
  437. reportDataList.datas.Add(reportData);
  438. }
  439. else
  440. {
  441. ChangeEndGameRuntime(reportDataList.datas);
  442. }
  443. SaveReportData(reportDataList);
  444. }
  445. //修改结束游戏时间/新增游戏结束时间
  446. private void ChangeEndGameRuntime(List<ReportData> reportDataList)
  447. {
  448. for (int i = 0; i < reportDataList.Count; i++)
  449. {
  450. ReportData reportData = reportDataList[i];
  451. if (reportData.status == 3 && reportData.gameBootId == gameBootId)
  452. {
  453. reportData.runTime = GetRunSeconds();
  454. return;
  455. }
  456. }
  457. ReportData newReportData = new ReportData();
  458. newReportData.gameBootId = gameBootId;
  459. newReportData.reportId = Guid.NewGuid().ToString();
  460. newReportData.status = 3;
  461. newReportData.startTime = GetTimeStampSeconds(gameBootTime);
  462. newReportData.runTime = GetRunSeconds();
  463. reportDataList.Add(newReportData);
  464. }
  465. private void UpdateReportData()
  466. {
  467. if (Application.internetReachability == NetworkReachability.NotReachable)
  468. {
  469. Debug.LogError("Cannot Update Report Data, no network");
  470. return;
  471. }
  472. string channelId = GetChannelId();
  473. string deviceId = API_Module_Device.Current.SN;//GetSerialno();
  474. string deviceBrand = "ShadowCreator";
  475. string deviceModel = API_Module_Device.Current.DeviceName;
  476. string systemName = SystemInfo.operatingSystem;
  477. string systemVersion = SystemInfo.operatingSystem;
  478. string androidVersion = API_Module_Device.Current.RELEASE_VERSION;
  479. string channelUserId = localAuthenticationData.channelUserId;
  480. #if !UNITY_EDITOR
  481. string appPackageName = Application.identifier;
  482. #else
  483. string appPackageName = "com.Company.jianping";
  484. #endif
  485. Debug.Log("deviceModel:" + deviceModel + " deviceId:" + deviceId);
  486. ReportDataList reportDataList = LoadReportData();
  487. if (reportDataList == null)
  488. {
  489. return;
  490. }
  491. List<ReportData> uploadReportDataList = reportDataList.datas.Where(item => item.status != 3 || (item.status == 3 && item.gameBootId != gameBootId)).ToList();
  492. //for (int i = 0; i < uploadReportDataList.Count; i++)
  493. //{
  494. //ReportData uploadReportData = uploadReportDataList[i];
  495. SendReportInfo(channelId, deviceId, deviceBrand, deviceModel, systemName, systemVersion, androidVersion, channelUserId, appPackageName,
  496. uploadReportDataList, (reportId) =>
  497. {
  498. ReportDataList localReportData = LoadReportData();
  499. int removeCount = reportDataList.datas.RemoveAll(item => item.status != 3 || (item.status == 3 && item.gameBootId != gameBootId));
  500. Debug.LogError("删除" + removeCount.ToString() + "条数据");
  501. SaveReportData(reportDataList);
  502. });
  503. //}
  504. }
  505. //上报数据接口
  506. private void SendReportInfo(string channelId, string deviceId, string deviceBrand, string deviceModel,
  507. string systemName, string systemVersion, string androidVersion, string channelUserId, string appPackageName, List<ReportData> reportData, Action<string> onReportSuccess)
  508. {
  509. if (testMode) return;
  510. string requestUrl = GetConfigUrl() + NetworkApiConstant.SDK_REPORT;
  511. Dictionary<string, string> headerDic = new Dictionary<string, string>();
  512. headerDic.Add("Content-Type", "application/json");
  513. ReportRequestDataList reportRequestDataList = new ReportRequestDataList();
  514. reportRequestDataList.data = new List<ReportRequestData>();
  515. for (int i = 0; i < reportData.Count; i++)
  516. {
  517. ReportRequestData reportRequestData = new ReportRequestData();
  518. reportRequestData.channelId = channelId;
  519. reportRequestData.deviceId = deviceId;
  520. reportRequestData.deviceBrand = deviceBrand;
  521. reportRequestData.deviceModel = deviceModel;
  522. reportRequestData.systemName = systemName;
  523. reportRequestData.systemVersion = systemVersion;
  524. reportRequestData.androidVersion = androidVersion;
  525. reportRequestData.channelUserId = channelUserId;
  526. reportRequestData.appPackageName = appPackageName;
  527. reportRequestData.status = reportData[i].status;
  528. reportRequestData.startTime = reportData[i].startTime;
  529. reportRequestData.runTime = reportData[i].runTime;
  530. reportRequestData.runUniqTag = reportData[i].gameBootId;
  531. reportRequestData.singleRunTime = reportData[i].status == 2 ? (long)checkPerSecond : 0;
  532. reportRequestDataList.data.Add(reportRequestData);
  533. }
  534. AuthenticationNetworkManager.Instance.Post<ReportRequestDataList>(requestUrl, headerDic, reportRequestDataList,
  535. (responseStr) =>
  536. {
  537. onReportSuccess?.Invoke(responseStr);
  538. },
  539. (errorStr) =>
  540. {
  541. },
  542. EncryptUtils.AesEncrypt);
  543. }
  544. public long GetTimeStampSeconds(DateTime dateTime)
  545. {
  546. TimeSpan ts = dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
  547. return Convert.ToInt64(ts.TotalSeconds);
  548. }
  549. public long GetRunSeconds()
  550. {
  551. TimeSpan ts = DateTime.Now - gameBootTime;
  552. return Convert.ToInt64(ts.TotalSeconds);
  553. }
  554. #endregion
  555. }