JsbSystem.ts 13 KB

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