| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /*
- * @Author: mojunshou 1637302775@qq.com
- * @Date: 2025-03-31 10:45:44
- * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-04-08 16:33:08
- * @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';
- 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() {
- 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;
- }
- }
- window['CocosHandler'] = CocosHandler;
|