CocosHandler.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 17:33:14
  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. import { AndroidEvent } from '../config/AndroidEvent';
  17. import { UIID } from '../config/GameUIConfig';
  18. const { ccclass, property } = _decorator;
  19. type CocosHandlerType = {
  20. method: string;
  21. param?: string;
  22. }
  23. export class CocosHandler {
  24. // 单例模式
  25. private static _inst: CocosHandler;
  26. public static get inst(): CocosHandler {
  27. if (!this._inst) {
  28. this._inst = new CocosHandler();
  29. }
  30. return this._inst;
  31. }
  32. // 发送消息到 Android
  33. public async sendMessageToAndroid(data: CocosHandlerType) {
  34. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  35. let result = await native.reflection.callStaticMethod("com/cocos/game/AtmobCocosCaller", "call", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", data.method, data.param);
  36. return result;
  37. }
  38. }
  39. //微信登录
  40. async wechat_login() {
  41. const param = {
  42. "appId": "wx1234567890", //这个ID没申请下来
  43. "callback":
  44. {
  45. "onSuccess": "CocosHandler.inst.wechat_login_success",
  46. "onFaile": "CocosHandler.inst.wechat_login_fail"
  47. }
  48. }
  49. const data: CocosHandlerType = {
  50. method: "auth.wechat",
  51. param: JSON.stringify(param)
  52. }
  53. let result = await this.sendMessageToAndroid(data);
  54. console.log("微信登录结果>>>>>>>>>>", result);
  55. // smc.account.AccountModel.
  56. return result;
  57. }
  58. //获取隐私授权状态
  59. async getPrivacyStatus() {
  60. const data: CocosHandlerType = {
  61. method: "privacy.grant.get",
  62. }
  63. let result = await this.sendMessageToAndroid(data);
  64. console.log("安卓返回隐私授权状态", result);
  65. return JSON.parse(result);
  66. }
  67. //保存隐私授权状态
  68. async savePrivacyStatus(status: boolean) {
  69. const param = {
  70. "granted": status,
  71. }
  72. const data: CocosHandlerType = {
  73. method: "privacy.grant.set",
  74. param: JSON.stringify(param)
  75. }
  76. let result = await this.sendMessageToAndroid(data);
  77. console.log("安卓返回隐私授权状态", result);
  78. return JSON.parse(result);
  79. }
  80. //打开隐私协议或者用户协议
  81. openAgreement() {
  82. const type = smc.account.AccountModel.ProtocolType;
  83. console.log(">>>>>协议类型>>>>>>>>>>>>>>>>>>", type)
  84. const param = {
  85. "url": type == 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl
  86. }
  87. const data: CocosHandlerType = {
  88. method: "system.browser.open",
  89. param: JSON.stringify(param)
  90. }
  91. this.sendMessageToAndroid(data);
  92. }
  93. //方法請求
  94. async methodRequest(type: number) {
  95. let param = {};
  96. switch (type) {
  97. case 1:
  98. break;
  99. case 2:
  100. break;
  101. }
  102. param = {
  103. "url": "",
  104. "callback":
  105. {
  106. "onSuccess": "CocosHandler.inst.methodRequest_success",
  107. "onFaile": "CocosHandler.inst.methodRequest_fail"
  108. },
  109. " param": {
  110. }
  111. }
  112. const data: CocosHandlerType = {
  113. method: "request.post",
  114. param: JSON.stringify(param)
  115. }
  116. }
  117. //=================================以下是广告方法====================
  118. //每次启动都加载一次启屏广告
  119. async ad_interstitial_start() {
  120. const param = {
  121. "funcId": "101",
  122. "callback":
  123. {
  124. "onClose": "CocosHandler.inst.ad_splash_close"
  125. }
  126. }
  127. const data: CocosHandlerType = {
  128. method: "ad.splash",
  129. param: JSON.stringify(param)
  130. }
  131. let result = await this.sendMessageToAndroid(data);
  132. console.log("启屏广告返回数据", result);
  133. return result;
  134. }
  135. //启屏广告关闭时回调
  136. ad_splash_close() {
  137. if (oops.gui.has(UIID.KindTips)) {
  138. oops.gui.remove(UIID.KindTips);
  139. }
  140. if (oops.gui.has(UIID.Retention)) {
  141. oops.gui.remove(UIID.Retention);
  142. }
  143. oops.message.dispatchEvent(AndroidEvent.AgreePrivacy);
  144. }
  145. //广告请求--插屏广告
  146. async ad_interstitial() {
  147. const param = {
  148. "funcId": "103",
  149. "callback":
  150. {
  151. "onLoaded": "",
  152. "onLoadFailed": "CocosHandler.inst.ad_interstitial_load_failed",
  153. "onShow": "",
  154. "onShowFailed": "CocosHandler.inst.ad_interstitial_show_failed",
  155. "onClose": "CocosHandler.inst.ad_interstitial_close"
  156. }
  157. }
  158. const data: CocosHandlerType = {
  159. method: "ad.interstitial",
  160. param: JSON.stringify(param)
  161. }
  162. let result = await this.sendMessageToAndroid(data);
  163. return result;
  164. }
  165. //广告关闭回调
  166. ad_interstitial_close(type: boolean) {
  167. console.log("广告关闭回调", type);
  168. }
  169. //广告加载成功回调
  170. ad_interstitial_loaded() {
  171. console.log("广告加载成功回调");
  172. }
  173. //广告加载失败回调
  174. ad_interstitial_load_failed(msg: string) {
  175. console.log("广告加载失败回调", msg);
  176. }
  177. //广告显示失败回调
  178. ad_interstitial_show_failed() {
  179. console.log("广告显示失败回调");
  180. }
  181. //
  182. //===================安卓回调Cocos======================
  183. //微信登录成功回调
  184. wechat_login_success(str: string) {
  185. console.log("微信登录成功回调", str);
  186. //保存数据
  187. // oops.message.dispatchEvent(GameEvent.WechatLoginSuss);
  188. // 获取用户信息
  189. //然后就是和服务器通信
  190. }
  191. //微信登录失败回调
  192. wechat_login_fail(str: string) {
  193. console.log("微信登录失败回调", str);
  194. oops.gui.toast("微信登录失败");
  195. }
  196. //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
  197. async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
  198. }
  199. //==================跟服务器交互======================
  200. //微信登录
  201. async wx_login(code: string) {
  202. const param = {
  203. "code": code,
  204. "url": ProtocolEvent.WechatLogin
  205. }
  206. const data: CocosHandlerType = {
  207. method: "request.post",
  208. param: JSON.stringify(param)
  209. }
  210. let result = await this.sendMessageToAndroid(data);
  211. console.log("微信登录结果", result);
  212. return result;
  213. }
  214. //获取账号信息
  215. getAccountInfo() {
  216. const param = {
  217. "url": ProtocolEvent.AccountInfo,
  218. "param": {
  219. "authToken": "123456"
  220. },
  221. "callback": {
  222. "onSuccess": "CocosHandler.inst.getAccountInfo_success",
  223. "onFail": "CocosHandler.inst.getAccountInfo_fail"
  224. }
  225. }
  226. const data: CocosHandlerType = {
  227. method: "request.post",
  228. param: JSON.stringify(param)
  229. }
  230. this.sendMessageToAndroid(data);
  231. }
  232. //获取账号信息成功回调
  233. getAccountInfo_success(str: string) {
  234. console.log("获取账号信息成功回调", str);
  235. }
  236. //获取账号信息失败回调
  237. getAccountInfo_fail(str: string) {
  238. console.log("获取账号信息失败回调", str);
  239. }
  240. }
  241. window['CocosHandler'] = CocosHandler;