150 lines
5.2 KiB
TypeScript
150 lines
5.2 KiB
TypeScript
import { Node, instantiate, tween } from "cc";
|
|
import {tgxUIAlert, tgxUIController, tgx_class, tgxAudioMgr} from "../../core_tgx/tgx";
|
|
import { GameUILayers } from "../../scripts/GameUILayers";
|
|
import { Layout_UIMail } from "./Layout_UIMail";
|
|
import { MailItem } from "./MailItem";
|
|
import { NetGameServer } from "../../module_basic/scripts/NetGameServer";
|
|
import { ModuleDef } from "../../scripts/ModuleDef";
|
|
import { EMusicDefine } from "../../module_basic/scripts/MusicDefine";
|
|
|
|
@tgx_class(ModuleDef.Arean)
|
|
export class UIMail extends tgxUIController {
|
|
constructor() {
|
|
super("ui_mail/ui_mail", GameUILayers.POPUP, Layout_UIMail);
|
|
}
|
|
|
|
private _itemPrefab: Node = null;
|
|
|
|
private _lastSelected: MailItem = null;
|
|
|
|
protected onCreated(): void {
|
|
let layout = this.layout as Layout_UIMail;
|
|
|
|
this._itemPrefab = layout.mailRoot.children[0];
|
|
layout.mailRoot.removeAllChildren();
|
|
this.onButtonEvent(layout.btnClose, () => {
|
|
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLOSE);
|
|
this.close();
|
|
});
|
|
|
|
this.onButtonEvent(layout.btnMarkAllAsRead, async () => {
|
|
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
|
|
let ret = await NetGameServer.inst.callApi('lobby/mail/MarkAllAsRead', { });
|
|
if (ret.isSucc) {
|
|
this.onMarkAllAsRead();
|
|
}
|
|
});
|
|
|
|
this.onButtonEvent(layout.btnClearAll, async () => {
|
|
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
|
|
tgxUIAlert.show('确定要删除吗?', true).onClick(async b => {
|
|
if (b) {
|
|
let ret = await NetGameServer.inst.callApi('lobby/mail/ClearAllMails', { });
|
|
if (ret.isSucc) {
|
|
layout.mailRoot.removeAllChildren();
|
|
layout.lblContent.node.active = false;
|
|
layout.emptyTips.active = true;
|
|
this._lastSelected = null;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
this.onButtonEvent(layout.btnDelete, () => {
|
|
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
|
|
tgxUIAlert.show('确定要删除吗?', true).onClick(async b => {
|
|
if (b) {
|
|
let deleteItem = this._lastSelected;
|
|
let ret = await NetGameServer.inst.callApi('lobby/mail/DeleteMail', { mailId: this._lastSelected.data.mailId });
|
|
if (ret.isSucc) {
|
|
deleteItem.node.removeFromParent();
|
|
if (!layout.mailRoot.children.length) {
|
|
layout.lblContent.node.active = false;
|
|
layout.emptyTips.active = true;
|
|
this._lastSelected = null;
|
|
}
|
|
else {
|
|
this.setAsSelected(layout.mailRoot.children[0].getComponent(MailItem));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
this.initMailList();
|
|
}
|
|
|
|
onMarkAllAsRead() {
|
|
let layout = this.layout as Layout_UIMail;
|
|
layout.mailRoot.children.forEach(v => {
|
|
let comp = v.getComponent(MailItem);
|
|
comp.hasRead.active = true;
|
|
comp.data.state = 'read';
|
|
});
|
|
}
|
|
|
|
async initMailList() {
|
|
let layout = this.layout as Layout_UIMail;
|
|
let ret = await NetGameServer.inst.callApi('lobby/mail/GetMails', { });
|
|
layout.waiting.active = false;
|
|
if (!ret.isSucc) {
|
|
layout.emptyTips.active = true;
|
|
return;
|
|
}
|
|
|
|
layout.emptyTips.active = false;
|
|
|
|
if(!ret.res.mails.length){
|
|
layout.emptyTips.active = true;
|
|
return;
|
|
}
|
|
|
|
ret.res.mails.sort((a, b): number => {
|
|
return b.time - a.time;
|
|
});
|
|
|
|
let index = 0;
|
|
ret.res.mails.forEach(v => {
|
|
let item = instantiate(this._itemPrefab);
|
|
layout.mailRoot.addChild(item);
|
|
let comp = item.getComponent(MailItem);
|
|
comp.setData(v);
|
|
|
|
this.onButtonEvent(item, () => {
|
|
if (this._lastSelected != comp) {
|
|
this.setAsSelected(comp);
|
|
}
|
|
});
|
|
|
|
if (index == 0) {
|
|
layout.lblContent.string = comp.data.content;
|
|
comp.selected = true;
|
|
this._lastSelected = comp;
|
|
}
|
|
else {
|
|
comp.selected = false;
|
|
}
|
|
|
|
index++;
|
|
});
|
|
}
|
|
|
|
async setAsSelected(comp: MailItem) {
|
|
let layout = this.layout as Layout_UIMail;
|
|
layout.lblContent.node.active = true;
|
|
layout.lblContent.string = comp.data.content + '\n' + new Date(comp.data.time).toString();
|
|
comp.selected = true;
|
|
this._lastSelected.selected = false;
|
|
this._lastSelected = comp;
|
|
if (!comp.data.state) {
|
|
let ret = await NetGameServer.inst.callApi('lobby/mail/MarkAsRead', { mailId: comp.data.mailId });
|
|
if (ret.isSucc) {
|
|
comp.data.state = 'read';
|
|
comp.hasRead.active = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|