| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- /*
- * @Author: mojunshou 1637302775@qq.com
- * @Date: 2025-03-06 15:56:14
- * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-03-06 16:33:21
- * @Description: Cocos向安卓发送消息处理类
- */
- // CocosHandler.ts
- import { native } from 'cc';
- import { _decorator, Component } from 'cc';
- import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
- const { ccclass } = _decorator;
- @ccclass('CocosHandler')
- export class CocosHandler extends Component {
- // 单例模式
- private static instance: CocosHandler;
- public static getInstance(): CocosHandler {
- if (!CocosHandler.instance) {
- CocosHandler.instance = new CocosHandler();
- }
- return CocosHandler.instance;
- }
- // 初始化通信
- // public initCommunication() {
- // // 注册 Android 调用 Cocos 的方法
- // if (window.jsb) {
- // window.jsb.reflection.setNativeCallback('onAndroidMessage', this.onAndroidMessage.bind(this));
- // }
- // }
- // 处理 Android 发送的消息
- private onAndroidMessage(jsonStr: string) {
- const data = JSON.parse(jsonStr);
- console.log('Cocos 收到 Android 消息:', data);
- // 处理逻辑
- // 例如:根据 data 的内容执行不同的操作,不是每个回调都要处理
- }
- // 发送消息到 Android
- public async sendMessageToAndroid(json: Object) {
- if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
- let jsonStr = JSON.stringify(json);
- console.log("js传给安卓的数据打印>>>>", jsonStr);
- let result = await native.reflection.callStaticMethod("com/cocos/game/AndroidHandler", "onCocosMessage", "(Ljava/lang/String;)Ljava/lang/String;", jsonStr);
- //等待上边安卓返回数据,再执行下边代码
- if (result) {
- let android_result = JSON.parse(result);
- console.log("安卓返回数据打印>>>>>", android_result);
- return android_result;
- }
- }
- }
- async login(username: string, password: string) {
- //区分普通登录,微信登录,游客登录
- let obj = {
- method: "login",
- callback: "StartViewComp.androidCallJs", //安卓完成业务的回调JS函数
- data: {
- username: username,
- password: password,
- type: 1 //登录类型
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //用户注册
- async user_register(username: string, password: string) {
- let obj = {
- method: "user_register",
- callback: "",
- data: {
- username: username,
- password: password
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //微信登录
- async wechat_login() {
- let obj = {
- method: "wechat_login",
- callback: "",
- data: {
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //用户提现
- async user_withdrawal(user_id: string) {
- let obj = {
- method: "user_withdrawal",
- callback: "",
- data: {
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //获取提现记录
- async get_withdrawal_record(user_id: string) {
- let obj = {
- method: "get_withdrawal_record",
- callback: "",
- data: {
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //获取用户信息
- async get_user_info(user_id: string) {
- let obj = {
- method: "get_user_info",
- callback: "",
- data: {
- user_id: user_id
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //现金提现
- async cash_withdrawal(user_id: string, amount: string) {
- let obj = {
- method: "cash_withdrawal",
- callback: "",
- data: {
- user_id: user_id,
- amount: amount
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //红包提现
- async red_packet_withdrawal(user_id: string, amount: string) {
- let obj = {
- method: "red_packet_withdrawal",
- callback: "",
- data: {
- user_id: user_id,
- amount: amount
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //复制文字
- async copy_text(text: string) {
- let obj = {
- method: "copy_text",
- callback: "",
- data: {
- text: text
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //获取远程CDN地址,这用和安卓交互
- //获取游戏数据信息--返回后才能进入游戏渲染数据,也可以登录后推送过来
- async get_game_data() {
- let obj = {
- method: "get_game_data",
- callback: "",
- data: {
- }
- }
- let result = await this.sendMessageToAndroid(obj);
- return result;
- }
- //
- }
- window['CocosHandler'] = CocosHandler.getInstance();
|