YZ_Tool_IOS.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 = "https://apps.youlesp.com/gss?";
  8. const POST_ServerUrl: string = "https://report.youletd.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_IOS {
  13. public className: string = "JNIHelper";
  14. _serverConfig: any = null;
  15. public get ServerConfig() {
  16. return this._serverConfig;
  17. }
  18. /**
  19. * 当前版本号
  20. */
  21. public gameVersion(): string {
  22. return utils.config.nativeIoSConfig.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=login";
  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("data=" + data);
  75. utils.showLog("result=" + result);
  76. utils.showLog("result.uid=" + result.uid);
  77. if (result.uid) {
  78. self._service_uid = "" + result.uid;
  79. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  80. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  81. }
  82. }
  83. } else {
  84. utils.showLog("获取数据失败1");
  85. }
  86. this.isReport = false;
  87. })
  88. }
  89. /**
  90. *
  91. * @param data 配置数据
  92. */
  93. public init(data) {
  94. if (PlatUtils.IsNativeIOS) {
  95. if (data) {
  96. let configObj: any = JSON.parse(data);
  97. if (configObj && configObj.ios) {
  98. utils.showLog("本地配置数据:" + JSON.stringify(configObj.ios));
  99. ST_DefaultServerConfig = JSON.stringify(configObj.ios);
  100. }
  101. }
  102. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  103. this._service_uid = this._service_uid ? this._service_uid : "0";
  104. this._loadConfig();
  105. // if (PlatUtils.IsNativeIOS) {
  106. // //初始化时监听返回按钮退出事件
  107. // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, (event: any) => {
  108. // if (event.keyCode == cc.macro.KEY.escape || event.keyCode == cc.macro.KEY.back) {
  109. // if (PlatUtils.IsNativeIOS) {
  110. // if (this.showGameExitDialogType == 1) {
  111. // this.showGameExitDialog();
  112. // } else {
  113. // this.GameExit();
  114. // }
  115. // }
  116. // }
  117. // }, this);
  118. // }
  119. }
  120. }
  121. _gameExitDialogNode: cc.Node = null;
  122. /**
  123. * 显示退出弹窗
  124. */
  125. private showGameExitDialog() {
  126. if (!utils._isConfigInit) {
  127. utils.showLog("warn:" + "组件配置未初始化!");
  128. return null;
  129. }
  130. if (this._gameExitDialogNode && cc.isValid(this._gameExitDialogNode)) {
  131. utils.showLog("退出窗口还在。");
  132. return;
  133. }
  134. if (utils.config.otherconfig.gameExitDialog) {
  135. // if(utils.getRecommondGameList())
  136. let node: cc.Node = cc.instantiate(utils.config.otherconfig.gameExitDialog);
  137. if (node) {
  138. if (this._gameExitDialogNode && cc.isValid(this._gameExitDialogNode)) {
  139. this._gameExitDialogNode.destroy();
  140. }
  141. this._gameExitDialogNode = node;
  142. this._gameExitDialogNode.group = "default";
  143. cc.director.getScene().addChild(this._gameExitDialogNode, 9999);
  144. }
  145. } else {
  146. utils.showLog("warn:" + "未找到预制体 GameExitDialog, 请查看CommonUtils组件上是否赋值 !");
  147. }
  148. }
  149. public GameExit() {
  150. if (PlatUtils.IsNativeIOS) {
  151. cc.log("AdAgentNative GameExit");
  152. try {
  153. jsb.reflection.callStaticMethod(this.className, "gameExit");
  154. } catch (error) {
  155. cc.log(error);
  156. }
  157. }
  158. }
  159. /**
  160. * 获取更多游戏显示的类型
  161. * 0:默认互推面板 1: 跳转到原生平台
  162. */
  163. private getMoreGameShowType() {
  164. try {
  165. this.moreGameShowType = jsb.reflection.callStaticMethod(this.className, "getMoreGameShowType", "()I");
  166. } catch (error) {
  167. utils.showLog(error);
  168. }
  169. }
  170. deviceInfo: string = "";
  171. /**
  172. * 获取手机设备信息
  173. *
  174. */
  175. private getDeviceInfo() {
  176. try {
  177. this.deviceInfo = jsb.reflection.callStaticMethod(this.className, "getDeviceInfo", "()Ljava/lang/String;");
  178. utils.showLog("获取原生平台手机设备信息:" + this.deviceInfo);
  179. } catch (error) {
  180. utils.showLog(error);
  181. }
  182. }
  183. /**
  184. * 跳转原生平台的更多游戏
  185. */
  186. public showMoreGames() {
  187. if (PlatUtils.IsNativeIOS) {
  188. utils.showLog("AdAgentNative showMoreGame");
  189. try {
  190. jsb.reflection.callStaticMethod(this.className, "showNativeMoreGame", "()V");
  191. } catch (error) {
  192. cc.log(error);
  193. }
  194. }
  195. }
  196. _loadConfig() {
  197. if (PlatUtils.IsNativeIOS) {
  198. let method: string = "m=g";
  199. utils.commomHttpRequest(ST_ServerUrl + method, (ret, data) => {
  200. if (ret) {
  201. utils.showLog("原生IOS服务器配置数据获取成功: data = " + data);
  202. if (data) {
  203. let result = JSON.parse(data);
  204. if (result) {
  205. if (!utils.DebugLoacalConfig) {
  206. this._serverConfig = result;
  207. if (this._serverConfig.is_show_log_view && this._serverConfig.is_show_log_view == "true") {
  208. utils.showLogView = true;
  209. }
  210. } else {
  211. utils.showLog("开启了本地数据测试,使用本地配置!");
  212. }
  213. } else {
  214. utils.showLog("原生安卓服务器配置数据不是合法的JSON数据, 使用本地配置!");
  215. }
  216. }
  217. } else {
  218. utils.showLog("原生安卓服务器配置数据获取失败, 使用本地配置!");
  219. }
  220. if (!this._serverConfig) {
  221. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  222. } else {
  223. if (this._serverConfig.is_local_pos_id
  224. && this._serverConfig.is_local_pos_id == "false") {
  225. // 使用服务器下发的广告id
  226. utils.showLog("使用服务器下发的广告id");
  227. utils.config.nativeIoSConfig.bannerId = this._serverConfig.banner_pos_id;
  228. utils.config.nativeIoSConfig.insertId = this._serverConfig.intersititia_pos_id;
  229. utils.config.nativeIoSConfig.videoId = this._serverConfig.video_pos_id;
  230. } else {
  231. this._serverConfig.video_pos_id = utils.config.nativeIoSConfig.videoId
  232. this._serverConfig.banner_pos_id = utils.config.nativeIoSConfig.bannerId;
  233. this._serverConfig.intersititia_pos_id = utils.config.nativeIoSConfig.insertId;
  234. utils.showLog("使用本地配置的广告ID");
  235. }
  236. }
  237. utils.showLog("执行IOS >>> 初始化");
  238. jsb.reflection.callStaticMethod(this.className, "init:", JSON.stringify(this._serverConfig));
  239. // this.setLocalConfig(JSON.stringify(this._serverConfig));
  240. utils.emitServerInitEvent();
  241. });
  242. }
  243. }
  244. /**
  245. * 通用http请求,只封装了GET请求
  246. * @param url 请求的url地址
  247. * @param callback Function(ret:boolean, data:string)
  248. * 请求结束回调,成功ret为true, data为返回的数据string。 失败为ret为false, data为空
  249. */
  250. private commomHttpRequest(url: string, callback: Function) {
  251. if (!utils._isConfigInit) {
  252. utils.showLog("warn:" + "本地数据未初始化!");
  253. return;
  254. }
  255. let completeCallback = callback;
  256. let xhr = new XMLHttpRequest();
  257. xhr.timeout = 6000; // 单位毫秒
  258. 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()}`
  259. utils.showLog("服务器地址:" + requestUrl);
  260. xhr.open('POST', requestUrl);
  261. xhr.send(`app_list=${this.appList}`);
  262. xhr.onreadystatechange = function () {
  263. utils.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status);
  264. if (xhr.readyState == 4 && xhr.status == 200) {
  265. if (completeCallback) {
  266. completeCallback(true, xhr.responseText);
  267. }
  268. }
  269. if (xhr.status != 200) {
  270. if (completeCallback) {
  271. completeCallback(false, "");
  272. }
  273. }
  274. }
  275. xhr.ontimeout = function () {
  276. utils.showLog("请求超时!");
  277. if (completeCallback) {
  278. completeCallback(false, "");
  279. }
  280. }
  281. xhr.onerror = function (err) {
  282. if (completeCallback) {
  283. completeCallback(false, "");
  284. }
  285. }
  286. }
  287. /**
  288. * 设置原生客户端数据数据
  289. * @param data
  290. */
  291. private setLocalConfig(data: string) {
  292. try {
  293. utils.showLog("设置原生客户端数据数据" + data);
  294. jsb.reflection.callStaticMethod(this.className, "setLocalConfig", "(Ljava/lang/String;)V", data);
  295. } catch (error) {
  296. utils.showLog(error);
  297. }
  298. }
  299. /**
  300. * 获取原生客户端数据数据
  301. * @param data
  302. */
  303. public getNativeData(): string {
  304. let data = "";
  305. try {
  306. data = jsb.reflection.callStaticMethod(this.className, "getLocalConfig");
  307. utils.showLog("获取原生客户端数据数据 : " + data);
  308. } catch (error) {
  309. utils.showLog(error);
  310. }
  311. return data;
  312. }
  313. /**
  314. *
  315. * @param id 跳转ID
  316. * @param callback 跳转回调
  317. */
  318. public navigateToGame(appId: string, callback: Function, path?: string) {
  319. if (PlatUtils.IsNativeIOS) {
  320. try {
  321. jsb.reflection.callStaticMethod(this.className, "navigateToGame:", appId);
  322. } catch (error) {
  323. utils.showLog(error);
  324. }
  325. }
  326. }
  327. /**
  328. * 获取交叉推广数据
  329. */
  330. public getRecommondGameList() {
  331. if (PlatUtils.IsNativeIOS
  332. && utils.Tool_IOS
  333. && utils.Tool_IOS.ServerConfig) {
  334. return utils.Tool_IOS.ServerConfig.jump_list;
  335. }
  336. return null;
  337. }
  338. /**
  339. * 上报关卡数据
  340. * @param level 当前关卡ID
  341. * @param levelName 关卡名称
  342. * @param status 状态
  343. */
  344. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  345. if (PlatUtils.IsNativeIOS) {
  346. let method = "m=rlevel";
  347. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  348. utils.commomHttpRequest(url, function (ret, data) {
  349. if (ret) {
  350. utils.showLog("关卡数据上报成功!");
  351. } else {
  352. utils.showLog("关卡数据上报失败!");
  353. }
  354. }.bind(this));
  355. }
  356. }
  357. /**
  358. * 上报自定义事件
  359. * @param level 当前关卡ID
  360. * @param levelName 关卡名称
  361. * @param status 状态
  362. */
  363. public sendEvent(eventName: string) {
  364. if (PlatUtils.IsNativeIOS) {
  365. let method = "m=revent";
  366. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  367. utils.commomHttpRequest(url, function (ret, data) {
  368. if (ret) {
  369. utils.showLog("上报自定义事件成功!");
  370. } else {
  371. utils.showLog("上报自定义事件失败!");
  372. }
  373. }.bind(this));
  374. }
  375. }
  376. /**
  377. * 弹出提示框
  378. * @param msg 消息
  379. */
  380. public showToast(msg: string) {
  381. if (PlatUtils.IsNativeIOS) {
  382. console.log("showToast >>>>>");
  383. //@ts-ignore
  384. jsb.reflection.callStaticMethod(this.className, "showToast:", msg);
  385. }
  386. }
  387. /**
  388. * 实名认证
  389. */
  390. public realNameAuth(code: string, name: string, callBack: Function) {
  391. if (!utils._isConfigInit) {
  392. utils.showLog("warn:" + "本地数据未初始化!");
  393. return;
  394. }
  395. let method: string = "m=realNameAuth"
  396. let completeCallback = callBack;
  397. let xhr = new XMLHttpRequest();
  398. xhr.timeout = 15000; // 单位毫秒
  399. let requestUrl: string = "https://apps.youlesp.com/gss?" + method + `&time_stamp=${(new Date()).getTime()}`;
  400. let data: any = {};
  401. data.kyx = false;
  402. data.app_id = utils.config.nativeIoSConfig.appID;
  403. data.channel = "ios";
  404. data.device_uid = this.uid;
  405. data.uid = this.serviceId;
  406. data.game_type = 2;
  407. data.device_data = this.deviceInfo;
  408. data.game_version = utils.config.nativeIoSConfig.version;
  409. data.reqv = YZ_Constant.SERVER_VERSION;
  410. data.app_list = this.appList;
  411. data.id_card = code;
  412. data.real_name = name;
  413. utils.showLog("服务器地址:" + requestUrl);
  414. utils.showLog("请求参数:" + JSON.stringify(data));
  415. let requestData = JSON.stringify(data);
  416. xhr.open('POST', requestUrl);
  417. xhr.send("json_data=" + utils.aesEncrypt(requestData));
  418. utils.showLog("json_data=:" + utils.aesEncrypt(requestData));
  419. xhr.onreadystatechange = function () {
  420. utils.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status);
  421. if (xhr.readyState == 4) {
  422. if (xhr.status == 200) {
  423. if (completeCallback) {
  424. completeCallback(true, utils.aesDecrypt(xhr.responseText));
  425. }
  426. } else {
  427. if (completeCallback) {
  428. completeCallback(false, "");
  429. }
  430. }
  431. }
  432. if (xhr.status != 200) {
  433. }
  434. }
  435. xhr.ontimeout = function () {
  436. utils.showLog("请求超时!");
  437. if (completeCallback) {
  438. completeCallback(false, "");
  439. }
  440. }
  441. xhr.onerror = function (err) {
  442. if (completeCallback) {
  443. completeCallback(false, "");
  444. }
  445. }
  446. }
  447. }