StartViewComp.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-02-27 16:33:37
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-03-06 15:49:28
  6. * @FilePath: \Cocos_android\assets\script\game\start\StartViewComp.ts
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%A“”
  8. */
  9. import { native } from "cc";
  10. import { _decorator } from "cc";
  11. import { oops } from "db://oops-framework/core/Oops";
  12. import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
  13. import { ecs } from "db://oops-framework/libs/ecs/ECS";
  14. import { CCComp } from "db://oops-framework/module/common/CCComp";
  15. const { ccclass, property } = _decorator;
  16. /** 视图层对象 */
  17. @ccclass('StartViewComp')
  18. @ecs.register('StartView', false)
  19. export class StartViewComp extends CCComp {
  20. private lang: boolean = false;
  21. /** 视图层逻辑代码分离演示 */
  22. start() {
  23. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  24. console.log("这是登录场景");
  25. }
  26. //显示手机声音状态
  27. showVolumeState() {
  28. if (this.lang == false) {
  29. this.lang = true;
  30. oops.log.logView("切换中文")
  31. oops.language.setLanguage("zh", () => { });
  32. }
  33. else {
  34. this.lang = false;
  35. oops.log.logView("切换英文")
  36. oops.language.setLanguage("en", () => { });
  37. }
  38. this.login("admin", "123456");
  39. }
  40. //回调安卓传Json数据,需要从Json解析成字符串,再传过去
  41. async callAndroidWithJson(args: Object) {
  42. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  43. let jsonStr = JSON.stringify(args);
  44. console.log("js传给安卓的数据打印>>>>>>>>>>>>>>>>>>>>", jsonStr);
  45. let result = await native.reflection.callStaticMethod("com/cocos/game/AndroidHandler", "onCocosMessage", "(Ljava/lang/String;)Ljava/lang/String;", jsonStr);
  46. //等待上边安卓返回数据,再执行下边代码
  47. if (result) {
  48. let android_result = JSON.parse(result);
  49. console.log("安卓返回数据打印>>>>>>>>>>>>>>>>>>>>", android_result);
  50. return android_result;
  51. }
  52. }
  53. //返回全部用字符串Json格式,然后在js解析,然后做相应处理,但是需要区分Json 后的字符串和普通字符串,需要做对用的标记
  54. }
  55. async login(username: string, password: string) {
  56. //区分普通登录,微信登录,游客登录
  57. let obj = {
  58. method: "login",
  59. callback: "StartViewComp.androidCallJs", //安卓完成业务的回调JS函数
  60. data: {
  61. username: username,
  62. password: password,
  63. type: 1 //登录类型
  64. }
  65. }
  66. let result = await this.callAndroidWithJson(obj);
  67. return result;
  68. }
  69. //用户注册
  70. async user_register(username: string, password: string) {
  71. let obj = {
  72. method: "user_register",
  73. callback: "",
  74. data: {
  75. username: username,
  76. password: password
  77. }
  78. }
  79. let result = await this.callAndroidWithJson(obj);
  80. return result;
  81. }
  82. //微信登录
  83. async wechat_login() {
  84. let obj = {
  85. method: "wechat_login",
  86. callback: "",
  87. data: {
  88. }
  89. }
  90. let result = await this.callAndroidWithJson(obj);
  91. return result;
  92. }
  93. //用户提现
  94. async user_withdrawal(user_id: string) {
  95. let obj = {
  96. method: "user_withdrawal",
  97. callback: "",
  98. data: {
  99. }
  100. }
  101. let result = await this.callAndroidWithJson(obj);
  102. return result;
  103. }
  104. //获取提现记录
  105. async get_withdrawal_record(user_id: string) {
  106. let obj = {
  107. method: "get_withdrawal_record",
  108. callback: "",
  109. data: {
  110. }
  111. }
  112. let result = await this.callAndroidWithJson(obj);
  113. return result;
  114. }
  115. //获取用户信息
  116. async get_user_info(user_id: string) {
  117. let obj = {
  118. method: "get_user_info",
  119. callback: "",
  120. data: {
  121. user_id: user_id
  122. }
  123. }
  124. let result = await this.callAndroidWithJson(obj);
  125. return result;
  126. }
  127. //现金提现
  128. async cash_withdrawal(user_id: string, amount: string) {
  129. let obj = {
  130. method: "cash_withdrawal",
  131. callback: "",
  132. data: {
  133. user_id: user_id,
  134. amount: amount
  135. }
  136. }
  137. let result = await this.callAndroidWithJson(obj);
  138. return result;
  139. }
  140. //红包提现
  141. async red_packet_withdrawal(user_id: string, amount: string) {
  142. let obj = {
  143. method: "red_packet_withdrawal",
  144. callback: "",
  145. data: {
  146. user_id: user_id,
  147. amount: amount
  148. }
  149. }
  150. let result = await this.callAndroidWithJson(obj);
  151. return result;
  152. }
  153. //复制文字
  154. async copy_text(text: string) {
  155. let obj = {
  156. method: "copy_text",
  157. callback: "",
  158. data: {
  159. text: text
  160. }
  161. }
  162. let result = await this.callAndroidWithJson(obj);
  163. return result;
  164. }
  165. //获取隐私协议
  166. //获取用户协议
  167. //关于我们
  168. //安卓直接回调js函数
  169. public static androidCallJs(name: string, password: string) {
  170. console.log("安卓回调数据???????????????", name, password);
  171. }
  172. /** 视图对象通过 ecs.Entity.remove(StartViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  173. reset() {
  174. this.node.destroy();
  175. }
  176. }
  177. window.StartViewComp = StartViewComp;