JsbSystem.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import { DataEventId } from "../../game/data/GameData";
  2. /**
  3. * Jsb管理类(与安卓Java类或者IOS交互)
  4. * @description
  5. * @author 冯聪
  6. */
  7. export default class JsbSystem {
  8. /**代码桥在AS中的路径 */
  9. private static _JSB_ANDROID_PATH: string = "org/cocos2dx/javascript/thirdSdk/ThirdSdkMgr";
  10. /**代码桥在IOS中的路径 */
  11. private static _JSB_IOS_PATH: string = "ThirdSdkMgr";
  12. //callBack
  13. public static callBack: Function = null;
  14. /**
  15. * 是否文件存在
  16. * @param filePath 文件路径
  17. * @returns 是否文件存在 boolean
  18. */
  19. public static isFileExist(fileName: string): boolean {
  20. if (cc.sys.os == cc.sys.OS_WINDOWS) {
  21. return true;
  22. }
  23. let filePath = this.getWritablePath() + fileName + ".apk";
  24. return jsb.fileUtils.isFileExist(filePath);
  25. }
  26. /**
  27. * 获取写入的路径
  28. * @description
  29. * @returns path 写入路径(string)
  30. */
  31. public static getWritablePath(): string {
  32. return jsb.fileUtils.getWritablePath();
  33. }
  34. /**
  35. * 调用项目工程静态方法
  36. * @description 调用Android与IOS对应的静态方法
  37. * @param path 调用路径:"org/cocos2dx/javascript/AppActivity":java中的包名(.换成/)+class文件名,就当是路径好了
  38. * @param methodName 调用的方法名:必须public static
  39. * @param methodSignature 方法签名:“(Ljava/lang/String;)Ljava/lang/String;”:()内的是调用的java方法的参数类型,()外面的是返回值类
  40. * @param parameters 参数:是java方法需要传入的参数(可以多个,与methodSignature中()内的数量和类型一一对应)
  41. */
  42. public static callStaticMethod(path: string, methodName: string, methodSignature: string, ...parameters: any): any {
  43. let value = jsb.reflection.callStaticMethod(path, methodName, methodSignature, ...parameters);
  44. return value;
  45. }
  46. /**
  47. * Java调用返回测试
  48. * @description 自定义功能 JavaToCocos
  49. */
  50. public static backTest() {
  51. }
  52. /**
  53. * 更新设备信息
  54. * @description 自定义功能 JavaToCocos
  55. * @param info info(Java返回)
  56. */
  57. public static updateDeviceInfo(info) {
  58. mk.console.log("===[JsbSystem] updateDeviceInfo", info)
  59. if (cc.sys.os == cc.sys.OS_ANDROID) {
  60. gData.appData.updateDeviceInfo(info);
  61. }
  62. else if (cc.sys.os == cc.sys.OS_IOS) {
  63. }
  64. }
  65. /** 获取手机授权 */
  66. public static checkPermission() {
  67. if (CC_DEBUG) {
  68. gData.appData.mac = '81:16:ec:c3:d3:55,81:46:dc:dd:b7:57';
  69. gData.appData.machineInfo.mac = gData.appData.mac;
  70. gData.appData.getDeviceInfoCompelete = true;
  71. }
  72. if (cc.sys.os == cc.sys.OS_ANDROID) {
  73. this.callStaticMethod(this._JSB_ANDROID_PATH, "checkPermission", "()V");
  74. }
  75. else if (cc.sys.os == cc.sys.OS_IOS) {
  76. jsb.reflection.callStaticMethod("GameConfig", "checkPermission:", "");
  77. }
  78. }
  79. /**
  80. * 获取设备信息
  81. * @description 自定义功能 CocosToJava
  82. */
  83. public static getDeviceInfo() {
  84. let deviceInfo = "";
  85. if (cc.sys.os == cc.sys.OS_ANDROID) {
  86. deviceInfo = this.callStaticMethod(this._JSB_ANDROID_PATH, "getDeviceInfo", "()Ljava/lang/String;");
  87. console.log("===[JsbSystem] getDeviceInfo ", deviceInfo);
  88. gData.appData.updateDeviceInfo(deviceInfo);
  89. }
  90. else if (cc.sys.os == cc.sys.OS_IOS) {
  91. this.callStaticMethod(this._JSB_IOS_PATH, "getDeviceInfo", "");
  92. }
  93. else {
  94. gData.appData.getDeviceInfoCompelete = true;
  95. }
  96. }
  97. /**
  98. * 获取包名
  99. * @description 自定义功能 CocosToJava
  100. * @returns packName 包名
  101. */
  102. public static getPackageName() {
  103. let packName = '';
  104. if (cc.sys.os == cc.sys.OS_ANDROID) {
  105. packName = this.callStaticMethod(this._JSB_ANDROID_PATH, "getPackageName", "()Ljava/lang/String;");
  106. }
  107. else if (cc.sys.os == cc.sys.OS_IOS) {
  108. packName = this.callStaticMethod("CsjAdReward", "getPackageName", "");
  109. }
  110. return packName;
  111. }
  112. /**
  113. * 获取appid 和 配置version
  114. * @description 自定义功能 CocosToJava
  115. * @returns appid
  116. */
  117. public static getWxAppId() {
  118. let unicodeStr = "";
  119. if (cc.sys.os == cc.sys.OS_ANDROID) {
  120. unicodeStr = this.callStaticMethod(this._JSB_ANDROID_PATH, "getWxAppId", "()Ljava/lang/String;");
  121. }
  122. else if (cc.sys.os == cc.sys.OS_IOS) {
  123. unicodeStr = this.callStaticMethod("CsjAdReward", "getWxAppId", "",);
  124. }
  125. let unicodeArr = unicodeStr.split(';');
  126. if (unicodeArr) {
  127. gData.appData.appId = unicodeArr[0];
  128. gData.appData.version = unicodeArr[1];
  129. }
  130. if (CC_DEBUG) {
  131. gData.appData.appId = "wx667751c00aad1686";
  132. gData.appData.version = "1.1.1";
  133. }
  134. // console.log('gData.appData ', unicodeStr)
  135. // console.log('gData.appData.appId ', gData.appData.appId)
  136. // console.log('gData.appData.version ', gData.appData.version)
  137. }
  138. /**
  139. * 获取广告渠道和相关id
  140. * @description 自定义功能 CocosToJava
  141. * @returns str
  142. */
  143. public static getAdvertisingChannel(): string {
  144. let str = "";
  145. if (cc.sys.os == cc.sys.OS_ANDROID) {
  146. str = this.callStaticMethod(this._JSB_ANDROID_PATH, "getAdvertisingChannel", "()Ljava/lang/String;");
  147. }
  148. else if (cc.sys.os == cc.sys.OS_IOS) {
  149. str = this.callStaticMethod("CsjAdReward", "AdvertisingChannel", "");
  150. }
  151. return str;
  152. }
  153. /** 显示开屏 */
  154. static showSplash() {
  155. // mk.data.sendDataEvent('slashAd', '开屏')
  156. if (cc.sys.os == cc.sys.OS_ANDROID) {
  157. this.callStaticMethod(this._JSB_ANDROID_PATH, "showSplash", "()V");
  158. }
  159. else if (cc.sys.os == cc.sys.OS_IOS) {
  160. jsb.reflection.callStaticMethod("GameConfig", "showSplash:", "");
  161. }
  162. }
  163. /**
  164. * 复制到剪切板
  165. * @description Andorid功能 CocosToJava
  166. * @param str 剪切板字符串内容
  167. */
  168. public static setClipboard(str: string) {
  169. if (cc.sys.os == cc.sys.OS_ANDROID) {
  170. this.callStaticMethod(this._JSB_ANDROID_PATH, "setClipboard", "(Ljava/lang/String;)V", str);
  171. }
  172. else if (cc.sys.os == cc.sys.OS_IOS) {
  173. }
  174. else if (cc.sys.isBrowser) {
  175. var input = str;
  176. const el: any = document.createElement('textarea');
  177. el.value = input;
  178. el.setAttribute('readonly', '');
  179. el.style.contain = 'strict';
  180. el.style.position = 'absolute';
  181. el.style.left = '-9999px';
  182. el.style.fontSize = '12pt';
  183. const selection = getSelection();
  184. var originalRange: any = false;
  185. if (selection.rangeCount > 0) {
  186. originalRange = selection.getRangeAt(0);
  187. }
  188. document.body.appendChild(el);
  189. el.select();
  190. el.selectionStart = 0;
  191. el.selectionEnd = input.length;
  192. var success = false;
  193. try {
  194. success = document.execCommand('copy');
  195. } catch (err) { }
  196. document.body.removeChild(el);
  197. if (originalRange) {
  198. selection.removeAllRanges();
  199. selection.addRange(originalRange);
  200. }
  201. return success;
  202. }
  203. }
  204. /**
  205. * 请求用户授权
  206. * @description Andorid功能 CocosToJava
  207. */
  208. public static requestPermission() {
  209. if (cc.sys.os == cc.sys.OS_ANDROID) {
  210. this.callStaticMethod(this._JSB_ANDROID_PATH, "requestPermission", "()V");
  211. }
  212. else if (cc.sys.os == cc.sys.OS_IOS) {
  213. this.callStaticMethod("AppController", "requestPermission", "");
  214. }
  215. }
  216. /**
  217. * 获取安卓的版本号
  218. * @description Andorid功能 CocosToJava
  219. */
  220. public static getAppVersion() {
  221. if (cc.sys.os == cc.sys.OS_ANDROID) {
  222. this.callStaticMethod(this._JSB_ANDROID_PATH, "getAppVersion", "()V");
  223. }
  224. else if (cc.sys.os == cc.sys.OS_IOS) {
  225. this.callStaticMethod("AppController", "getAppVersion", "");
  226. }
  227. }
  228. /**
  229. * 设置安卓得版本号
  230. * @description Andorid功能 JavaToCocos
  231. * @param appVersion app版本号
  232. */
  233. public static setAppVersion(appVersion: string) {
  234. if (cc.sys.os == cc.sys.OS_ANDROID) {
  235. // GameConst.appVersion = appVersion;
  236. }
  237. else if (cc.sys.os == cc.sys.OS_IOS) {
  238. }
  239. }
  240. /**
  241. * 安装apk
  242. * @description Andorid功能 CocosToJava
  243. * @param apkName apkName
  244. */
  245. public static installApk(apkName: string) {
  246. let fullPath = this.getWritablePath() + apkName + ".apk";
  247. if (cc.sys.os == cc.sys.OS_ANDROID) {
  248. this.callStaticMethod(this._JSB_ANDROID_PATH, "installApk", "(Ljava/lang/String;)V", fullPath);
  249. }
  250. else if (cc.sys.os == cc.sys.OS_IOS) {
  251. this.callStaticMethod("GameConfig", "installApk:", "");
  252. }
  253. }
  254. /**
  255. * 是否可以启动游戏
  256. * @param packageName 包名
  257. * @param start 如存在是否启动
  258. * @returns ifCan
  259. */
  260. public static ifCanStartGame(packageName: string, start: boolean): boolean {
  261. let isCan = false;
  262. if (cc.sys.os == cc.sys.OS_ANDROID) {
  263. isCan = this.callStaticMethod(this._JSB_ANDROID_PATH, "isStartGame", "(Ljava/lang/String;Z)Z", packageName, start);
  264. return isCan;
  265. }
  266. else if (cc.sys.os == cc.sys.OS_IOS) {
  267. isCan = this.callStaticMethod("GameConfig", "installApk:", "");
  268. }
  269. return isCan;
  270. }
  271. /**
  272. * 数数事件注册
  273. * @description 数数SDK功能
  274. * @returns
  275. */
  276. static setTAEventRegister() {
  277. if (cc.sys.os == cc.sys.OS_ANDROID) {
  278. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventRegister", "()V");
  279. }
  280. else if (cc.sys.os == cc.sys.OS_IOS) {
  281. }
  282. }
  283. /**
  284. * 发送自定义事件 自定义功能 CocosToJava
  285. * @description 发送统计事件
  286. * @param key 事件的key值
  287. * @param value 事件的value值
  288. */
  289. public static sendEvent(key: string, value: string) {
  290. if (cc.sys.os == cc.sys.OS_ANDROID) {
  291. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEvent", "(Ljava/lang/String;Ljava/lang/String;)V", key, value);
  292. }
  293. else if (cc.sys.os == cc.sys.OS_IOS) {
  294. }
  295. }
  296. /**
  297. * 发送自定义事件 自定义功能 CocosToJava
  298. * @description 发送统计事件
  299. * @param key 事件的key值
  300. * @param value 事件的value值
  301. */
  302. public static sendEventValue(key: string, value: string, value1: number) {
  303. if (cc.sys.os == cc.sys.OS_ANDROID) {
  304. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEventValue", "(Ljava/lang/String;Ljava/lang/String;F)V", key, value, value1);
  305. }
  306. else if (cc.sys.os == cc.sys.OS_IOS) {
  307. }
  308. }
  309. /**
  310. * 设置人物属性
  311. * @param type 0 set 1 add
  312. * @param property 属性名
  313. * @param value 数值
  314. */
  315. public static setTAEventUser(type: number, property: string, value: number) {
  316. if (cc.sys.os == cc.sys.OS_ANDROID) {
  317. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventUser", "(ILjava/lang/String;F)V", type, property, value);
  318. }
  319. else if (cc.sys.os == cc.sys.OS_IOS) {
  320. }
  321. }
  322. /**
  323. * 设置人物属性
  324. * @param type 0 set 1 add
  325. * @param property 属性名
  326. * @param value 数值
  327. */
  328. public static setTAEventUserStr(type: number, property: string, value: string) {
  329. if (cc.sys.os == cc.sys.OS_ANDROID) {
  330. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventUserStr", "(ILjava/lang/String;Ljava/lang/String;)V", type, property, value);
  331. }
  332. else if (cc.sys.os == cc.sys.OS_IOS) {
  333. }
  334. }
  335. /** 设置人物uin到数数
  336. * id uin
  337. */
  338. public static setTAUserID(id: string) {
  339. if (cc.sys.os == cc.sys.OS_ANDROID) {
  340. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAUserID", "(Ljava/lang/String;)V", id);
  341. }
  342. else if (cc.sys.os == cc.sys.OS_IOS) {
  343. }
  344. }
  345. /**
  346. * 微信授权
  347. * @description 微信SDK功能 CocosToJava
  348. */
  349. public static WxAuth() {
  350. if (cc.sys.os == cc.sys.OS_ANDROID) {
  351. this.callStaticMethod(this._JSB_ANDROID_PATH, "WxAuth", "()V");
  352. }
  353. else if (cc.sys.os == cc.sys.OS_IOS) {
  354. this.callStaticMethod("AppController", "WxAuth", "");
  355. }
  356. }
  357. /**
  358. * 获取授权code
  359. * @description 微信SDK功能 JavaToCocos
  360. * @param code 微信code(Java返回)
  361. */
  362. public static onGetWxAccessCode(code) {
  363. // 暂时注释,需要网络通信
  364. gData.wechatData.code = code;
  365. console.log("=== gData.wechatData.code", gData.wechatData.code);
  366. gData.loginData.wxLogin();
  367. }
  368. /**
  369. * 微信打开小游戏
  370. * @description 微信SDK功能 CocosToJava
  371. */
  372. public static OpenMiniGame() {
  373. if (cc.sys.os == cc.sys.OS_ANDROID) {
  374. this.callStaticMethod(this._JSB_ANDROID_PATH, "OpenMiniGame", "()V");
  375. }
  376. else if (cc.sys.os == cc.sys.OS_IOS) {
  377. this.callStaticMethod("AppController", "WxAuth", "");
  378. }
  379. }
  380. /**
  381. * 分享成微信小游戏
  382. * @description 微信SDK功能 CocosToJava
  383. */
  384. public static ShareToMiniGame() {
  385. if (cc.sys.os == cc.sys.OS_ANDROID) {
  386. this.callStaticMethod(this._JSB_ANDROID_PATH, "ShareToMiniGame", "()V");
  387. }
  388. else if (cc.sys.os == cc.sys.OS_IOS) {
  389. this.callStaticMethod("AppController", "WxAuth", "");
  390. }
  391. }
  392. /**
  393. * 分享图片
  394. * @description 微信SDK功能 CocosToJava
  395. */
  396. public static sharePic() {
  397. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  398. return
  399. }
  400. if (cc.sys.os == cc.sys.OS_ANDROID) {
  401. jsb.reflection.callStaticMethod(this._JSB_ANDROID_PATH, "sharePic", "()V");
  402. }
  403. else if (cc.sys.os == cc.sys.OS_IOS) {
  404. jsb.reflection.callStaticMethod("GameConfig", "sharePic:", "");
  405. }
  406. }
  407. /** 打开微信 */
  408. public static openWx() {
  409. if (cc.sys.os == cc.sys.OS_ANDROID) {
  410. this.callStaticMethod(this._JSB_ANDROID_PATH, "openWx", "()V");
  411. }
  412. else if (cc.sys.os == cc.sys.OS_IOS) {
  413. this.callStaticMethod("AppController", "openWx", "");
  414. }
  415. }
  416. /** 下载文件
  417. * @param url 下载地址
  418. * @param fileName 存储文件名称
  419. * @param successCb 成功回调
  420. * @param failCb 失败回调
  421. * @param progressCb 下载进度回调
  422. */
  423. public static downFile2Local(url, fileName, successCb, failCb, progressCb): any {
  424. var fullPath = jsb.fileUtils.getWritablePath() + fileName
  425. if (jsb) {
  426. var downloader = new jsb.Downloader();
  427. downloader.setOnFileTaskSuccess(function () {
  428. console.log("DownLoadM setOnFileTaskSuccess", "")
  429. if (successCb) {
  430. console.log("DownLoadM result... ", jsb.fileUtils.isFileExist(fullPath))
  431. console.log("DownLoadM setOnFileTaskSuccess fullPath...", fullPath)
  432. successCb(fullPath);
  433. }
  434. });
  435. downloader.setOnTaskError(function (task: any, errorCode: number, errorCodeInternal: number, errorStr: string) {
  436. console.log("DownLoadM setOnTaskError", errorCode + " ; " + errorCodeInternal + " ; " + errorStr)
  437. if (failCb) {
  438. failCb(fullPath);
  439. }
  440. });
  441. downloader.setOnTaskProgress(function (task: any, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) {
  442. console.log("DownLoadM", bytesReceived + " ; " + totalBytesReceived + " ; " + totalBytesExpected)
  443. if (progressCb) {
  444. progressCb(bytesReceived, totalBytesReceived, totalBytesExpected)
  445. }
  446. });
  447. downloader.createDownloadFileTask(url, fullPath);//创建下载任务
  448. return downloader
  449. }
  450. }
  451. /** 上传开屏广告星云埋点 */
  452. public static updateSplashAdLog(info) {
  453. let arr = info.split('#')
  454. // console.log('updateSplashAdLog placementId ', arr[0])
  455. // console.log('updateSplashAdLog callbackInfo ', arr[1])
  456. let data = JSON.parse(arr[1])
  457. gData.adData.updateADLog(6, arr[0], data);
  458. let times = mk.storage.getStorage('ad_openEcpm');
  459. if (!times) {
  460. times = 1;
  461. }
  462. else {
  463. times++;
  464. }
  465. mk.storage.setStorage('ad_openEcpm', times);
  466. if (times <= 300) {
  467. if (times <= 50) {
  468. mk.data.sendDataEventValue(DataEventId.ad_openEcpm, `ad_video_ecpm${times}`, data.adsource_price);
  469. }
  470. else if (times % 10 == 0) {
  471. mk.data.sendDataEventValue(DataEventId.ad_openEcpm, `ad_video_ecpm${times}`, data.adsource_price);
  472. }
  473. }
  474. }
  475. public static clickSplash() {
  476. mk.data.sendDataEvent(DataEventId.Ad_click, 'splash_click');
  477. }
  478. /** 获取阿里风控设备token */
  479. public static getAliRiskDeviceToken() {
  480. let token = '';
  481. if (cc.sys.os == cc.sys.OS_ANDROID) {
  482. token = this.callStaticMethod(this._JSB_ANDROID_PATH, "getAliDeviceToken", "()Ljava/lang/String;");
  483. }
  484. else if (cc.sys.os == cc.sys.OS_IOS) {
  485. token = this.callStaticMethod("AppController", "openWx", "");
  486. }
  487. return token;
  488. }
  489. public static closePanelFromHome() {
  490. if (this.callBack) {
  491. this.callBack();
  492. }
  493. }
  494. public static systemExit() {
  495. if (cc.sys.os == cc.sys.OS_ANDROID) {
  496. this.callStaticMethod(this._JSB_ANDROID_PATH, "systemExit", "()V");
  497. }
  498. }
  499. public static changeNetState(state: string) {
  500. if (state == '1') {
  501. mk.ui.closePanel('NetDisConnect');
  502. }
  503. else if (state == '0') {
  504. mk.ui.openPanel('module/netDisconnect/NetDisConnect');
  505. }
  506. }
  507. //分享链接
  508. public static shareInviteUrl(url: string, title: string, describe: string, imgUrl: string) {
  509. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  510. return
  511. }
  512. if (cc.sys.os == cc.sys.OS_ANDROID) {
  513. jsb.reflection.callStaticMethod(this._JSB_ANDROID_PATH, "shareInviteUrl",
  514. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", url, title, describe, imgUrl);
  515. }
  516. else if (cc.sys.os == cc.sys.OS_IOS) {
  517. //to do
  518. // jsb.reflection.callStaticMethod("GameConfig", "sharePic:", "");
  519. }
  520. }
  521. }
  522. window['JsbMgr'] = JsbSystem