CocosHandler.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-31 10:45:44
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-08 16:33:08
  6. * @Description: CocosHandler 处理类负责与安卓交互
  7. */
  8. import { native } from 'cc';
  9. import { _decorator } from 'cc';
  10. import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
  11. import { smc } from '../SingletonModuleComp';
  12. import { Account } from '../../account/Account';
  13. import { oops } from 'db://oops-framework/core/Oops';
  14. import { GameEvent } from '../config/GameEvent';
  15. import { ProtocolEvent } from './ProtocolEvent';
  16. const { ccclass, property } = _decorator;
  17. type CocosHandlerType = {
  18. method: string;
  19. param?: string;
  20. }
  21. export class CocosHandler {
  22. // 单例模式
  23. private static _inst: CocosHandler;
  24. public static get inst(): CocosHandler {
  25. if (!this._inst) {
  26. this._inst = new CocosHandler();
  27. }
  28. return this._inst;
  29. }
  30. // 发送消息到 Android
  31. public async sendMessageToAndroid(data: CocosHandlerType) {
  32. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  33. let result = await native.reflection.callStaticMethod("com/cocos/game/AtmobCocosCaller", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", data.method, data.param);
  34. return result;
  35. }
  36. }
  37. //微信登录
  38. async wechat_login() {
  39. const param = {
  40. "appId": "wx1234567890", //这个ID没申请下来
  41. "callback":
  42. {
  43. "onSuccess": "CocosHandler.inst.wechat_login_success",
  44. "onFaile": "CocosHandler.inst.wechat_login_fail"
  45. }
  46. }
  47. const data: CocosHandlerType = {
  48. method: "auth.wechat",
  49. param: JSON.stringify(param)
  50. }
  51. let result = await this.sendMessageToAndroid(data);
  52. console.log("微信登录结果>>>>>>>>>>", result);
  53. // smc.account.AccountModel.
  54. return result;
  55. }
  56. //获取隐私授权状态
  57. async getPrivacyStatus() {
  58. const data: CocosHandlerType = {
  59. method: "privacy.grant.get",
  60. }
  61. let result = await this.sendMessageToAndroid(data);
  62. console.log("安卓返回隐私授权状态", result);
  63. return JSON.parse(result);
  64. }
  65. //保存隐私授权状态
  66. async savePrivacyStatus(status: boolean) {
  67. const param = {
  68. "granted": status,
  69. }
  70. const data: CocosHandlerType = {
  71. method: "privacy.grant.set",
  72. param: JSON.stringify(param)
  73. }
  74. let result = await this.sendMessageToAndroid(data);
  75. console.log("安卓返回隐私授权状态", result);
  76. return JSON.parse(result);
  77. }
  78. //打开隐私协议或者用户协议
  79. openAgreement() {
  80. const type = smc.account.AccountModel.ProtocolType;
  81. console.log(">>>>>协议类型>>>>>>>>>>>>>>>>>>", type)
  82. const param = {
  83. "url": type == 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl
  84. }
  85. const data: CocosHandlerType = {
  86. method: "system.browser.open",
  87. param: JSON.stringify(param)
  88. }
  89. this.sendMessageToAndroid(data);
  90. }
  91. //方法請求
  92. async methodRequest(type: number) {
  93. let param = {};
  94. switch (type) {
  95. case 1:
  96. break;
  97. case 2:
  98. break;
  99. }
  100. param = {
  101. "url": "",
  102. "callback":
  103. {
  104. "onSuccess": "CocosHandler.inst.methodRequest_success",
  105. "onFaile": "CocosHandler.inst.methodRequest_fail"
  106. },
  107. " param": {
  108. }
  109. }
  110. const data: CocosHandlerType = {
  111. method: "request.post",
  112. param: JSON.stringify(param)
  113. }
  114. }
  115. //=================================以下是广告方法====================
  116. //广告请求--插屏广告
  117. async ad_interstitial() {
  118. const param = {
  119. "funcId": "103",
  120. "callback":
  121. {
  122. "onLoaded": "",
  123. "onLoadFailed": "CocosHandler.inst.ad_interstitial_load_failed",
  124. "onShow": "",
  125. "onShowFailed": "CocosHandler.inst.ad_interstitial_show_failed",
  126. "onClose": "CocosHandler.inst.ad_interstitial_close"
  127. }
  128. }
  129. const data: CocosHandlerType = {
  130. method: "ad.interstitial",
  131. param: JSON.stringify(param)
  132. }
  133. let result = await this.sendMessageToAndroid(data);
  134. return result;
  135. }
  136. //广告关闭回调
  137. ad_interstitial_close(type: boolean) {
  138. console.log("广告关闭回调", type);
  139. }
  140. //广告加载成功回调
  141. ad_interstitial_loaded() {
  142. console.log("广告加载成功回调");
  143. }
  144. //广告加载失败回调
  145. ad_interstitial_load_failed(msg: string) {
  146. console.log("广告加载失败回调", msg);
  147. }
  148. //广告显示失败回调
  149. ad_interstitial_show_failed() {
  150. console.log("广告显示失败回调");
  151. }
  152. //
  153. //===================安卓回调Cocos======================
  154. //微信登录成功回调
  155. wechat_login_success(str: string) {
  156. console.log("微信登录成功回调", str);
  157. //保存数据
  158. // oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
  159. // 获取用户信息
  160. //然后就是和服务器通信
  161. }
  162. //微信登录失败回调
  163. wechat_login_fail(str: string) {
  164. console.log("微信登录失败回调", str);
  165. oops.gui.toast("微信登录失败");
  166. }
  167. //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
  168. async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
  169. }
  170. //==================跟服务器交互======================
  171. //微信登录
  172. async wx_login(code: string) {
  173. const param = {
  174. "code": code,
  175. "url": ProtocolEvent.WechatLogin
  176. }
  177. const data: CocosHandlerType = {
  178. method: "request.post",
  179. param: JSON.stringify(param)
  180. }
  181. let result = await this.sendMessageToAndroid(data);
  182. console.log("微信登录结果", result);
  183. return result;
  184. }
  185. }
  186. window['CocosHandler'] = CocosHandler;