CocosHandler.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-06 15:56:14
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-03-06 16:33:21
  6. * @Description: Cocos向安卓发送消息处理类
  7. */
  8. // CocosHandler.ts
  9. import { native } from 'cc';
  10. import { _decorator, Component } from 'cc';
  11. import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
  12. const { ccclass } = _decorator;
  13. @ccclass('CocosHandler')
  14. export class CocosHandler extends Component {
  15. // 单例模式
  16. private static instance: CocosHandler;
  17. public static getInstance(): CocosHandler {
  18. if (!CocosHandler.instance) {
  19. CocosHandler.instance = new CocosHandler();
  20. }
  21. return CocosHandler.instance;
  22. }
  23. // 初始化通信
  24. // public initCommunication() {
  25. // // 注册 Android 调用 Cocos 的方法
  26. // if (window.jsb) {
  27. // window.jsb.reflection.setNativeCallback('onAndroidMessage', this.onAndroidMessage.bind(this));
  28. // }
  29. // }
  30. // 处理 Android 发送的消息
  31. private onAndroidMessage(jsonStr: string) {
  32. const data = JSON.parse(jsonStr);
  33. console.log('Cocos 收到 Android 消息:', data);
  34. // 处理逻辑
  35. // 例如:根据 data 的内容执行不同的操作,不是每个回调都要处理
  36. }
  37. // 发送消息到 Android
  38. public async sendMessageToAndroid(json: Object) {
  39. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  40. let jsonStr = JSON.stringify(json);
  41. console.log("js传给安卓的数据打印>>>>", jsonStr);
  42. let result = await native.reflection.callStaticMethod("com/cocos/game/AndroidHandler", "onCocosMessage", "(Ljava/lang/String;)Ljava/lang/String;", jsonStr);
  43. //等待上边安卓返回数据,再执行下边代码
  44. if (result) {
  45. let android_result = JSON.parse(result);
  46. console.log("安卓返回数据打印>>>>>", android_result);
  47. return android_result;
  48. }
  49. }
  50. }
  51. async login(username: string, password: string) {
  52. //区分普通登录,微信登录,游客登录
  53. let obj = {
  54. method: "login",
  55. callback: "StartViewComp.androidCallJs", //安卓完成业务的回调JS函数
  56. data: {
  57. username: username,
  58. password: password,
  59. type: 1 //登录类型
  60. }
  61. }
  62. let result = await this.sendMessageToAndroid(obj);
  63. return result;
  64. }
  65. //用户注册
  66. async user_register(username: string, password: string) {
  67. let obj = {
  68. method: "user_register",
  69. callback: "",
  70. data: {
  71. username: username,
  72. password: password
  73. }
  74. }
  75. let result = await this.sendMessageToAndroid(obj);
  76. return result;
  77. }
  78. //微信登录
  79. async wechat_login() {
  80. let obj = {
  81. method: "wechat_login",
  82. callback: "",
  83. data: {
  84. }
  85. }
  86. let result = await this.sendMessageToAndroid(obj);
  87. return result;
  88. }
  89. //用户提现
  90. async user_withdrawal(user_id: string) {
  91. let obj = {
  92. method: "user_withdrawal",
  93. callback: "",
  94. data: {
  95. }
  96. }
  97. let result = await this.sendMessageToAndroid(obj);
  98. return result;
  99. }
  100. //获取提现记录
  101. async get_withdrawal_record(user_id: string) {
  102. let obj = {
  103. method: "get_withdrawal_record",
  104. callback: "",
  105. data: {
  106. }
  107. }
  108. let result = await this.sendMessageToAndroid(obj);
  109. return result;
  110. }
  111. //获取用户信息
  112. async get_user_info(user_id: string) {
  113. let obj = {
  114. method: "get_user_info",
  115. callback: "",
  116. data: {
  117. user_id: user_id
  118. }
  119. }
  120. let result = await this.sendMessageToAndroid(obj);
  121. return result;
  122. }
  123. //现金提现
  124. async cash_withdrawal(user_id: string, amount: string) {
  125. let obj = {
  126. method: "cash_withdrawal",
  127. callback: "",
  128. data: {
  129. user_id: user_id,
  130. amount: amount
  131. }
  132. }
  133. let result = await this.sendMessageToAndroid(obj);
  134. return result;
  135. }
  136. //红包提现
  137. async red_packet_withdrawal(user_id: string, amount: string) {
  138. let obj = {
  139. method: "red_packet_withdrawal",
  140. callback: "",
  141. data: {
  142. user_id: user_id,
  143. amount: amount
  144. }
  145. }
  146. let result = await this.sendMessageToAndroid(obj);
  147. return result;
  148. }
  149. //复制文字
  150. async copy_text(text: string) {
  151. let obj = {
  152. method: "copy_text",
  153. callback: "",
  154. data: {
  155. text: text
  156. }
  157. }
  158. let result = await this.sendMessageToAndroid(obj);
  159. return result;
  160. }
  161. //获取远程CDN地址,这用和安卓交互
  162. //获取游戏数据信息--返回后才能进入游戏渲染数据,也可以登录后推送过来
  163. async get_game_data() {
  164. let obj = {
  165. method: "get_game_data",
  166. callback: "",
  167. data: {
  168. }
  169. }
  170. let result = await this.sendMessageToAndroid(obj);
  171. return result;
  172. }
  173. //
  174. }
  175. window['CocosHandler'] = CocosHandler.getInstance();