UI_Invite.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Layout } 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. const layout = this.getLayout<Layout_Invite>();
  34. chsdk.getShareList().then((res) => {
  35. if (res.code === 0 && res.data) {
  36. // 确保数据有效
  37. const friendCount = res.data.length; // 计算人数
  38. layout.Txt.string = friendCount + '/' + layout.target_count;
  39. layout.progressBar.progress = friendCount / layout.target_count;
  40. let remain= layout.target_count - friendCount;
  41. layout.Num_Sp.spriteFrame=layout.Num[remain-1];
  42. } else {
  43. // 错误处理
  44. console.error(`获取分享列表失败: ${res.err || "未知错误"}`);
  45. }
  46. }).catch((err) => {
  47. // 捕获 promise 的异常
  48. console.error(`请求失败: ${err.message}`);
  49. });
  50. }
  51. show(){
  52. const layout=this.getLayout<Layout_Invite>();
  53. layout.progressBar.progress = layout.count/layout.target_count;
  54. layout.Txt.string = layout.count + '/' + layout.target_count;
  55. }
  56. }