JsbSystem.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. //GameConst.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. }
  129. /**
  130. * 请求用户授权
  131. * @description Andorid功能 CocosToJava
  132. */
  133. public static requestPermission() {
  134. if (cc.sys.os == cc.sys.OS_ANDROID) {
  135. this.callStaticMethod(this._JSB_ANDROID_PATH, "requestPermission", "()V");
  136. }
  137. else if (cc.sys.os == cc.sys.OS_IOS) {
  138. this.callStaticMethod("AppController", "requestPermission", "");
  139. }
  140. }
  141. /**
  142. * 获取安卓的版本号
  143. * @description Andorid功能 CocosToJava
  144. */
  145. public static getAppVersion() {
  146. if (cc.sys.os == cc.sys.OS_ANDROID) {
  147. this.callStaticMethod(this._JSB_ANDROID_PATH, "getAppVersion", "()V");
  148. }
  149. else if (cc.sys.os == cc.sys.OS_IOS) {
  150. this.callStaticMethod("AppController", "getAppVersion", "");
  151. }
  152. }
  153. /**
  154. * 设置安卓得版本号
  155. * @description Andorid功能 JavaToCocos
  156. * @param appVersion app版本号
  157. */
  158. public static setAppVersion(appVersion: string) {
  159. if (cc.sys.os == cc.sys.OS_ANDROID) {
  160. // GameConst.appVersion = appVersion;
  161. }
  162. else if (cc.sys.os == cc.sys.OS_IOS) {
  163. }
  164. }
  165. /**
  166. * 安装apk
  167. * @description Andorid功能 CocosToJava
  168. * @param apkName apkName
  169. */
  170. public static installApk(apkName: string) {
  171. let fullPath = this.getWritablePath() + apkName;
  172. if (cc.sys.os == cc.sys.OS_ANDROID) {
  173. this.callStaticMethod(this._JSB_ANDROID_PATH, "installApk", "(Ljava/lang/String;)V", fullPath);
  174. }
  175. else if (cc.sys.os == cc.sys.OS_IOS) {
  176. this.callStaticMethod("GameConfig", "installApk:", "");
  177. }
  178. }
  179. /**
  180. * 是否可以启动游戏
  181. * @param packageName 包名
  182. * @param start 如存在是否启动
  183. * @returns ifCan
  184. */
  185. static ifCanStartGame(packageName: string, start: boolean): boolean {
  186. let isCan = false;
  187. if (cc.sys.os == cc.sys.OS_ANDROID) {
  188. isCan = this.callStaticMethod(this._JSB_ANDROID_PATH, "isStartGame", "(Ljava/lang/String;Z)Z", packageName, start);
  189. }
  190. else if (cc.sys.os == cc.sys.OS_IOS) {
  191. isCan = this.callStaticMethod("GameConfig", "installApk:", "");
  192. }
  193. return isCan;
  194. }
  195. /**
  196. * 数数事件注册
  197. * @description 数数SDK功能 已经自动放到AS授权后了 前端无需调用
  198. * @returns
  199. */
  200. static setTAEventRegister() {
  201. if (cc.sys.os == cc.sys.OS_ANDROID) {
  202. this.callStaticMethod(this._JSB_ANDROID_PATH, "setTAEventRegister", "()V");
  203. }
  204. else if (cc.sys.os == cc.sys.OS_IOS) {
  205. }
  206. }
  207. /**
  208. * 发送自定义事件 自定义功能 CocosToJava
  209. * @description 发送统计事件
  210. * @param key 事件的key值
  211. * @param value 事件的value值
  212. */
  213. public static sendEvent(key: string, value: string) {
  214. if (cc.sys.os == cc.sys.OS_ANDROID) {
  215. this.callStaticMethod(this._JSB_ANDROID_PATH, "sendEvent", "(Ljava/lang/String;Ljava/lang/String;)V", key, value);
  216. }
  217. else if (cc.sys.os == cc.sys.OS_IOS) {
  218. }
  219. }
  220. /**
  221. * 微信授权
  222. * @description 微信SDK功能 CocosToJava
  223. */
  224. public static WxAuth() {
  225. if (cc.sys.os == cc.sys.OS_ANDROID) {
  226. this.callStaticMethod(this._JSB_ANDROID_PATH, "WxAuth", "()V");
  227. }
  228. else if (cc.sys.os == cc.sys.OS_IOS) {
  229. this.callStaticMethod("AppController", "WxAuth", "");
  230. }
  231. }
  232. /**
  233. * 获取授权code
  234. * @description 微信SDK功能 JavaToCocos
  235. * @param code 微信code(Java返回)
  236. */
  237. public static onGetAccessCode(code) {
  238. // 暂时注释,需要网络通信
  239. // GameConst.wxCode = code;
  240. // HttpMgr.Inst.wxlogin(true);
  241. }
  242. /**
  243. * 微信打开小游戏
  244. * @description 微信SDK功能 CocosToJava
  245. */
  246. public static OpenMiniGame() {
  247. if (cc.sys.os == cc.sys.OS_ANDROID) {
  248. this.callStaticMethod(this._JSB_ANDROID_PATH, "OpenMiniGame", "()V");
  249. }
  250. else if (cc.sys.os == cc.sys.OS_IOS) {
  251. this.callStaticMethod("AppController", "WxAuth", "");
  252. }
  253. }
  254. /**
  255. * 分享成微信小游戏
  256. * @description 微信SDK功能 CocosToJava
  257. */
  258. public static ShareToMiniGame() {
  259. if (cc.sys.os == cc.sys.OS_ANDROID) {
  260. this.callStaticMethod(this._JSB_ANDROID_PATH, "ShareToMiniGame", "()V");
  261. }
  262. else if (cc.sys.os == cc.sys.OS_IOS) {
  263. this.callStaticMethod("AppController", "WxAuth", "");
  264. }
  265. }
  266. /** 下载文件
  267. * @param url 下载地址
  268. * @param fileName 存储文件名称
  269. * @param successCb 成功回调
  270. * @param failCb 失败回调
  271. * @param progressCb 下载进度回调
  272. */
  273. public static downFile2Local(url, fileName, successCb, failCb, progressCb): any {
  274. var fullPath = jsb.fileUtils.getWritablePath() + fileName
  275. if (jsb) {
  276. var downloader = new jsb.Downloader();
  277. downloader.setOnFileTaskSuccess(function () {
  278. console.log("DownLoadM setOnFileTaskSuccess", "")
  279. if (successCb) {
  280. console.log("DownLoadM result... ", jsb.fileUtils.isFileExist(fullPath))
  281. console.log("DownLoadM setOnFileTaskSuccess fullPath...", fullPath)
  282. successCb(fullPath);
  283. }
  284. });
  285. downloader.setOnTaskError(function (task: any, errorCode: number, errorCodeInternal: number, errorStr: string) {
  286. console.log("DownLoadM setOnTaskError", errorCode + " ; " + errorCodeInternal + " ; " + errorStr)
  287. if (failCb) {
  288. failCb(fullPath);
  289. }
  290. });
  291. downloader.setOnTaskProgress(function (task: any, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) {
  292. console.log("DownLoadM", bytesReceived + " ; " + totalBytesReceived + " ; " + totalBytesExpected)
  293. if (progressCb) {
  294. progressCb(bytesReceived, totalBytesReceived, totalBytesExpected)
  295. }
  296. });
  297. downloader.createDownloadFileTask(url, fullPath);//创建下载任务
  298. return downloader
  299. }
  300. }
  301. }
  302. window['JsbMgr'] = JsbSystem