YouWanAnalytics.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import PlatUtils from "../PlatUtils";
  2. import { utils } from "../Utils";
  3. import YZ_Constant from "../YZ_Constant";
  4. import YZ_LocalStorage from "../YZ_LocalStorage";
  5. import EventAdInfo, { AdEventParameter, YwAdStatus, YwAdType } from "./EventAdInfo";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class YouWanAnalytics {
  9. private static LOGIN_URL: string = "http://as.youlesp.com/as/login";
  10. private static EVENT_AD_URL: string = "http://e.youlesp.com/ae/ad";
  11. // private static LOGIN_URL: string = "http://192.168.31.108:8080/as/login";
  12. // private static EVENT_AD_URL: string = "http://192.168.31.108:8080/ae/ad";
  13. private static get _yw_uid(): number {
  14. return parseInt(YZ_LocalStorage.getItem(YZ_Constant.ST_YOUWAN_UID, "-1"));
  15. }
  16. public static login(callBackFunc?: Function) {
  17. if (!utils.config.otherconfig.yw_app_id) {
  18. this.showLog("yw_app_id 未配置,不进行上报!");
  19. return;
  20. }
  21. let data: any = {};
  22. this.getDeviceInfo(data);
  23. this.getStaticParame(data)
  24. this.connect(this.LOGIN_URL, data, (res, data) => {
  25. if (res) {
  26. this.showLog("登录成功! >>result:" + JSON.stringify(data) + " #uid=" + data.uid);
  27. YZ_LocalStorage.setItem(YZ_Constant.ST_YOUWAN_UID, data.uid);
  28. } else {
  29. this.showLog("登录失败!");
  30. }
  31. callBackFunc && callBackFunc(res, data);
  32. });
  33. }
  34. public static EventAd(adType: YwAdType, adStatus: YwAdStatus) {
  35. if (!utils.config.otherconfig.yw_app_id) {
  36. this.showLog("yw_app_id 未配置,不进行上报!");
  37. return;
  38. }
  39. if (!PlatUtils.IsOPPO) return;
  40. let adInfo = new EventAdInfo(adType, adStatus);
  41. let data: any = {};
  42. this.getStaticParame(data);
  43. data.ad_data = [];
  44. data.ad_data[0] = adInfo.toJsonData();
  45. this.connect(this.EVENT_AD_URL, data, (res) => {
  46. if (res) {
  47. this.showLog(`上报广告事件成功:${JSON.stringify(adInfo.toJsonData())}`);
  48. } else {
  49. this.showLog(`上报广告事件失败:${JSON.stringify(adInfo.toJsonData())}`);
  50. }
  51. });
  52. }
  53. public static EventAdWithObj(adType: YwAdType, adStatus: YwAdStatus, adEventParameter: AdEventParameter) {
  54. if (!utils.config.otherconfig.yw_app_id) {
  55. this.showLog("yw_app_id 未配置,不进行上报!");
  56. return;
  57. }
  58. if (!PlatUtils.IsOPPO) return;
  59. let adInfo = new EventAdInfo(adType, adStatus, adEventParameter);
  60. let data: any = {};
  61. this.getStaticParame(data);
  62. data.ad_data = [];
  63. data.ad_data[0] = adInfo.toJsonData();
  64. this.connect(this.EVENT_AD_URL, data, (res) => {
  65. if (res) {
  66. this.showLog(`上报广告事件成功:${JSON.stringify(adInfo.toJsonData())}`);
  67. } else {
  68. this.showLog(`上报广告事件失败:${JSON.stringify(adInfo.toJsonData())}`);
  69. }
  70. });
  71. }
  72. private static getStaticParame(data: any) {
  73. data.app_id = utils.config.otherconfig.yw_app_id;
  74. data.sdk_version = utils.utilsVersion;
  75. data.app_type = 1;
  76. if (this._yw_uid != -1) {
  77. data.uid = this._yw_uid;
  78. }
  79. data.country = "CN"
  80. data.uuid = YZ_LocalStorage.getItem(YZ_Constant.ST_UUID, "") || utils.generateUUID();
  81. if (PlatUtils.IsOPPO) {
  82. data.channel = "oppo";
  83. data.app_version = utils.config.oppoconfig.version;
  84. }
  85. }
  86. private static getDeviceInfo(data: any) {
  87. let systemInfo: any = {};
  88. if (PlatUtils.IsOPPO) {
  89. systemInfo = utils.oppoTool.SysInfo;
  90. data.pkg = utils.config.oppoconfig.packageName;
  91. data.device_id = utils.oppoTool._device_id;
  92. data.kernel_version = systemInfo.platformVersionCode;
  93. data.device_model = systemInfo.model;
  94. data.device_manufactory = systemInfo.brand;
  95. data.screen_height = systemInfo.screenHeight;
  96. data.screen_width = systemInfo.screenWidth;
  97. data.language = systemInfo.language;
  98. data.os = "Android";
  99. if (systemInfo.system.indexOf(" ") > -1) {
  100. data.os_version_release = systemInfo.system.substr(systemInfo.system.indexOf(" ") + 1);
  101. } else {
  102. data.os_version_release = systemInfo.system;
  103. }
  104. }
  105. }
  106. private static connect(url: string, parame: any, callBackFunc?: Function) {
  107. let xhr = new XMLHttpRequest();
  108. xhr.timeout = 6000; // 单位毫秒
  109. xhr.open('POST', url + "?time_stamp=" + (new Date()).getTime());
  110. xhr.send(`data=${JSON.stringify(parame)}`);
  111. this.showLog("connect:#url=" + url);
  112. this.showLog(`connect: #parame==${JSON.stringify(parame)}`);
  113. xhr.onreadystatechange = () => {
  114. this.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status + " ; responseText=", xhr.responseText);
  115. if (xhr.readyState == 4) {
  116. if (xhr.status == 200) {
  117. if (xhr.responseText) {
  118. try {
  119. let result = JSON.parse(xhr.responseText);
  120. if (result.code == 0) {
  121. callBackFunc && callBackFunc(true, result.data || "");
  122. } else {
  123. callBackFunc && callBackFunc(false);
  124. }
  125. } catch (error) {
  126. this.showLog("connect erro:#msg=" + error);
  127. }
  128. }
  129. } else {
  130. this.showLog(`connect erro: #parame==${JSON.stringify(parame)}`);
  131. callBackFunc && callBackFunc(false);
  132. }
  133. }
  134. }
  135. xhr.ontimeout = () => {
  136. this.showLog("connect timeout!");
  137. callBackFunc && callBackFunc(false);
  138. }
  139. xhr.onerror = () => {
  140. this.showLog("connect onerror!");
  141. callBackFunc && callBackFunc(false);
  142. }
  143. }
  144. private static showLog(msg, ...any: any[]): void {
  145. utils.showLog(`[YouWan] --- ${msg}`, any);
  146. }
  147. }