JsbSystem.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. jsb.reflection.callStaticMethod(path, methodName, methodSignature, ...parameters);
  41. }
  42. /**
  43. * Java调用返回测试
  44. * @description 自定义功能 JavaToCocos
  45. */
  46. public static backTest() {
  47. }
  48. /**
  49. * 更新设备信息
  50. * @description 自定义功能 JavaToCocos
  51. * @param info info(Java返回)
  52. */
  53. public static updateDeviceInfo(info) {
  54. mk.console.log("updateDeviceInfo" + cc.sys.os, info)
  55. if (cc.sys.os == cc.sys.OS_ANDROID) {
  56. gData.appData.updateDeviceInfo(info);
  57. }
  58. else if (cc.sys.os == cc.sys.OS_IOS) {
  59. }
  60. }
  61. /**
  62. * 获取设备信息
  63. * @description 自定义功能 CocosToJava
  64. */
  65. public static getDeviceInfo() {
  66. mk.console.log("getDeviceInfo", cc.sys.os)
  67. if (cc.sys.os == cc.sys.OS_ANDROID) {
  68. this.callStaticMethod(this._JSB_ANDROID_PATH, "getDeviceInfo", "()V");
  69. }
  70. else if (cc.sys.os == cc.sys.OS_IOS) {
  71. this.callStaticMethod(this._JSB_IOS_PATH, "getDeviceInfo", "");
  72. }
  73. else {
  74. gData.appData.getDeviceInfoCompelete = true;
  75. }
  76. }
  77. /**
  78. * 获取包名
  79. * @description 自定义功能 CocosToJava
  80. * @returns packName 包名
  81. */
  82. public static getPackageName() {
  83. let packName = '';
  84. if (cc.sys.os == cc.sys.OS_ANDROID) {
  85. packName = this.callStaticMethod(this._JSB_ANDROID_PATH, "getPackageName", "()Ljava/lang/String;");
  86. }
  87. else if (cc.sys.os == cc.sys.OS_IOS) {
  88. packName = this.callStaticMethod("CsjAdReward", "getPackageName", "");
  89. }
  90. return packName;
  91. }
  92. /**
  93. * 获取微信appid
  94. * @description 自定义功能 CocosToJava
  95. * @returns appid
  96. */
  97. public static getWxAppId() {
  98. let appid = "";
  99. if (cc.sys.os == cc.sys.OS_ANDROID) {
  100. appid = this.callStaticMethod(this._JSB_ANDROID_PATH, "getWXAppId", "()Ljava/lang/String;");
  101. }
  102. else if (cc.sys.os == cc.sys.OS_IOS) {
  103. appid = this.callStaticMethod("CsjAdReward", "getWXAppId", "",);
  104. }
  105. return appid;
  106. }
  107. /**
  108. * 获取广告渠道和相关id
  109. * @description 自定义功能 CocosToJava
  110. * @returns str
  111. */
  112. public static getAdvertisingChannel(): string {
  113. let str = "";
  114. if (cc.sys.os == cc.sys.OS_ANDROID) {
  115. str = this.callStaticMethod(this._JSB_ANDROID_PATH, "getAdvertisingChannel", "()Ljava/lang/String;");
  116. }
  117. else if (cc.sys.os == cc.sys.OS_IOS) {
  118. str = this.callStaticMethod("CsjAdReward", "AdvertisingChannel", "");
  119. }
  120. return str;
  121. }
  122. /**
  123. * 复制到剪切板
  124. * @description Andorid功能 CocosToJava
  125. * @param str 剪切板字符串内容
  126. */
  127. public static setClipboard(str: string) {
  128. if (cc.sys.os == cc.sys.OS_ANDROID) {
  129. this.callStaticMethod(this._JSB_ANDROID_PATH, "setClipboard", "(Ljava/lang/String;)V", str);
  130. }
  131. else if (cc.sys.os == cc.sys.OS_IOS) {
  132. }
  133. else if (cc.sys.isBrowser) {
  134. var input = str;
  135. const el: any = document.createElement('textarea');
  136. el.value = input;
  137. el.setAttribute('readonly', '');
  138. el.style.contain = 'strict';
  139. el.style.position = 'absolute';
  140. el.style.left = '-9999px';
  141. el.style.fontSize = '12pt';
  142. const selection = getSelection();
  143. var originalRange: any = false;
  144. if (selection.rangeCount > 0) {
  145. originalRange = selection.getRangeAt(0);
  146. }
  147. document.body.appendChild(el);
  148. el.select();
  149. el.selectionStart = 0;
  150. el.selectionEnd = input.length;
  151. var success = false;
  152. try {
  153. success = document.execCommand('copy');
  154. } catch (err) { }
  155. document.body.removeChild(el);
  156. if (originalRange) {
  157. selection.removeAllRanges();
  158. selection.addRange(originalRange);
  159. }
  160. return success;
  161. }
  162. }
  163. /**
  164. * 请求用户授权
  165. * @description Andorid功能 CocosToJava
  166. */
  167. public static requestPermission() {
  168. if (cc.sys.os == cc.sys.OS_ANDROID) {
  169. this.callStaticMethod(this._JSB_ANDROID_PATH, "requestPermission", "()V");
  170. }
  171. else if (cc.sys.os == cc.sys.OS_IOS) {
  172. this.callStaticMethod("AppController", "requestPermission", "");
  173. }
  174. }
  175. /**
  176. * 获取安卓的版本号
  177. * @description Andorid功能 CocosToJava
  178. */
  179. public static getAppVersion() {
  180. if (cc.sys.os == cc.sys.OS_ANDROID) {
  181. this.callStaticMethod(this._JSB_ANDROID_PATH, "getAppVersion", "()V");
  182. }
  183. else if (cc.sys.os == cc.sys.OS_IOS) {
  184. this.callStaticMethod("AppController", "getAppVersion", "");
  185. }
  186. }
  187. /**
  188. * 设置安卓得版本号
  189. * @description Andorid功能 JavaToCocos
  190. * @param appVersion app版本号
  191. */
  192. public static setAppVersion(appVersion: string) {
  193. if (cc.sys.os == cc.sys.OS_ANDROID) {
  194. // GameConst.appVersion = appVersion;
  195. }
  196. else if (cc.sys.os == cc.sys.OS_IOS) {
  197. }
  198. }
  199. /**
  200. * 安装apk
  201. * @description Andorid功能 CocosToJava
  202. * @param apkName apkName
  203. */
  204. public static installApk(apkName: string) {
  205. let fullPath = this.getWritablePath() + apkName;
  206. if (cc.sys.os == cc.sys.OS_ANDROID) {
  207. this.callStaticMethod(this._JSB_ANDROID_PATH, "installApk", "(Ljava/lang/String;)V", fullPath);
  208. }
  209. else if (cc.sys.os == cc.sys.OS_IOS) {
  210. this.callStaticMethod("GameConfig", "installApk:", "");
  211. }
  212. }
  213. /**
  214. * 是否可以启动游戏
  215. * @param packageName 包名
  216. * @param start 如存在是否启动
  217. * @returns ifCan
  218. */
  219. static ifCanStartGame(packageName: string, start: boolean): boolean {
  220. let isCan = false;
  221. if (cc.sys.os == cc.sys.OS_ANDROID) {
  222. isCan = this.callStaticMethod(this._JSB_ANDROID_PATH, "isStartGame", "(Ljava/lang/String;Z)Z", packageName, start);
  223. }
  224. else if (cc.sys.os == cc.sys.OS_IOS) {
  225. isCan = this.callStaticMethod("GameConfig", "installApk:", "");
  226. }
  227. return isCan;
  228. }
  229. /**
  230. * 数数事件注册
  231. * @description 数数SDK功能 已经自动放到AS授权后了 前端无需调用
  232. * @returns
  233. */
  234. static setTAEventRegister() {
  235. if (cc.sys.os == cc.sys.OS_ANDROID) {
  236. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventRegister", "()V");
  237. }
  238. else if (cc.sys.os == cc.sys.OS_IOS) {
  239. }
  240. }
  241. /**
  242. * 发送自定义事件 自定义功能 CocosToJava
  243. * @description 发送统计事件
  244. * @param key 事件的key值
  245. * @param value 事件的value值
  246. */
  247. public static sendEvent(key: string, value: string) {
  248. if (cc.sys.os == cc.sys.OS_ANDROID) {
  249. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEvent", "(Ljava/lang/String;Ljava/lang/String;)V", key, value);
  250. }
  251. else if (cc.sys.os == cc.sys.OS_IOS) {
  252. }
  253. }
  254. /**
  255. * 微信授权
  256. * @description 微信SDK功能 CocosToJava
  257. */
  258. public static WxAuth() {
  259. if (cc.sys.os == cc.sys.OS_ANDROID) {
  260. this.callStaticMethod(this._JSB_ANDROID_PATH, "WxAuth", "()V");
  261. }
  262. else if (cc.sys.os == cc.sys.OS_IOS) {
  263. this.callStaticMethod("AppController", "WxAuth", "");
  264. }
  265. }
  266. /**
  267. * 获取授权code
  268. * @description 微信SDK功能 JavaToCocos
  269. * @param code 微信code(Java返回)
  270. */
  271. public static onGetAccessCode(code) {
  272. // 暂时注释,需要网络通信
  273. // GameConst.wxCode = code;
  274. // HttpMgr.Inst.wxlogin(true);
  275. }
  276. /**
  277. * 微信打开小游戏
  278. * @description 微信SDK功能 CocosToJava
  279. */
  280. public static OpenMiniGame() {
  281. if (cc.sys.os == cc.sys.OS_ANDROID) {
  282. this.callStaticMethod(this._JSB_ANDROID_PATH, "OpenMiniGame", "()V");
  283. }
  284. else if (cc.sys.os == cc.sys.OS_IOS) {
  285. this.callStaticMethod("AppController", "WxAuth", "");
  286. }
  287. }
  288. /**
  289. * 分享成微信小游戏
  290. * @description 微信SDK功能 CocosToJava
  291. */
  292. public static ShareToMiniGame() {
  293. if (cc.sys.os == cc.sys.OS_ANDROID) {
  294. this.callStaticMethod(this._JSB_ANDROID_PATH, "ShareToMiniGame", "()V");
  295. }
  296. else if (cc.sys.os == cc.sys.OS_IOS) {
  297. this.callStaticMethod("AppController", "WxAuth", "");
  298. }
  299. }
  300. /** 下载文件
  301. * @param url 下载地址
  302. * @param fileName 存储文件名称
  303. * @param successCb 成功回调
  304. * @param failCb 失败回调
  305. * @param progressCb 下载进度回调
  306. */
  307. public static downFile2Local(url, fileName, successCb, failCb, progressCb): any {
  308. var fullPath = jsb.fileUtils.getWritablePath() + fileName
  309. if (jsb) {
  310. var downloader = new jsb.Downloader();
  311. downloader.setOnFileTaskSuccess(function () {
  312. console.log("DownLoadM setOnFileTaskSuccess", "")
  313. if (successCb) {
  314. console.log("DownLoadM result... ", jsb.fileUtils.isFileExist(fullPath))
  315. console.log("DownLoadM setOnFileTaskSuccess fullPath...", fullPath)
  316. successCb(fullPath);
  317. }
  318. });
  319. downloader.setOnTaskError(function (task: any, errorCode: number, errorCodeInternal: number, errorStr: string) {
  320. console.log("DownLoadM setOnTaskError", errorCode + " ; " + errorCodeInternal + " ; " + errorStr)
  321. if (failCb) {
  322. failCb(fullPath);
  323. }
  324. });
  325. downloader.setOnTaskProgress(function (task: any, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) {
  326. console.log("DownLoadM", bytesReceived + " ; " + totalBytesReceived + " ; " + totalBytesExpected)
  327. if (progressCb) {
  328. progressCb(bytesReceived, totalBytesReceived, totalBytesExpected)
  329. }
  330. });
  331. downloader.createDownloadFileTask(url, fullPath);//创建下载任务
  332. return downloader
  333. }
  334. }
  335. }
  336. window['JsbMgr'] = JsbSystem