JsbSystem.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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("===[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;
  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. 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. }
  227. else if (cc.sys.os == cc.sys.OS_IOS) {
  228. isCan = this.callStaticMethod("GameConfig", "installApk:", "");
  229. }
  230. return isCan;
  231. }
  232. /**
  233. * 数数事件注册
  234. * @description 数数SDK功能 已经自动放到AS授权后了 前端无需调用
  235. * @returns
  236. */
  237. static setTAEventRegister() {
  238. if (cc.sys.os == cc.sys.OS_ANDROID) {
  239. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventRegister", "()V");
  240. }
  241. else if (cc.sys.os == cc.sys.OS_IOS) {
  242. }
  243. }
  244. /**
  245. * 发送自定义事件 自定义功能 CocosToJava
  246. * @description 发送统计事件
  247. * @param key 事件的key值
  248. * @param value 事件的value值
  249. */
  250. public static sendEvent(key: string, value: string) {
  251. if (cc.sys.os == cc.sys.OS_ANDROID) {
  252. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEvent", "(Ljava/lang/String;Ljava/lang/String;)V", key, value);
  253. }
  254. else if (cc.sys.os == cc.sys.OS_IOS) {
  255. }
  256. }
  257. /**
  258. * 微信授权
  259. * @description 微信SDK功能 CocosToJava
  260. */
  261. public static WxAuth() {
  262. if (cc.sys.os == cc.sys.OS_ANDROID) {
  263. this.callStaticMethod(this._JSB_ANDROID_PATH, "WxAuth", "()V");
  264. }
  265. else if (cc.sys.os == cc.sys.OS_IOS) {
  266. this.callStaticMethod("AppController", "WxAuth", "");
  267. }
  268. }
  269. /**
  270. * 获取授权code
  271. * @description 微信SDK功能 JavaToCocos
  272. * @param code 微信code(Java返回)
  273. */
  274. public static onGetWxAccessCode(code) {
  275. // 暂时注释,需要网络通信
  276. gData.wechatData.code = code;
  277. console.log("=== gData.wechatData.code", gData.wechatData.code);
  278. gData.loginData.wxLogin();
  279. }
  280. /**
  281. * 微信打开小游戏
  282. * @description 微信SDK功能 CocosToJava
  283. */
  284. public static OpenMiniGame() {
  285. if (cc.sys.os == cc.sys.OS_ANDROID) {
  286. this.callStaticMethod(this._JSB_ANDROID_PATH, "OpenMiniGame", "()V");
  287. }
  288. else if (cc.sys.os == cc.sys.OS_IOS) {
  289. this.callStaticMethod("AppController", "WxAuth", "");
  290. }
  291. }
  292. /**
  293. * 分享成微信小游戏
  294. * @description 微信SDK功能 CocosToJava
  295. */
  296. public static ShareToMiniGame() {
  297. if (cc.sys.os == cc.sys.OS_ANDROID) {
  298. this.callStaticMethod(this._JSB_ANDROID_PATH, "ShareToMiniGame", "()V");
  299. }
  300. else if (cc.sys.os == cc.sys.OS_IOS) {
  301. this.callStaticMethod("AppController", "WxAuth", "");
  302. }
  303. }
  304. /**
  305. * 分享图片
  306. * @description 微信SDK功能 CocosToJava
  307. */
  308. public static sharePic() {
  309. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  310. return
  311. }
  312. if (cc.sys.os == cc.sys.OS_ANDROID) {
  313. jsb.reflection.callStaticMethod(this._JSB_ANDROID_PATH, "sharePic", "()V");
  314. }
  315. else if (cc.sys.os == cc.sys.OS_IOS) {
  316. jsb.reflection.callStaticMethod("GameConfig", "sharePic:", "");
  317. }
  318. }
  319. /** 下载文件
  320. * @param url 下载地址
  321. * @param fileName 存储文件名称
  322. * @param successCb 成功回调
  323. * @param failCb 失败回调
  324. * @param progressCb 下载进度回调
  325. */
  326. public static downFile2Local(url, fileName, successCb, failCb, progressCb): any {
  327. var fullPath = jsb.fileUtils.getWritablePath() + fileName
  328. if (jsb) {
  329. var downloader = new jsb.Downloader();
  330. downloader.setOnFileTaskSuccess(function () {
  331. console.log("DownLoadM setOnFileTaskSuccess", "")
  332. if (successCb) {
  333. console.log("DownLoadM result... ", jsb.fileUtils.isFileExist(fullPath))
  334. console.log("DownLoadM setOnFileTaskSuccess fullPath...", fullPath)
  335. successCb(fullPath);
  336. }
  337. });
  338. downloader.setOnTaskError(function (task: any, errorCode: number, errorCodeInternal: number, errorStr: string) {
  339. console.log("DownLoadM setOnTaskError", errorCode + " ; " + errorCodeInternal + " ; " + errorStr)
  340. if (failCb) {
  341. failCb(fullPath);
  342. }
  343. });
  344. downloader.setOnTaskProgress(function (task: any, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) {
  345. console.log("DownLoadM", bytesReceived + " ; " + totalBytesReceived + " ; " + totalBytesExpected)
  346. if (progressCb) {
  347. progressCb(bytesReceived, totalBytesReceived, totalBytesExpected)
  348. }
  349. });
  350. downloader.createDownloadFileTask(url, fullPath);//创建下载任务
  351. return downloader
  352. }
  353. }
  354. }
  355. window['JsbMgr'] = JsbSystem