JsbSystem.ts 13 KB

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