UI_Invite.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator } from "cc";
  2. import { GameUILayers, gui } from "../../../core/ui/ui";
  3. import ui_base from "../../../core/ui/ui_base";
  4. import { ModuleDef } from "../../../Scripts/ModuleDef";
  5. import { ani_ui } from "../UI_Main/UI_Main";
  6. import { Layout_Invite } from "./Layout_Invite";
  7. const { ccclass, property } = _decorator;
  8. @ccclass('UI_Invite')
  9. export class UI_Invite extends ui_base {
  10. constructor() {
  11. super(ModuleDef.GAME, 'ui/UI_Invite/Invite', GameUILayers.HUD, Layout_Invite);
  12. }
  13. protected async onCreated() {
  14. // 拉取通过邀请进入该游戏的好友数量
  15. this.get_Count();
  16. this.show();
  17. const layout = this.getLayout<Layout_Invite>();
  18. ani_ui(layout.Close_Btn.node.parent);
  19. this.onButtonEvent(layout.Close_Btn, () => {
  20. gui.close(UI_Invite);
  21. });
  22. this.onButtonEvent(layout.Invite_Friend_Btn, () => {
  23. //分享给好友
  24. chsdk.shareAppMessage();
  25. });
  26. this.onButtonEvent(layout.Refresh_Btn, () => {
  27. //刷新好友列表
  28. this.get_Count();
  29. this.show();
  30. })
  31. }
  32. get_Count(){
  33. chsdk.getShareList().then((res) => {
  34. if (res.code === 0 && res.data) {
  35. // 确保数据有效
  36. const friendCount = res.data.length; // 计算人数
  37. console.log(`通过邀请进入游戏的好友数量: ${friendCount}`);
  38. } else {
  39. // 错误处理
  40. console.error(`获取分享列表失败: ${res.err || "未知错误"}`);
  41. }
  42. }).catch((err) => {
  43. // 捕获 promise 的异常
  44. console.error(`请求失败: ${err.message}`);
  45. });
  46. }
  47. show(){
  48. const layout=this.getLayout<Layout_Invite>();
  49. layout.progressBar.progress = layout.count/layout.target_count;
  50. layout.Txt.string = layout.count + '/' + layout.target_count;
  51. }
  52. }