JsbSystem.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. }
  123. /**
  124. * 获取广告渠道和相关id
  125. * @description 自定义功能 CocosToJava
  126. * @returns str
  127. */
  128. public static getAdvertisingChannel(): string {
  129. let str = "";
  130. if (cc.sys.os == cc.sys.OS_ANDROID) {
  131. str = this.callStaticMethod(this._JSB_ANDROID_PATH, "getAdvertisingChannel", "()Ljava/lang/String;");
  132. }
  133. else if (cc.sys.os == cc.sys.OS_IOS) {
  134. str = this.callStaticMethod("CsjAdReward", "AdvertisingChannel", "");
  135. }
  136. return str;
  137. }
  138. /** 显示开屏 */
  139. static showSplash() {
  140. // mk.data.sendDataEvent('slashAd', '开屏')
  141. if (cc.sys.os == cc.sys.OS_ANDROID) {
  142. this.callStaticMethod(this._JSB_ANDROID_PATH, "showSplash", "()V");
  143. }
  144. else if (cc.sys.os == cc.sys.OS_IOS) {
  145. jsb.reflection.callStaticMethod("GameConfig", "showSplash:", "");
  146. }
  147. }
  148. /**
  149. * 复制到剪切板
  150. * @description Andorid功能 CocosToJava
  151. * @param str 剪切板字符串内容
  152. */
  153. public static setClipboard(str: string) {
  154. if (cc.sys.os == cc.sys.OS_ANDROID) {
  155. this.callStaticMethod(this._JSB_ANDROID_PATH, "setClipboard", "(Ljava/lang/String;)V", str);
  156. }
  157. else if (cc.sys.os == cc.sys.OS_IOS) {
  158. }
  159. else if (cc.sys.isBrowser) {
  160. var input = str;
  161. const el: any = document.createElement('textarea');
  162. el.value = input;
  163. el.setAttribute('readonly', '');
  164. el.style.contain = 'strict';
  165. el.style.position = 'absolute';
  166. el.style.left = '-9999px';
  167. el.style.fontSize = '12pt';
  168. const selection = getSelection();
  169. var originalRange: any = false;
  170. if (selection.rangeCount > 0) {
  171. originalRange = selection.getRangeAt(0);
  172. }
  173. document.body.appendChild(el);
  174. el.select();
  175. el.selectionStart = 0;
  176. el.selectionEnd = input.length;
  177. var success = false;
  178. try {
  179. success = document.execCommand('copy');
  180. } catch (err) { }
  181. document.body.removeChild(el);
  182. if (originalRange) {
  183. selection.removeAllRanges();
  184. selection.addRange(originalRange);
  185. }
  186. return success;
  187. }
  188. }
  189. /**
  190. * 请求用户授权
  191. * @description Andorid功能 CocosToJava
  192. */
  193. public static requestPermission() {
  194. if (cc.sys.os == cc.sys.OS_ANDROID) {
  195. this.callStaticMethod(this._JSB_ANDROID_PATH, "requestPermission", "()V");
  196. }
  197. else if (cc.sys.os == cc.sys.OS_IOS) {
  198. this.callStaticMethod("AppController", "requestPermission", "");
  199. }
  200. }
  201. /**
  202. * 获取安卓的版本号
  203. * @description Andorid功能 CocosToJava
  204. */
  205. public static getAppVersion() {
  206. if (cc.sys.os == cc.sys.OS_ANDROID) {
  207. this.callStaticMethod(this._JSB_ANDROID_PATH, "getAppVersion", "()V");
  208. }
  209. else if (cc.sys.os == cc.sys.OS_IOS) {
  210. this.callStaticMethod("AppController", "getAppVersion", "");
  211. }
  212. }
  213. /**
  214. * 设置安卓得版本号
  215. * @description Andorid功能 JavaToCocos
  216. * @param appVersion app版本号
  217. */
  218. public static setAppVersion(appVersion: string) {
  219. if (cc.sys.os == cc.sys.OS_ANDROID) {
  220. // GameConst.appVersion = appVersion;
  221. }
  222. else if (cc.sys.os == cc.sys.OS_IOS) {
  223. }
  224. }
  225. /**
  226. * 安装apk
  227. * @description Andorid功能 CocosToJava
  228. * @param apkName apkName
  229. */
  230. public static installApk(apkName: string) {
  231. let fullPath = this.getWritablePath() + apkName + ".apk";
  232. if (cc.sys.os == cc.sys.OS_ANDROID) {
  233. this.callStaticMethod(this._JSB_ANDROID_PATH, "installApk", "(Ljava/lang/String;)V", fullPath);
  234. }
  235. else if (cc.sys.os == cc.sys.OS_IOS) {
  236. this.callStaticMethod("GameConfig", "installApk:", "");
  237. }
  238. }
  239. /**
  240. * 是否可以启动游戏
  241. * @param packageName 包名
  242. * @param start 如存在是否启动
  243. * @returns ifCan
  244. */
  245. public static ifCanStartGame(packageName: string, start: boolean): boolean {
  246. let isCan = false;
  247. if (cc.sys.os == cc.sys.OS_ANDROID) {
  248. isCan = this.callStaticMethod(this._JSB_ANDROID_PATH, "isStartGame", "(Ljava/lang/String;Z)Z", packageName, start);
  249. return isCan;
  250. }
  251. else if (cc.sys.os == cc.sys.OS_IOS) {
  252. isCan = this.callStaticMethod("GameConfig", "installApk:", "");
  253. }
  254. return isCan;
  255. }
  256. /**
  257. * 数数事件注册
  258. * @description 数数SDK功能 已经自动放到AS授权后了 前端无需调用
  259. * @returns
  260. */
  261. static setTAEventRegister() {
  262. if (cc.sys.os == cc.sys.OS_ANDROID) {
  263. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventRegister", "()V");
  264. }
  265. else if (cc.sys.os == cc.sys.OS_IOS) {
  266. }
  267. }
  268. /**
  269. * 发送自定义事件 自定义功能 CocosToJava
  270. * @description 发送统计事件
  271. * @param key 事件的key值
  272. * @param value 事件的value值
  273. */
  274. public static sendEvent(key: string, value: string) {
  275. if (cc.sys.os == cc.sys.OS_ANDROID) {
  276. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEvent", "(Ljava/lang/String;Ljava/lang/String;)V", key, value);
  277. }
  278. else if (cc.sys.os == cc.sys.OS_IOS) {
  279. }
  280. }
  281. /**
  282. * 微信授权
  283. * @description 微信SDK功能 CocosToJava
  284. */
  285. public static WxAuth() {
  286. if (cc.sys.os == cc.sys.OS_ANDROID) {
  287. this.callStaticMethod(this._JSB_ANDROID_PATH, "WxAuth", "()V");
  288. }
  289. else if (cc.sys.os == cc.sys.OS_IOS) {
  290. this.callStaticMethod("AppController", "WxAuth", "");
  291. }
  292. }
  293. /**
  294. * 获取授权code
  295. * @description 微信SDK功能 JavaToCocos
  296. * @param code 微信code(Java返回)
  297. */
  298. public static onGetWxAccessCode(code) {
  299. // 暂时注释,需要网络通信
  300. gData.wechatData.code = code;
  301. console.log("=== gData.wechatData.code", gData.wechatData.code);
  302. gData.loginData.wxLogin();
  303. }
  304. /**
  305. * 微信打开小游戏
  306. * @description 微信SDK功能 CocosToJava
  307. */
  308. public static OpenMiniGame() {
  309. if (cc.sys.os == cc.sys.OS_ANDROID) {
  310. this.callStaticMethod(this._JSB_ANDROID_PATH, "OpenMiniGame", "()V");
  311. }
  312. else if (cc.sys.os == cc.sys.OS_IOS) {
  313. this.callStaticMethod("AppController", "WxAuth", "");
  314. }
  315. }
  316. /**
  317. * 分享成微信小游戏
  318. * @description 微信SDK功能 CocosToJava
  319. */
  320. public static ShareToMiniGame() {
  321. if (cc.sys.os == cc.sys.OS_ANDROID) {
  322. this.callStaticMethod(this._JSB_ANDROID_PATH, "ShareToMiniGame", "()V");
  323. }
  324. else if (cc.sys.os == cc.sys.OS_IOS) {
  325. this.callStaticMethod("AppController", "WxAuth", "");
  326. }
  327. }
  328. /**
  329. * 分享图片
  330. * @description 微信SDK功能 CocosToJava
  331. */
  332. public static sharePic() {
  333. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  334. return
  335. }
  336. if (cc.sys.os == cc.sys.OS_ANDROID) {
  337. jsb.reflection.callStaticMethod(this._JSB_ANDROID_PATH, "sharePic", "()V");
  338. }
  339. else if (cc.sys.os == cc.sys.OS_IOS) {
  340. jsb.reflection.callStaticMethod("GameConfig", "sharePic:", "");
  341. }
  342. }
  343. /** 打开微信 */
  344. public static openWx() {
  345. if (cc.sys.os == cc.sys.OS_ANDROID) {
  346. this.callStaticMethod(this._JSB_ANDROID_PATH, "openWx", "()V");
  347. }
  348. else if (cc.sys.os == cc.sys.OS_IOS) {
  349. this.callStaticMethod("AppController", "openWx", "");
  350. }
  351. }
  352. /** 下载文件
  353. * @param url 下载地址
  354. * @param fileName 存储文件名称
  355. * @param successCb 成功回调
  356. * @param failCb 失败回调
  357. * @param progressCb 下载进度回调
  358. */
  359. public static downFile2Local(url, fileName, successCb, failCb, progressCb): any {
  360. var fullPath = jsb.fileUtils.getWritablePath() + fileName
  361. if (jsb) {
  362. var downloader = new jsb.Downloader();
  363. downloader.setOnFileTaskSuccess(function () {
  364. console.log("DownLoadM setOnFileTaskSuccess", "")
  365. if (successCb) {
  366. console.log("DownLoadM result... ", jsb.fileUtils.isFileExist(fullPath))
  367. console.log("DownLoadM setOnFileTaskSuccess fullPath...", fullPath)
  368. successCb(fullPath);
  369. }
  370. });
  371. downloader.setOnTaskError(function (task: any, errorCode: number, errorCodeInternal: number, errorStr: string) {
  372. console.log("DownLoadM setOnTaskError", errorCode + " ; " + errorCodeInternal + " ; " + errorStr)
  373. if (failCb) {
  374. failCb(fullPath);
  375. }
  376. });
  377. downloader.setOnTaskProgress(function (task: any, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) {
  378. console.log("DownLoadM", bytesReceived + " ; " + totalBytesReceived + " ; " + totalBytesExpected)
  379. if (progressCb) {
  380. progressCb(bytesReceived, totalBytesReceived, totalBytesExpected)
  381. }
  382. });
  383. downloader.createDownloadFileTask(url, fullPath);//创建下载任务
  384. return downloader
  385. }
  386. }
  387. }
  388. window['JsbMgr'] = JsbSystem