| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /*
- * @Author: mojunshou 1637302775@qq.com
- * @Date: 2025-03-31 10:45:44
- * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-04-02 18:34:55
- * @Description: CocosHandler 处理类负责与安卓交互
- */
- import { native } from 'cc';
- import { _decorator, Component, Node } from 'cc';
- import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
- import { smc } from '../SingletonModuleComp';
- import { ecs } from 'db://oops-framework/libs/ecs/ECS';
- import { Account } from '../../account/Account';
- import { oops } from 'db://oops-framework/core/Oops';
- import { GameEvent } from '../config/GameEvent';
- const { ccclass, property } = _decorator;
- type CocosHandlerType = {
- method: string;
- param: string;
- }
- @ccclass('CocosHandler')
- export class CocosHandler {
- // 单例模式
- private static _inst: CocosHandler;
- public static get inst(): CocosHandler {
- if (!this._inst) {
- this._inst = new CocosHandler();
- }
- return this._inst;
- }
- // 处理 Android 发送的消息
- private onAndroidMessage(jsonStr: string) {
- const data = JSON.parse(jsonStr);
- console.log('Cocos 收到 Android 消息:', data);
- // 处理逻辑
- // 例如:根据 data 的内容执行不同的操作,不是每个回调都要处理
- }
- // 发送消息到 Android
- public async sendMessageToAndroid(json: CocosHandlerType) {
- if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
- let jsonStr = JSON.stringify(json);
- console.log("js传给安卓的数据打印>>>>", jsonStr);
- let result = await native.reflection.callStaticMethod("com/atmob/cocos/bridge/AtmobCocosBridge", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", jsonStr);
- //等待上边安卓返回数据,再执行下边代码
- if (result) {
- let android_result = JSON.parse(result);
- console.log("安卓返回数据打印>>>>>", android_result);
- return android_result;
- }
- }
- }
- //微信登录
- async wechat_login() {
- const param = {
- "appId": "wx1234567890",
- "callback":
- {
- "onSuccess": "CocosHandler.inst.wechat_login_success",
- "onFaile": "CocosHandler.inst.wechat_login_fail"
- }
- }
- const data: CocosHandlerType = {
- method: "auth.wechat",
- param: JSON.stringify(param)
- }
- let result = await this.sendMessageToAndroid(data);
- // smc.account.AccountModel.
- return result;
- }
- //===================安卓回调Cocos======================
- //微信登录成功回调
- wechat_login_success(str: string) {
- console.log("微信登录成功回调", str);
- //保存数据
- oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
- }
- //微信登录失败回调
- wechat_login_fail(str: string) {
- console.log("微信登录失败回调", str);
- oops.gui.toast("微信登录失败");
- }
- //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
- async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
- }
- }
- window['CocosHandler'] = CocosHandler;
|