YZ_Tool_Native.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. import PlatUtils from "./PlatUtils";
  2. import { utils } from "./Utils";
  3. import YZ_Constant, { LevelStatus } from "./YZ_Constant";
  4. import YZ_LocalStorage from "./YZ_LocalStorage";
  5. const { ccclass, property } = cc._decorator;
  6. let ST_DefaultServerConfig: string = "";
  7. const ST_ServerUrl: string = "http://apps.youlesp.com/gss?";
  8. const POST_ServerUrl: string = "http://report.youlesp.com/gss?"
  9. // const ST_ServerUrl: string = "http://192.168.31.39:8080/GameBoxServer/gss?";
  10. // const POST_ServerUrl: string = "http://192.168.31.39:8080/GameBoxServer/gss?"
  11. @ccclass
  12. export default class YZ_Tool_Native {
  13. public className: string = "";
  14. _serverConfig: any = null;
  15. public get ServerConfig() {
  16. return this._serverConfig;
  17. }
  18. /**
  19. * 当前版本号
  20. */
  21. public gameVersion(): string {
  22. return utils.config.nativeAndroidConfig.version;
  23. }
  24. //设备UID
  25. _uid: string = "0";
  26. public get uid() {
  27. // if (this._service_uid != "0") return this._uid;
  28. // this._login();
  29. return "0";
  30. }
  31. //服务器返回UID
  32. _service_uid: string = "0";
  33. /**
  34. * 服务器返回UID
  35. */
  36. public get serviceId() {
  37. if (this._service_uid != "0") return this._service_uid;
  38. this.reportLogin();
  39. return "0";
  40. }
  41. /**
  42. * 0:默认互推面板 1: 跳转到原生平台
  43. */
  44. moreGameShowType: number = 0;
  45. /**
  46. * 显示退出游戏弹窗的类型
  47. * 0:不弹窗 1:弹窗
  48. */
  49. showGameExitDialogType: number = 0;
  50. appList: string = "";
  51. _reportLoginTime: number = 0;
  52. _reportLoginInterval: number = 30;
  53. isReport: boolean = false;
  54. /**
  55. * 上报登录接口获取UID
  56. */
  57. reportLogin() {
  58. if (this.isReport) return;
  59. this.isReport = true;
  60. let self = this;
  61. let curTime: number = new Date().getTime();
  62. let interval: number = (curTime - this._reportLoginTime) / 1000;
  63. if (interval > 0 && interval < this._reportLoginInterval) {
  64. utils.showLog(`上报登录获取UID小于:${this._reportLoginInterval}秒`);
  65. return;
  66. }
  67. this._reportLoginTime = curTime;
  68. let method = "m=loginv2";
  69. let url: string = ST_ServerUrl + method + `&device_data=${encodeURI(this.deviceInfo)}`;
  70. utils.commomHttpRequest(url, (ret, data) => {
  71. if (ret) {
  72. if (data) {
  73. let result = JSON.parse(data);
  74. utils.showLog(result);
  75. if (result.uid) {
  76. self._service_uid = "" + result.uid;
  77. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  78. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  79. }
  80. if (result.config) {
  81. this._serverConfig = utils.aesDecrypt(result.config);
  82. this.setLocalConfig(JSON.stringify(this._serverConfig));
  83. }
  84. }
  85. } else {
  86. utils.showLog("获取数据失败1");
  87. }
  88. this.isReport = false;
  89. })
  90. }
  91. /**
  92. *
  93. * @param data 配置数据
  94. */
  95. public init() {
  96. if (PlatUtils.IsNativeAndroid) {
  97. ST_DefaultServerConfig = this.getNativeData();
  98. if (ST_DefaultServerConfig) {
  99. let data: any = JSON.parse(ST_DefaultServerConfig);
  100. this.deviceInfo = data.device_info;
  101. this.moreGameShowType = data.more_game_type;
  102. this.showGameExitDialogType = data.show_game_exit_dialog;
  103. this.appList = data.app_list;
  104. } else {
  105. utils.showLog("没有获取到本地数据,组件初始化失败!");
  106. return;
  107. }
  108. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  109. this._service_uid = this._service_uid ? this._service_uid : "0";
  110. this._loadConfig();
  111. if (PlatUtils.IsNativeAndroid) {
  112. //初始化时监听返回按钮退出事件
  113. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, (event: any) => {
  114. if (event.keyCode == cc.macro.KEY.escape || event.keyCode == cc.macro.KEY.back) {
  115. if (PlatUtils.IsNativeAndroid) {
  116. if (this.showGameExitDialogType == 1) {
  117. this.showGameExitDialog();
  118. } else {
  119. this.GameExit();
  120. }
  121. }
  122. }
  123. }, this);
  124. }
  125. }
  126. }
  127. _gameExitDialogNode: cc.Node = null;
  128. /**
  129. * 显示退出弹窗
  130. */
  131. private showGameExitDialog() {
  132. if (!utils._isConfigInit) {
  133. utils.showLog("warn:" + "组件配置未初始化!");
  134. return null;
  135. }
  136. if (this._gameExitDialogNode && cc.isValid(this._gameExitDialogNode)) {
  137. utils.showLog("退出窗口还在。");
  138. return;
  139. }
  140. if (utils.config.otherconfig.gameExitDialog) {
  141. // if(utils.getRecommondGameList())
  142. let node: cc.Node = cc.instantiate(utils.config.otherconfig.gameExitDialog);
  143. if (node) {
  144. if (this._gameExitDialogNode && cc.isValid(this._gameExitDialogNode)) {
  145. this._gameExitDialogNode.destroy();
  146. }
  147. this._gameExitDialogNode = node;
  148. this._gameExitDialogNode.group = "default";
  149. cc.director.getScene().addChild(this._gameExitDialogNode, 9999);
  150. }
  151. } else {
  152. utils.showLog("warn:" + "未找到预制体 GameExitDialog, 请查看CommonUtils组件上是否赋值 !");
  153. }
  154. }
  155. public GameExit() {
  156. if (PlatUtils.IsNativeAndroid) {
  157. cc.log("AdAgentNative GameExit");
  158. try {
  159. jsb.reflection.callStaticMethod(this.jniClassName, "gameExit", "()V");
  160. } catch (error) {
  161. cc.log(error);
  162. }
  163. }
  164. }
  165. /**
  166. * 获取更多游戏显示的类型
  167. * 0:默认互推面板 1: 跳转到原生平台
  168. */
  169. private getMoreGameShowType() {
  170. try {
  171. this.moreGameShowType = jsb.reflection.callStaticMethod(this.jniClassName, "getMoreGameShowType", "()I");
  172. } catch (error) {
  173. utils.showLog(error);
  174. }
  175. }
  176. deviceInfo: string = "";
  177. /**
  178. * 获取手机设备信息
  179. *
  180. */
  181. private getDeviceInfo() {
  182. try {
  183. this.deviceInfo = jsb.reflection.callStaticMethod(this.jniClassName, "getDeviceInfo", "()Ljava/lang/String;");
  184. utils.showLog("获取原生平台手机设备信息:" + this.deviceInfo);
  185. } catch (error) {
  186. utils.showLog(error);
  187. }
  188. }
  189. /**
  190. * 跳转原生平台的更多游戏
  191. */
  192. public showMoreGames() {
  193. if (PlatUtils.IsNativeAndroid) {
  194. utils.showLog("AdAgentNative showMoreGame");
  195. try {
  196. jsb.reflection.callStaticMethod(this.jniClassName, "showNativeMoreGame", "()V");
  197. } catch (error) {
  198. cc.log(error);
  199. }
  200. }
  201. }
  202. _loadConfig() {
  203. if (PlatUtils.IsNativeAndroid) {
  204. let method: string = "m=g" + `&device_data=${encodeURI(this.deviceInfo)}&real_name_auth=${utils.getRealNameAuthLocalData()}`;
  205. this.commomHttpRequest(ST_ServerUrl + method, (ret, data) => {
  206. this._curloadTimes++;
  207. let recall: boolean = false;
  208. if (ret) {
  209. utils.showLog("原生安卓服务器配置数据获取成功: data = " + data);
  210. if (data) {
  211. let result = JSON.parse(data);
  212. if (result) {
  213. if (!utils.DebugLoacalConfig) {
  214. this._serverConfig = result;
  215. if (this._serverConfig.is_show_log_view && this._serverConfig.is_show_log_view == "true") {
  216. utils.showLogView = true;
  217. }
  218. if (result.more_game_type) {
  219. this.moreGameShowType = result.more_game_type;
  220. }
  221. if (result.show_game_exit_dialog) {
  222. this.showGameExitDialogType = result.show_game_exit_dialog;
  223. }
  224. } else {
  225. utils.showLog("开启了本地数据测试,使用本地配置!");
  226. }
  227. } else {
  228. utils.showLog("原生安卓服务器配置数据不是合法的JSON数据, 使用本地配置!");
  229. }
  230. }
  231. } else {
  232. utils.showLog("原生安卓服务器配置数据获取失败!");
  233. if (this._curloadTimes < this._reloadTimes) {
  234. utils.showLog("请求异常,重新拉取配置!");
  235. recall = true;
  236. }
  237. }
  238. if (!this._serverConfig) {
  239. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  240. } else {
  241. if (this._serverConfig.is_local_pos_id
  242. && this._serverConfig.is_local_pos_id == "false") {
  243. // 使用服务器下发的广告id
  244. utils.showLog("使用服务器下发的广告id");
  245. } else {
  246. utils.showLog("使用本地配置的广告ID");
  247. }
  248. }
  249. if (recall) {
  250. this._loadConfig();
  251. } else if (!recall || this._curloadTimes >= this._reloadTimes) {
  252. this.setLocalConfig(JSON.stringify(this._serverConfig));
  253. utils.emitServerInitEvent();
  254. }
  255. });
  256. }
  257. }
  258. _reloadTimes: number = 6;
  259. _curloadTimes: number = 0;
  260. /**
  261. * 通用http请求,只封装了GET请求
  262. * @param url 请求的url地址
  263. * @param callback Function(ret:boolean, data:string)
  264. * 请求结束回调,成功ret为true, data为返回的数据string。 失败为ret为false, data为空
  265. */
  266. private commomHttpRequest(url: string, callback: Function) {
  267. if (!utils._isConfigInit) {
  268. utils.showLog("warn:" + "本地数据未初始化!");
  269. return;
  270. }
  271. let completeCallback = callback;
  272. let xhr = new XMLHttpRequest();
  273. xhr.timeout = 6000; // 单位毫秒
  274. let requestUrl: string = url + `&kyx=false&app_id=${utils.config.nativeAndroidConfig.appID}&channel=${utils.config.nativeAndroidConfig.channel}&device_uid=${utils.Tool_Native.uid}&uid=${utils.Tool_Native.serviceId}&game_type=2&time_stamp=${(new Date()).getTime()}&game_version=${utils.config.nativeAndroidConfig.version}&reqv=${YZ_Constant.SERVER_VERSION}`
  275. utils.showLog("服务器地址:" + requestUrl);
  276. xhr.open('POST', requestUrl);
  277. xhr.send(`app_list=${this.appList}`);
  278. xhr.onreadystatechange = function () {
  279. utils.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status);
  280. if (xhr.readyState == 4) {
  281. if (xhr.status == 200) {
  282. if (completeCallback) {
  283. completeCallback(true, utils.aesDecrypt(xhr.responseText));
  284. }
  285. } else {
  286. if (completeCallback) {
  287. completeCallback(false, "");
  288. }
  289. }
  290. }
  291. if (xhr.status != 200) {
  292. }
  293. }
  294. xhr.ontimeout = function () {
  295. utils.showLog("请求超时!");
  296. if (completeCallback) {
  297. completeCallback(false, "");
  298. }
  299. }
  300. xhr.onerror = function (err) {
  301. if (completeCallback) {
  302. completeCallback(false, "");
  303. }
  304. }
  305. }
  306. /**
  307. * 设置原生客户端数据数据
  308. * @param data
  309. */
  310. private setLocalConfig(data: string) {
  311. try {
  312. utils.showLog("设置原生客户端数据数据" + data);
  313. jsb.reflection.callStaticMethod(this.jniClassName, "setLocalConfig", "(Ljava/lang/String;)V", data);
  314. } catch (error) {
  315. utils.showLog(error);
  316. }
  317. }
  318. /**
  319. * 获取原生客户端数据数据
  320. * @param data
  321. */
  322. public getNativeData(): string {
  323. let data = "";
  324. try {
  325. data = jsb.reflection.callStaticMethod(this.jniClassName, "getLocalConfig", "()Ljava/lang/String;");
  326. utils.showLog("获取原生客户端数据数据 : " + data);
  327. } catch (error) {
  328. utils.showLog(error);
  329. }
  330. return data;
  331. }
  332. /**
  333. *
  334. * @param id 跳转ID
  335. * @param callback 跳转回调
  336. */
  337. public navigateToGame(jsonData: string, callback: Function, path?: string) {
  338. if (PlatUtils.IsNativeAndroid) {
  339. try {
  340. jsb.reflection.callStaticMethod(this.jniClassName, "navigateToGame", "(Ljava/lang/String;)V", jsonData);
  341. } catch (error) {
  342. utils.showLog(error);
  343. }
  344. }
  345. }
  346. /**
  347. * 获取JNI类名
  348. */
  349. public get jniClassName() {
  350. if (this.className) return this.className;
  351. try {
  352. this.className = jsb.reflection.callStaticMethod("aa.b.c.a", "a", "()Ljava/lang/String;");
  353. } catch (error) {
  354. }
  355. return this.className;
  356. }
  357. /**
  358. * 获取交叉推广数据
  359. */
  360. public getRecommondGameList() {
  361. if (PlatUtils.IsNativeAndroid
  362. && utils.Tool_Native
  363. && utils.Tool_Native.ServerConfig) {
  364. return utils.Tool_Native.ServerConfig.jump_list;
  365. }
  366. return null;
  367. }
  368. /**
  369. * 上报关卡数据
  370. * @param level 当前关卡ID
  371. * @param levelName 关卡名称
  372. * @param status 状态
  373. */
  374. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  375. if(2>1){
  376. return;
  377. }
  378. if (PlatUtils.IsNativeAndroid) {
  379. let method = "m=rlevel";
  380. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  381. utils.commomHttpRequest(url, function (ret, data) {
  382. if (ret) {
  383. utils.showLog("关卡数据上报成功!");
  384. // if (status == LevelStatus.GameStart) {
  385. // utils.showLog("配置数据获取成功: data = " + data);
  386. // if (data) {
  387. // let result = JSON.parse(data);
  388. // if (result) {
  389. // let keys = Object.keys(result);
  390. // for (let key in keys) {
  391. // this._serverConfig[key] = result[key];
  392. // utils.showLog("覆盖:#key=" + key, " #value=", result[key]);
  393. // }
  394. // }
  395. // }
  396. // } else {
  397. // utils.showLog("OPPO服务器配置数据获取失败, 使用本地配置!");
  398. // }
  399. } else {
  400. utils.showLog("关卡数据上报失败!");
  401. }
  402. }.bind(this));
  403. }
  404. }
  405. /**
  406. * 上报互推组件显示位置
  407. * @param otherGameAppId 跳转的ID
  408. * @param location 当前位置
  409. * @param status 0:点击,1:跳转成功
  410. */
  411. public postRecommentShowData(location: string) {
  412. if (PlatUtils.IsNativeAndroid) {
  413. let method = "m=rjumpshow";
  414. let url: string = ST_ServerUrl + method + `&location=${location}`;
  415. utils.commomHttpRequest(url, function (ret, data) {
  416. if (ret) {
  417. utils.showLog("上报互推组件显示位置成功!");
  418. } else {
  419. utils.showLog("上报互推组件显示位置失败!");
  420. }
  421. }.bind(this));
  422. }
  423. }
  424. /**
  425. * 上报互推组件数据
  426. * @param otherGameAppId 跳转的ID
  427. * @param location 当前位置
  428. * @param status 0:点击,1:跳转成功
  429. */
  430. public postDataByLocation(otherGameAppId: string, location: string, status: number = 0) {
  431. if(2>1){
  432. return;
  433. }
  434. if (PlatUtils.IsNativeAndroid) {
  435. let method = "m=rjump";
  436. let url: string = POST_ServerUrl + method + `&jump_app_id=${otherGameAppId}&location=${location}&status=${status}`
  437. utils.commomHttpRequest(url, function (ret, data) {
  438. if (ret) {
  439. utils.showLog(`数据上报成功`);
  440. } else {
  441. utils.showLog("数据上报失败!");
  442. }
  443. }.bind(this));
  444. }
  445. }
  446. /**
  447. * 上报自定义事件
  448. * @param level 当前关卡ID
  449. * @param levelName 关卡名称
  450. * @param status 状态
  451. */
  452. public sendEvent(eventName: string) {
  453. if(2>1){
  454. return;
  455. }
  456. if (PlatUtils.IsNativeAndroid) {
  457. let method = "m=revent";
  458. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  459. utils.commomHttpRequest(url, function (ret, data) {
  460. if (ret) {
  461. utils.showLog("上报自定义事件成功!");
  462. } else {
  463. utils.showLog("上报自定义事件失败!");
  464. }
  465. }.bind(this));
  466. }
  467. }
  468. /**
  469. * 上报自定义事件
  470. * @param level 当前关卡ID
  471. * @param levelName 关卡名称
  472. * @param status 状态
  473. */
  474. public sendEventNew(eventName: string, eventId: string = "default", eventData?: string, isCallBack: boolean = true) {
  475. if(2>1){
  476. return;
  477. }
  478. if (PlatUtils.IsNativeAndroid) {
  479. let method = "m=reventv2";
  480. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}&event_id=${eventId}`;
  481. if (eventData) {
  482. url += `&event_data=${encodeURI(eventData)}`
  483. }
  484. utils.showLog("自定义事件上报URL:" + url);
  485. utils.commomHttpRequest(url, (ret, data) => {
  486. if (ret) {
  487. utils.showLog("上报自定义事件成功!");
  488. isCallBack && this.callBackToEvent(data);
  489. } else {
  490. utils.showLog("上报自定义事件失败!");
  491. isCallBack && this.callBackToEvent("false");
  492. }
  493. });
  494. }
  495. }
  496. /**
  497. * 上报自定义事件
  498. * @param level 当前关卡ID
  499. * @param levelName 关卡名称
  500. * @param status 状态
  501. */
  502. public sendEventV3(eventName: string, eventId: string = "default", eventData?: string) {
  503. if (PlatUtils.IsNativeAndroid) {
  504. let method = "m=reventv2";
  505. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}&event_id=${eventId}`;
  506. if (eventData) {
  507. url += `&event_data=${encodeURI(eventData)}`
  508. }
  509. utils.commomHttpRequest(url, (ret, data) => {
  510. if (ret) {
  511. utils.showLog("上报自定义事件成功!");
  512. this.callBackToEvent(data);
  513. } else {
  514. utils.showLog("上报自定义事件失败!");
  515. this.callBackToEvent("false");
  516. }
  517. });
  518. }
  519. }
  520. /**
  521. * 返回事件上报的结果给安卓端
  522. * @param result
  523. */
  524. public callBackToEvent(result: string) {
  525. if (PlatUtils.IsNativeAndroid) {
  526. //@ts-ignore
  527. try {
  528. jsb.reflection.callStaticMethod(this.jniClassName, "callBackToEvent", "(Ljava/lang/String;)V", result);
  529. } catch (error) {
  530. utils.showLog(error);
  531. }
  532. }
  533. }
  534. /**
  535. * 弹出提示框
  536. * @param msg 消息
  537. */
  538. public showToast(msg: string) {
  539. if (PlatUtils.IsNativeAndroid) {
  540. //@ts-ignore
  541. try {
  542. jsb.reflection.callStaticMethod(this.jniClassName, "showToast", "(Ljava/lang/String;)V", msg);
  543. } catch (error) {
  544. utils.showLog(error);
  545. }
  546. }
  547. }
  548. /**
  549. * 显示隐私协议页面
  550. */
  551. public showPrivacyAgreement() {
  552. try {
  553. //@ts-ignore
  554. jsb.reflection.callStaticMethod(this.jniClassName, "showPrivacyAgreement", "()V");
  555. } catch (error) {
  556. utils.showLog(error);
  557. }
  558. }
  559. public showRealNameAuthPanel(parmes: string) {
  560. try {
  561. //@ts-ignore
  562. jsb.reflection.callStaticMethod(this.jniClassName, "showRealNameAuthPanel", "(Ljava/lang/String;)V", parmes);
  563. } catch (error) {
  564. utils.showLog(error);
  565. }
  566. }
  567. public realNameAuthResult(parmes: string) {
  568. try {
  569. //@ts-ignore
  570. jsb.reflection.callStaticMethod(this.jniClassName, "realNameAuthResult", "(Ljava/lang/String;)V", parmes);
  571. } catch (error) {
  572. utils.showLog(error);
  573. }
  574. }
  575. /**
  576. * 实名认证
  577. */
  578. public realNameAuth(code: string, name: string, callBack: Function) {
  579. if(2>1){
  580. return;
  581. }
  582. if (!utils._isConfigInit) {
  583. utils.showLog("warn:" + "本地数据未初始化!");
  584. return;
  585. }
  586. let method: string = "m=realNameAuth"
  587. let completeCallback = callBack;
  588. let xhr = new XMLHttpRequest();
  589. xhr.timeout = 15000; // 单位毫秒
  590. let requestUrl: string = "https://apps.youlesp.com/gss?" + method + `&time_stamp=${(new Date()).getTime()}`;
  591. let data: any = {};
  592. data.kyx = false;
  593. data.app_id = utils.config.nativeAndroidConfig.appID;
  594. data.channel = utils.config.nativeAndroidConfig.channel;
  595. data.device_uid = utils.Tool_Native.uid;
  596. data.uid = utils.Tool_Native.serviceId;
  597. data.game_type = 2;
  598. data.device_data = this.deviceInfo;
  599. data.game_version = utils.config.nativeAndroidConfig.version;
  600. data.reqv = YZ_Constant.SERVER_VERSION;
  601. data.app_list = this.appList;
  602. data.id_card = code;
  603. data.real_name = name;
  604. utils.showLog("服务器地址:" + requestUrl);
  605. utils.showLog("请求参数:" + JSON.stringify(data));
  606. let requestData = JSON.stringify(data);
  607. xhr.open('POST', requestUrl);
  608. xhr.send("json_data=" + utils.aesEncrypt(requestData));
  609. utils.showLog("json_data=:" + utils.aesEncrypt(requestData));
  610. xhr.onreadystatechange = function () {
  611. utils.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status);
  612. if (xhr.readyState == 4) {
  613. if (xhr.status == 200) {
  614. if (completeCallback) {
  615. completeCallback(true, utils.aesDecrypt(xhr.responseText));
  616. }
  617. } else {
  618. if (completeCallback) {
  619. completeCallback(false, "");
  620. }
  621. }
  622. }
  623. if (xhr.status != 200) {
  624. }
  625. }
  626. xhr.ontimeout = function () {
  627. utils.showLog("请求超时!");
  628. if (completeCallback) {
  629. completeCallback(false, "");
  630. }
  631. }
  632. xhr.onerror = function (err) {
  633. if (completeCallback) {
  634. completeCallback(false, "");
  635. }
  636. }
  637. }
  638. /**
  639. * 友盟游戏开始上报
  640. * @param levelID
  641. */
  642. public umaOnStart(levelID: string) {
  643. if (!this.checkUmeng()) return;
  644. this.umaTrackEvent("level", "um_plus_game_level", {
  645. game_level: levelID,//该字段名称不可修改,必传
  646. game_states: `0`
  647. })
  648. }
  649. /**
  650. *
  651. * @returns 是否开启友盟
  652. */
  653. private checkUmeng(): boolean {
  654. // if (!utils.config.oppoconfig.umengId) {
  655. // return false;
  656. // }
  657. return true;
  658. }
  659. /**
  660. * 友盟结算上报
  661. * @param levelID 关卡id
  662. * @param stageId
  663. * @param event
  664. */
  665. public umaReportedLevel(levelID: string, event: LevelStatus) {
  666. if (!this.checkUmeng()) return;
  667. let status = 1;
  668. if (event == LevelStatus.GameFail) {
  669. status = -1;
  670. }
  671. this.umaTrackEvent("level", "um_plus_game_level", {
  672. game_level: levelID,//该字段名称不可修改,必传
  673. game_states: `${status}`
  674. })
  675. }
  676. /**
  677. * 友盟自定义事件
  678. * @param eventId 事件ID,注意:事件ID必须要在后台配置
  679. * @param params 事件内容
  680. */
  681. public umaTrackEvent(eventType: string = "custom", eventId: string, params?) {
  682. if (!this.checkUmeng()) return;
  683. try {
  684. if (params) {
  685. params = JSON.stringify(params);
  686. }
  687. //@ts-ignore
  688. jsb.reflection.callStaticMethod(this.jniClassName, "umaTrackEvent", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", eventType, eventId, params);
  689. } catch (error) {
  690. utils.showLog(error);
  691. }
  692. }
  693. }