UIMail.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Node, instantiate } from "cc";
  2. import { GameUILayers } from "../../scripts/GameUILayers";
  3. import { Layout_UIMail } from "./Layout_UIMail";
  4. import { MailItem } from "./MailItem";
  5. import { lobbyNet } from "../scripts/NetGameServer";
  6. import { ModuleDef } from "../../scripts/ModuleDef";
  7. @tgx_class(ModuleDef.BASIC)
  8. export class UIMail extends tgx.UIController {
  9. constructor() {
  10. super("ui_mail/ui_mail", GameUILayers.POPUP, Layout_UIMail);
  11. }
  12. private _itemPrefab: Node = null;
  13. private _lastSelected: MailItem = null;
  14. protected onCreated(): void {
  15. let layout = this._layout as Layout_UIMail;
  16. this._itemPrefab = layout.mailRoot.children[0];
  17. layout.mailRoot.removeAllChildren();
  18. this.onButtonEvent(layout.btnClose, () => {
  19. this.close();
  20. });
  21. this.onButtonEvent(layout.btnMarkAllAsRead, async () => {
  22. let ret = await lobbyNet.callApi('lobby/mail/MarkAllAsRead', { });
  23. if (ret.isSucc) {
  24. this.onMarkAllAsRead();
  25. }
  26. });
  27. this.onButtonEvent(layout.btnClearAll, async () => {
  28. tgx.UIAlert.show('确定要删除吗?', true).onClick(async b => {
  29. if (b) {
  30. let ret = await lobbyNet.callApi('lobby/mail/ClearAllMails', { });
  31. if (ret.isSucc) {
  32. layout.mailRoot.removeAllChildren();
  33. layout.lblContent.node.active = false;
  34. layout.emptyTips.active = true;
  35. this._lastSelected = null;
  36. }
  37. }
  38. });
  39. });
  40. this.onButtonEvent(layout.btnDelete, () => {
  41. tgx.UIAlert.show('确定要删除吗?', true).onClick(async b => {
  42. if (b) {
  43. let deleteItem = this._lastSelected;
  44. let ret = await lobbyNet.callApi('lobby/mail/DeleteMail', { mailId: this._lastSelected.data.mailId });
  45. if (ret.isSucc) {
  46. deleteItem.node.removeFromParent();
  47. if (!layout.mailRoot.children.length) {
  48. layout.lblContent.node.active = false;
  49. layout.emptyTips.active = true;
  50. this._lastSelected = null;
  51. }
  52. else {
  53. this.setAsSelected(layout.mailRoot.children[0].getComponent(MailItem));
  54. }
  55. }
  56. }
  57. });
  58. });
  59. this.initMailList();
  60. }
  61. onMarkAllAsRead() {
  62. let layout = this._layout as Layout_UIMail;
  63. layout.mailRoot.children.forEach(v => {
  64. let comp = v.getComponent(MailItem);
  65. comp.hasRead.active = true;
  66. comp.data.state = 'read';
  67. });
  68. }
  69. async initMailList() {
  70. let layout = this._layout as Layout_UIMail;
  71. let ret = await lobbyNet.callApi('lobby/mail/GetMails', { });
  72. if (!ret.isSucc) {
  73. layout.emptyTips.active = true;
  74. return;
  75. }
  76. layout.emptyTips.active = false;
  77. if(!ret.res.mails.length){
  78. layout.emptyTips.active = true;
  79. return;
  80. }
  81. ret.res.mails.sort((a, b): number => {
  82. return b.time - a.time;
  83. });
  84. let index = 0;
  85. ret.res.mails.forEach(v => {
  86. let item = instantiate(this._itemPrefab);
  87. layout.mailRoot.addChild(item);
  88. let comp = item.getComponent(MailItem);
  89. comp.setData(v);
  90. this.onButtonEvent(item, () => {
  91. if (this._lastSelected != comp) {
  92. this.setAsSelected(comp);
  93. }
  94. });
  95. if (index == 0) {
  96. layout.lblContent.string = comp.data.content;
  97. comp.selected = true;
  98. this._lastSelected = comp;
  99. }
  100. else {
  101. comp.selected = false;
  102. }
  103. index++;
  104. });
  105. }
  106. async setAsSelected(comp: MailItem) {
  107. let layout = this._layout as Layout_UIMail;
  108. layout.lblContent.node.active = true;
  109. layout.lblContent.string = comp.data.content + '\n' + new Date(comp.data.time).toString();
  110. comp.selected = true;
  111. this._lastSelected.selected = false;
  112. this._lastSelected = comp;
  113. if (!comp.data.state) {
  114. let ret = await lobbyNet.callApi('lobby/mail/MarkAsRead', { mailId: comp.data.mailId });
  115. if (ret.isSucc) {
  116. comp.data.state = 'read';
  117. comp.hasRead.active = true;
  118. }
  119. }
  120. }
  121. }