JsbSystem.ts 12 KB

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