JsbSystem.ts 13 KB

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