CocosHandler.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-31 10:45:44
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-07 15:35:51
  6. * @Description: CocosHandler 处理类负责与安卓交互
  7. */
  8. import { native } from 'cc';
  9. import { _decorator } from 'cc';
  10. import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
  11. import { smc } from '../SingletonModuleComp';
  12. import { Account } from '../../account/Account';
  13. import { oops } from 'db://oops-framework/core/Oops';
  14. import { GameEvent } from '../config/GameEvent';
  15. const { ccclass, property } = _decorator;
  16. type CocosHandlerType = {
  17. method: string;
  18. param?: string;
  19. }
  20. export class CocosHandler {
  21. // 单例模式
  22. private static _inst: CocosHandler;
  23. public static get inst(): CocosHandler {
  24. if (!this._inst) {
  25. this._inst = new CocosHandler();
  26. }
  27. return this._inst;
  28. }
  29. // 发送消息到 Android
  30. public async sendMessageToAndroid(data: CocosHandlerType) {
  31. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  32. let result = await native.reflection.callStaticMethod("com/cocos/game/AtmobCocosCaller", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", data.method, data.param);
  33. return result;
  34. }
  35. }
  36. //微信登录
  37. async wechat_login() {
  38. const param = {
  39. "appId": "wx1234567890",
  40. "callback":
  41. {
  42. "onSuccess": "CocosHandler.inst.wechat_login_success",
  43. "onFaile": "CocosHandler.inst.wechat_login_fail"
  44. }
  45. }
  46. const data: CocosHandlerType = {
  47. method: "auth.wechat",
  48. param: JSON.stringify(param)
  49. }
  50. let result = await this.sendMessageToAndroid(data);
  51. // smc.account.AccountModel.
  52. return result;
  53. }
  54. //获取隐私授权状态
  55. async getPrivacyStatus() {
  56. const data: CocosHandlerType = {
  57. method: "system.privacy.get",
  58. }
  59. let result = await this.sendMessageToAndroid(data);
  60. return result;
  61. }
  62. //保存隐私授权状态
  63. async savePrivacyStatus(status: boolean) {
  64. const param = {
  65. "granted": status,
  66. }
  67. const data: CocosHandlerType = {
  68. method: "privacy.grant.set",
  69. param: JSON.stringify(param)
  70. }
  71. let result = await this.sendMessageToAndroid(data);
  72. return result;
  73. }
  74. //打开隐私协议或者用户协议
  75. openAgreement() {
  76. const type = smc.account.AccountModel.ProtocolType;
  77. console.log(">>>>>协议类型>>>>>>>>>>>>>>>>>>", type)
  78. const param = {
  79. "url": type == 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl
  80. }
  81. const data: CocosHandlerType = {
  82. method: "system.browser.open",
  83. param: JSON.stringify(param)
  84. }
  85. this.sendMessageToAndroid(data);
  86. }
  87. //方法請求
  88. async methodRequest(type: number) {
  89. let param = {};
  90. switch (type) {
  91. case 1:
  92. break;
  93. case 2:
  94. break;
  95. }
  96. param = {
  97. "url": "",
  98. "callback":
  99. {
  100. "onSuccess": "CocosHandler.inst.methodRequest_success",
  101. "onFaile": "CocosHandler.inst.methodRequest_fail"
  102. },
  103. " param": {
  104. }
  105. }
  106. const data: CocosHandlerType = {
  107. method: "request.post",
  108. param: JSON.stringify(param)
  109. }
  110. }
  111. //广告请求--插屏广告
  112. async ad_interstitial() {
  113. const param = {
  114. "funcId": "103",
  115. "callback":
  116. {
  117. "onLoaded": "",
  118. "onLoadFailed": "CocosHandler.inst.ad_interstitial_load_failed",
  119. "onShow": "",
  120. "onShowFailed": "CocosHandler.inst.ad_interstitial_show_failed",
  121. "onClose": "CocosHandler.inst.ad_interstitial_close"
  122. }
  123. }
  124. const data: CocosHandlerType = {
  125. method: "ad.interstitial",
  126. param: JSON.stringify(param)
  127. }
  128. let result = await this.sendMessageToAndroid(data);
  129. return result;
  130. }
  131. //广告关闭回调
  132. ad_interstitial_close(type: boolean) {
  133. console.log("广告关闭回调", type);
  134. }
  135. //广告加载成功回调
  136. ad_interstitial_loaded() {
  137. console.log("广告加载成功回调");
  138. }
  139. //广告加载失败回调
  140. ad_interstitial_load_failed() {
  141. console.log("广告加载失败回调");
  142. }
  143. //广告显示失败回调
  144. ad_interstitial_show_failed() {
  145. console.log("广告显示失败回调");
  146. }
  147. //===================安卓回调Cocos======================
  148. //微信登录成功回调
  149. wechat_login_success(str: string) {
  150. console.log("微信登录成功回调", str);
  151. //保存数据
  152. oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
  153. }
  154. //微信登录失败回调
  155. wechat_login_fail(str: string) {
  156. console.log("微信登录失败回调", str);
  157. oops.gui.toast("微信登录失败");
  158. }
  159. //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
  160. async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
  161. }
  162. }
  163. window['CocosHandler'] = CocosHandler;