/* * @Author: mojunshou 1637302775@qq.com * @Date: 2025-03-31 10:45:44 * @LastEditors: mojunshou 1637302775@qq.com * @LastEditTime: 2025-04-08 17:33:14 * @Description: CocosHandler 处理类负责与安卓交互 */ import { native } from 'cc'; import { _decorator } from 'cc'; import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil'; import { smc } from '../SingletonModuleComp'; import { Account } from '../../account/Account'; import { oops } from 'db://oops-framework/core/Oops'; import { GameEvent } from '../config/GameEvent'; import { ProtocolEvent } from './ProtocolEvent'; import { AndroidEvent } from '../config/AndroidEvent'; import { UIID } from '../config/GameUIConfig'; const { ccclass, property } = _decorator; type CocosHandlerType = { method: string; param?: string; } export class CocosHandler { // 单例模式 private static _inst: CocosHandler; public static get inst(): CocosHandler { if (!this._inst) { this._inst = new CocosHandler(); } return this._inst; } // 发送消息到 Android public async sendMessageToAndroid(data: CocosHandlerType) { if (DeviceUtil.isAndroid && DeviceUtil.isNative) { let result = await native.reflection.callStaticMethod("com/cocos/game/AtmobCocosCaller", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", data.method, data.param); return result; } } //微信登录 async wechat_login() { const param = { "appId": "wx1234567890", //这个ID没申请下来 "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); console.log("微信登录结果>>>>>>>>>>", result); // smc.account.AccountModel. return result; } //获取隐私授权状态 async getPrivacyStatus() { const data: CocosHandlerType = { method: "privacy.grant.get", } let result = await this.sendMessageToAndroid(data); console.log("安卓返回隐私授权状态", result); return JSON.parse(result); } //保存隐私授权状态 async savePrivacyStatus(status: boolean) { const param = { "granted": status, } const data: CocosHandlerType = { method: "privacy.grant.set", param: JSON.stringify(param) } let result = await this.sendMessageToAndroid(data); console.log("安卓返回隐私授权状态", result); return JSON.parse(result); } //打开隐私协议或者用户协议 openAgreement() { const type = smc.account.AccountModel.ProtocolType; console.log(">>>>>协议类型>>>>>>>>>>>>>>>>>>", type) const param = { "url": type == 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl } const data: CocosHandlerType = { method: "system.browser.open", param: JSON.stringify(param) } this.sendMessageToAndroid(data); } //方法請求 async methodRequest(type: number) { let param = {}; switch (type) { case 1: break; case 2: break; } param = { "url": "", "callback": { "onSuccess": "CocosHandler.inst.methodRequest_success", "onFaile": "CocosHandler.inst.methodRequest_fail" }, " param": { } } const data: CocosHandlerType = { method: "request.post", param: JSON.stringify(param) } } //=================================以下是广告方法==================== //每次启动都加载一次启屏广告 async ad_interstitial_start() { const param = { "funcId": "101", "callback": { "onClose": "CocosHandler.inst.ad_splash_close" } } const data: CocosHandlerType = { method: "ad.splash", param: JSON.stringify(param) } let result = await this.sendMessageToAndroid(data); console.log("启屏广告返回数据", result); return result; } //启屏广告关闭时回调 ad_splash_close() { if (oops.gui.has(UIID.KindTips)) { oops.gui.remove(UIID.KindTips); } if (oops.gui.has(UIID.Retention)) { oops.gui.remove(UIID.Retention); } oops.message.dispatchEvent(AndroidEvent.AgreePrivacy); } //广告请求--插屏广告 async ad_interstitial() { const param = { "funcId": "103", "callback": { "onLoaded": "", "onLoadFailed": "CocosHandler.inst.ad_interstitial_load_failed", "onShow": "", "onShowFailed": "CocosHandler.inst.ad_interstitial_show_failed", "onClose": "CocosHandler.inst.ad_interstitial_close" } } const data: CocosHandlerType = { method: "ad.interstitial", param: JSON.stringify(param) } let result = await this.sendMessageToAndroid(data); return result; } //广告关闭回调 ad_interstitial_close(type: boolean) { console.log("广告关闭回调", type); } //广告加载成功回调 ad_interstitial_loaded() { console.log("广告加载成功回调"); } //广告加载失败回调 ad_interstitial_load_failed(msg: string) { console.log("广告加载失败回调", msg); } //广告显示失败回调 ad_interstitial_show_failed() { console.log("广告显示失败回调"); } // //===================安卓回调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) { } //==================跟服务器交互====================== //微信登录 async wx_login(code: string) { const param = { "code": code, "url": ProtocolEvent.WechatLogin } const data: CocosHandlerType = { method: "request.post", param: JSON.stringify(param) } let result = await this.sendMessageToAndroid(data); console.log("微信登录结果", result); return result; } //获取账号信息 getAccountInfo() { const param = { "url": ProtocolEvent.AccountInfo, "param": { "authToken": "123456" }, "callback": { "onSuccess": "CocosHandler.inst.getAccountInfo_success", "onFail": "CocosHandler.inst.getAccountInfo_fail" } } const data: CocosHandlerType = { method: "request.post", param: JSON.stringify(param) } this.sendMessageToAndroid(data); } //获取账号信息成功回调 getAccountInfo_success(str: string) { console.log("获取账号信息成功回调", str); } //获取账号信息失败回调 getAccountInfo_fail(str: string) { console.log("获取账号信息失败回调", str); } } window['CocosHandler'] = CocosHandler;