JsbSystem.ts 15 KB

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