CocosHandler.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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-02 18:34:55
  6. * @Description: CocosHandler 处理类负责与安卓交互
  7. */
  8. import { native } from 'cc';
  9. import { _decorator, Component, Node } from 'cc';
  10. import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
  11. import { smc } from '../SingletonModuleComp';
  12. import { ecs } from 'db://oops-framework/libs/ecs/ECS';
  13. import { Account } from '../../account/Account';
  14. import { oops } from 'db://oops-framework/core/Oops';
  15. import { GameEvent } from '../config/GameEvent';
  16. const { ccclass, property } = _decorator;
  17. type CocosHandlerType = {
  18. method: string;
  19. param: string;
  20. }
  21. @ccclass('CocosHandler')
  22. export class CocosHandler {
  23. // 单例模式
  24. private static _inst: CocosHandler;
  25. public static get inst(): CocosHandler {
  26. if (!this._inst) {
  27. this._inst = new CocosHandler();
  28. }
  29. return this._inst;
  30. }
  31. // 处理 Android 发送的消息
  32. private onAndroidMessage(jsonStr: string) {
  33. const data = JSON.parse(jsonStr);
  34. console.log('Cocos 收到 Android 消息:', data);
  35. // 处理逻辑
  36. // 例如:根据 data 的内容执行不同的操作,不是每个回调都要处理
  37. }
  38. // 发送消息到 Android
  39. public async sendMessageToAndroid(json: CocosHandlerType) {
  40. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  41. let jsonStr = JSON.stringify(json);
  42. console.log("js传给安卓的数据打印>>>>", jsonStr);
  43. let result = await native.reflection.callStaticMethod("com/atmob/cocos/bridge/AtmobCocosBridge", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", jsonStr);
  44. //等待上边安卓返回数据,再执行下边代码
  45. if (result) {
  46. let android_result = JSON.parse(result);
  47. console.log("安卓返回数据打印>>>>>", android_result);
  48. return android_result;
  49. }
  50. }
  51. }
  52. //微信登录
  53. async wechat_login() {
  54. const param = {
  55. "appId": "wx1234567890",
  56. "callback":
  57. {
  58. "onSuccess": "CocosHandler.inst.wechat_login_success",
  59. "onFaile": "CocosHandler.inst.wechat_login_fail"
  60. }
  61. }
  62. const data: CocosHandlerType = {
  63. method: "auth.wechat",
  64. param: JSON.stringify(param)
  65. }
  66. let result = await this.sendMessageToAndroid(data);
  67. // smc.account.AccountModel.
  68. return result;
  69. }
  70. //===================安卓回调Cocos======================
  71. //微信登录成功回调
  72. wechat_login_success(str: string) {
  73. console.log("微信登录成功回调", str);
  74. //保存数据
  75. oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
  76. }
  77. //微信登录失败回调
  78. wechat_login_fail(str: string) {
  79. console.log("微信登录失败回调", str);
  80. oops.gui.toast("微信登录失败");
  81. }
  82. //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
  83. async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
  84. }
  85. }
  86. window['CocosHandler'] = CocosHandler;