|
|
@@ -1,32 +1,22 @@
|
|
|
/*
|
|
|
* @Author: mojunshou 1637302775@qq.com
|
|
|
- * @Date: 2025-03-31 10:45:44
|
|
|
+ * @Date: 2025-04-07 17:49:55
|
|
|
* @LastEditors: mojunshou 1637302775@qq.com
|
|
|
- * @LastEditTime: 2025-04-10 10:48:20
|
|
|
- * @Description: CocosHandler 处理类负责与安卓交互
|
|
|
+ * @LastEditTime: 2025-04-11 11:13:59
|
|
|
+ * @Description:
|
|
|
*/
|
|
|
+// CocosHandler.ts
|
|
|
+// 封装对外模块调度(统一入口)
|
|
|
+
|
|
|
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';
|
|
|
-import { get } from 'http';
|
|
|
-import { AD_TYPE } from '../config/GameDefine';
|
|
|
-const { ccclass, property } = _decorator;
|
|
|
|
|
|
-type CocosHandlerType = {
|
|
|
+export type CocosHandlerType = {
|
|
|
method: string;
|
|
|
param?: string;
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
export class CocosHandler {
|
|
|
-
|
|
|
- // 单例模式
|
|
|
private static _inst: CocosHandler;
|
|
|
public static get inst(): CocosHandler {
|
|
|
if (!this._inst) {
|
|
|
@@ -35,449 +25,20 @@ export class CocosHandler {
|
|
|
return this._inst;
|
|
|
}
|
|
|
|
|
|
- // 发送消息到 Android
|
|
|
- public async sendMessageToAndroid(data: CocosHandlerType) {
|
|
|
+ public async sendMessageToAndroid(data: CocosHandlerType, logTag: string = '') {
|
|
|
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 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;
|
|
|
- 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 wechat_login() {
|
|
|
- const param = {
|
|
|
- "callback":
|
|
|
- {
|
|
|
- "onSuccess": "CocosHandler.inst.wechat_login_success",
|
|
|
- "onFaile": "CocosHandler.inst.wechat_login_fail"
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- const data: CocosHandlerType = {
|
|
|
- method: "auth.wechat",
|
|
|
- param: JSON.stringify(param)
|
|
|
- }
|
|
|
- this.sendMessageToAndroid(data);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //微信登录成功回调
|
|
|
- wechat_login_success(str: string) {
|
|
|
- console.log("微信登录成功回调", str);
|
|
|
- //保存数据
|
|
|
- // oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
|
|
|
- // 获取用户信息
|
|
|
- //然后就是和服务器通信
|
|
|
- this.wx_login(str);
|
|
|
- }
|
|
|
-
|
|
|
- //微信登录失败回调
|
|
|
- wechat_login_fail(str: string) {
|
|
|
- console.log("微信登录失败回调", str);
|
|
|
- oops.gui.toast("微信登录失败");
|
|
|
- }
|
|
|
-
|
|
|
- //==================跟服务器交互======================
|
|
|
- //微信登录
|
|
|
- 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);
|
|
|
- if (result.data) {
|
|
|
- this.getAccountInfo();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //获取账号信息
|
|
|
- getAccountInfo() {
|
|
|
- const param = {
|
|
|
- "url": ProtocolEvent.AccountInfo,
|
|
|
- "callback": {
|
|
|
- "onSuccess": "CocosHandler.inst.getAccountInfo_success",
|
|
|
- "onFail": "CocosHandler.inst.getAccountInfo_fail"
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- this.sendMsgToServer(param);
|
|
|
- }
|
|
|
-
|
|
|
- //获取账号信息成功回调
|
|
|
- getAccountInfo_success(str: string) {
|
|
|
- console.log("获取账号信息成功回调", str);
|
|
|
- //保存账号信息
|
|
|
- let data = JSON.parse(str);
|
|
|
- smc.account.AccountModel.uid = data.uid;
|
|
|
- console.log("uid>>>>>>>", data.uid);
|
|
|
- smc.account.AccountModel.curScore = data.currentLevelInfo.score;
|
|
|
- smc.account.AccountModel.curLevel = data.currentLevelInfo.level;
|
|
|
- smc.account.AccountModel.targetScore = smc.account.AccountModel.curLevelConfig.score;
|
|
|
- smc.account.AccountModel.curLevelConfig = data.currentLevelConf;
|
|
|
- smc.account.AccountModel.costInfo = data.handlingChargeConf;
|
|
|
- smc.account.AccountModel.isLogined = data.logined;
|
|
|
- smc.account.AccountModel.accountName = data.nickname;
|
|
|
- smc.account.AccountModel.uid = data.uid;
|
|
|
- smc.account.AccountModel.headUrl = data.headImgUrl;
|
|
|
- oops.message.dispatchEvent(GameEvent.UserLogin);
|
|
|
- }
|
|
|
-
|
|
|
- //获取账号信息失败回调
|
|
|
- getAccountInfo_fail(code: number, str: string) {
|
|
|
- console.log("获取账号信息失败回调", str);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //领取每日奖励
|
|
|
- getDailyReward(level: number) {
|
|
|
- const param = {
|
|
|
- "url": ProtocolEvent.GetDailyReward,
|
|
|
- "param": {
|
|
|
- "level": level
|
|
|
- },
|
|
|
- "callback": {
|
|
|
- "onSuccess": "CocosHandler.inst.getDailyReward_success",
|
|
|
- "onFail": "CocosHandler.inst.request_fail"
|
|
|
- }
|
|
|
- }
|
|
|
- this.sendMsgToServer(param);
|
|
|
- }
|
|
|
-
|
|
|
- //领取每日奖励成功回调
|
|
|
- getDailyReward_success(str: string) {
|
|
|
- console.log("领取每日奖励成功回调", str);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- //======获取红包提现列表数据
|
|
|
- getDailyTaskReward() {
|
|
|
- //先请求一遍用户道具信息
|
|
|
- this.getUserItemInfo();
|
|
|
- const param = {
|
|
|
- "url": ProtocolEvent.GetDailyTask,
|
|
|
- "callback": {
|
|
|
- "onSuccess": "CocosHandler.inst.getDailyTaskReward_success",
|
|
|
- "onFail": "CocosHandler.inst.request_fail"
|
|
|
- }
|
|
|
- }
|
|
|
- this.sendMsgToServer(param);
|
|
|
- }
|
|
|
-
|
|
|
- //获取红包提现列表数据成功回调
|
|
|
- getDailyTaskReward_success(str: string) {
|
|
|
- console.log("获取红包提现列表数据成功回调", str);
|
|
|
- //这里再打开记录
|
|
|
- oops.message.dispatchEvent(GameEvent.openRedBagView, "openRedBagView");
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //获取用户道具信息
|
|
|
- getUserItemInfo() {
|
|
|
- const param = {
|
|
|
- "url": ProtocolEvent.UserItemInfo,
|
|
|
- "callback": {
|
|
|
- "onSuccess": "CocosHandler.inst.getUserItemInfo_success",
|
|
|
- "onFail": "CocosHandler.inst.request_fail"
|
|
|
- }
|
|
|
- }
|
|
|
- this.sendMsgToServer(param);
|
|
|
- }
|
|
|
-
|
|
|
- //获取用户道具信息成功
|
|
|
- getUserItemInfo_success(str: string) {
|
|
|
- console.log("获取用户道具信息成功回调", str);
|
|
|
- //保存用户道具信息
|
|
|
- let result = JSON.parse(str);
|
|
|
- // smc.account.AccountModel.setUserItemInfo(result);
|
|
|
- smc.account.AccountModel.gameCoin = result.data.props["1005"];
|
|
|
- smc.account.AccountModel.cashCoin = result.data.props["1004"];
|
|
|
- smc.account.AccountModel.handlingCharge = result.data.props["1009"];
|
|
|
- smc.account.AccountModel.goldCoin = result.data.propsp["1006"];
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //获取提现记录
|
|
|
- getRecordList() {
|
|
|
- const param = {
|
|
|
- "url": ProtocolEvent.GetWithdrawRecord,
|
|
|
- "param": {
|
|
|
- "offset": 0,
|
|
|
- "limit": 50
|
|
|
- },
|
|
|
- "callback": {
|
|
|
- "onSuccess": "CocosHandler.inst.getRecordList_success",
|
|
|
- "onFail": "CocosHandler.inst.request_fail"
|
|
|
- }
|
|
|
- }
|
|
|
- this.sendMsgToServer(param);
|
|
|
- }
|
|
|
-
|
|
|
- //获取提现记录成功
|
|
|
- getRecordList_success(str: string) {
|
|
|
- console.log("获取提现记录成功>>>>>>>>>>", str)
|
|
|
- let result = JSON.parse(str);
|
|
|
- if (result.count > 0) {
|
|
|
- smc.account.AccountModel.recordList = result.data.list;
|
|
|
- }
|
|
|
- oops.message.dispatchEvent(GameEvent.openRecordView);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- //发送信息给服务器
|
|
|
- sendMsgToServer(param: any) {
|
|
|
- const data: CocosHandlerType = {
|
|
|
- method: "request.post",
|
|
|
- param: JSON.stringify(param)
|
|
|
- }
|
|
|
- this.sendMessageToAndroid(data);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- request_fail(code: number, str: string) {
|
|
|
- console.log("请求失败回调", code, str);
|
|
|
- }
|
|
|
- //=================================以下是广告方法====================
|
|
|
- //每次启动都加载一次启屏广告
|
|
|
-
|
|
|
- //显示广告
|
|
|
- async show_ad(id: string) {
|
|
|
- const data: CocosHandlerType = {
|
|
|
- method: "",
|
|
|
- param: ""
|
|
|
- }
|
|
|
- const param = {
|
|
|
- "funcId": "",
|
|
|
- "callback":
|
|
|
- {
|
|
|
- "onLoaded": "",
|
|
|
- "onShow": "",
|
|
|
- "onLoadFailed": "",
|
|
|
- "onShowFailed": "",
|
|
|
- "onClose": ""
|
|
|
- }
|
|
|
- }
|
|
|
- switch (id) {
|
|
|
- //启屏广告
|
|
|
- case AD_TYPE.Start:
|
|
|
- param.funcId = id;
|
|
|
- param.callback.onShowFailed = "CocosHandler.inst.ad_splash_onShowFailed";
|
|
|
- param.callback.onClose = "CocosHandler.inst.ad_splash_close";
|
|
|
- data.method = "ad.splash";
|
|
|
- break;
|
|
|
- //插屏广告
|
|
|
- case AD_TYPE.Jion_Main:
|
|
|
- case AD_TYPE.Double_Close:
|
|
|
- case AD_TYPE.Rebates:
|
|
|
- case AD_TYPE.Double_Speed_Close:
|
|
|
- param.funcId = id;
|
|
|
- data.method = "ad.interstitial";
|
|
|
- param.callback.onClose = "CocosHandler.inst.ad_interstitial_close";
|
|
|
- param.callback.onShowFailed = "CocosHandler.inst.ad_interstitial_show_failed";
|
|
|
- break;
|
|
|
- //激励广告
|
|
|
- case AD_TYPE.Double_Receive:
|
|
|
- case AD_TYPE.Double_Speed_Receive:
|
|
|
- case AD_TYPE.Pass_Receive:
|
|
|
- case AD_TYPE.Resurrection:
|
|
|
- param.funcId = id;
|
|
|
- data.method = "ad.reward"
|
|
|
- param.callback.onClose = "CocosHandler.inst.ad_reward_close";
|
|
|
- param.callback.onShowFailed = "CocosHandler.inst.ad_reward_onShowFailed"
|
|
|
-
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- data.param = JSON.stringify(param);
|
|
|
- let result = await this.sendMessageToAndroid(data);
|
|
|
- console.log("广告ID", id, "广告返回数据", result);
|
|
|
- return result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- async ad_interstitial_start() {
|
|
|
- const param = {
|
|
|
- "funcId": "101",
|
|
|
- "callback":
|
|
|
- {
|
|
|
- "onShowFailed": "ad_splash_onShowFailed",
|
|
|
- "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;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //启屏广告关闭时回调
|
|
|
- async 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);
|
|
|
- }
|
|
|
- let result = await CocosHandler.inst.savePrivacyStatus(true);
|
|
|
- if (result.code == 0) {
|
|
|
- //广告结束后调用登录
|
|
|
- this.getAccountInfo();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //启屏广告加载失败
|
|
|
- ad_splash_onShowFailed() {
|
|
|
- this.getAccountInfo();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //广告请求--插屏广告
|
|
|
- async ad_interstitial(code: string) {
|
|
|
- const param = {
|
|
|
- "funcId": code,
|
|
|
- "callback":
|
|
|
- {
|
|
|
- "onLoaded": "",
|
|
|
- "onLoadFailed": "CocosHandler.inst.ad_interstitial_load_failed",
|
|
|
- "onShow": "",
|
|
|
- "onShowFailed": "CocosHandler.inst.ad_interstitial_show_failed",
|
|
|
- "onClose": "CocosHandler.inst.ad_interstitial_close"
|
|
|
+ const result = await native.reflection.callStaticMethod(
|
|
|
+ 'com/cocos/game/AtmobCocosCaller',
|
|
|
+ 'call',
|
|
|
+ '(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;',
|
|
|
+ data.method,
|
|
|
+ data.param
|
|
|
+ );
|
|
|
+ if (logTag) {
|
|
|
+ console.log(`[${logTag}] Android 返回:`, result);
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
- 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("广告显示失败回调");
|
|
|
- }
|
|
|
-
|
|
|
- //广告请求--激励视频广告
|
|
|
- async ad_reward(id: number) {
|
|
|
- const param = {
|
|
|
- "funcId": `${id}`,
|
|
|
- "callback":
|
|
|
- {
|
|
|
- "onClose": "CocosHandler.inst.ad_reward_close"
|
|
|
- }
|
|
|
- }
|
|
|
- const data: CocosHandlerType = {
|
|
|
- method: "ad.reward",
|
|
|
- param: JSON.stringify(param)
|
|
|
- }
|
|
|
- let result = await this.sendMessageToAndroid(data);
|
|
|
- return result;
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- //激励视频广告关闭回调
|
|
|
- ad_reward_close(str: string) {
|
|
|
- console.log("激励视频广告关闭回调", str);
|
|
|
-
|
|
|
-
|
|
|
- //返回通知主场景,告诉要不要发奖励
|
|
|
- }
|
|
|
-
|
|
|
- //激励视频显示失败回调
|
|
|
- ad_reward_onShowFailed(str: string) {
|
|
|
- console.log("激励视频显示失败回调", str);
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
-window['CocosHandler'] = CocosHandler;
|
|
|
-
|
|
|
-
|