UI_Invite.ts 2.4 KB

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