CocosHandler.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 10:16:02
  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. //===================安卓回调Cocos======================
  112. //微信登录成功回调
  113. wechat_login_success(str: string) {
  114. console.log("微信登录成功回调", str);
  115. //保存数据
  116. oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
  117. }
  118. //微信登录失败回调
  119. wechat_login_fail(str: string) {
  120. console.log("微信登录失败回调", str);
  121. oops.gui.toast("微信登录失败");
  122. }
  123. //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
  124. async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
  125. }
  126. }
  127. window['CocosHandler'] = CocosHandler;