123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /** @format */
- import {UI} from '../enums/UI'
- import {BaseUI} from './BaseUI'
- import {Data, Mgr} from '../GameControl'
- import {ccUtils} from '../utils/ccUtils'
- import {label, list, node, observer, render} from '../mobx/observer'
- import {msgCmd} from '../proto/msg_cmd'
- import List from '../uiutils/List'
- import {MailConfig} from '../config/MailConfig'
- import {idNum, mail, packageMail} from '../proto/typedef'
- import {getMailGoodsRsp, getMailRsp, sendMail} from '../proto/game'
- import {EVENT} from '../enums/Enum'
- import {IRewardNty} from '../interface/UIInterface'
- import {i18nLabel} from '../uiutils/i18nLabel'
- const {ccclass, property} = cc._decorator
- @ccclass
- @observer
- export class Example extends BaseUI {
- emailData: mail[]
- packageMail: idNum[]
- selEmail: mail
- @list('em_sv')
- emailList: List
- @list('e_txt/item_sv')
- emItemList: List
- @node('e_txt/txt_sv/view/content/txt_lb')
- emTxtLbNode: cc.Node
- @node('e_txt/cl_btn')
- claimBtnNode: cc.Node
- @node('e_txt/del_btn')
- delBtnNode: cc.Node
- receiveAll: boolean = false
- /********************** 周期事件 **********************/
- onLoad() {}
- onEnable() {
- Mgr.net.add(msgCmd.cmd_get_mail_rsp, this, this.onGetMailRsp)
- Mgr.net.add(msgCmd.cmd_flag_mail_rsp, this, this.onFlagMailRsp)
- Mgr.net.add(msgCmd.cmd_get_mail_goods_rsp, this, this.onGetMailGoodsRsp)
- Mgr.net.add(msgCmd.cmd_del_mail_rsp, this, this.onDelMailRsp)
- Mgr.net.send(msgCmd.cmd_get_mail)
- }
- onShow(args, fromUI: number) {}
- onHide(): any {
- Mgr.event.removeAll(this)
- }
- initContentUI() {
- // 根据附件是否领取(或者是否有附件)判断激活哪个按钮
- let emData = this.selEmail
- if (null != emData) {
- this.claimBtnNode.active = !emData.receive
- this.delBtnNode.active = emData.receive || !emData.attach
- // 若有附件,根据领取与否,更新附件状态(是否置灰)
- if (emData.attach) {
- this.packageMail = Mgr.goods.getGoodsList(emData.attachment)
- this.emItemList.numItems = this.packageMail.length
- }
- }
- }
- initCurEmail() {
- let emCfg = MailConfig[this.selEmail.subject]
- // 设置右侧邮件标题
- let emTitleNode = cc.find('e_txt', this.node)
- ccUtils.setLabel(emCfg.title, emTitleNode, 'tit_lb')
- // 设置右侧邮件内容
- let str = emCfg.content
- if (str == '%s') {
- str = this.selEmail.body
- }
- this.emTxtLbNode.getComponent(i18nLabel).init(str, this.selEmail.body.split('^'))
- // 更新UI(附件图标+领取/删除按钮)状态
- this.initContentUI()
- }
- initEmailItem(node: cc.Node, index: number) {
- // 左侧邮件列表标题和时间初始化,加载上限1000
- if (undefined != this.emailData && index < 1000) {
- let emailListData = this.emailData[index]
- // 将邮件的唯一标识绑定到邮件列表Item node
- node['emID'] = emailListData.id
- // 设置被选中邮件背景框为放大的高亮框
- let itemSelNode = cc.find('it_s_bg', node)
- itemSelNode.active = this.selEmail.id == emailListData.id
- // itemSelNode.scale = itemSelNode.active ? 1.2 : 1
- // 根据邮件是否已读设置右上角红点显示与否 优化成领取后消失
- let itemRedDots = cc.find('red_dots', node)
- itemRedDots.active = this.isRedTip(emailListData, index)
- // 若列表中的邮件已读,且未被选中,将邮件的默认背景框置灰
- let itemUnSelNode = cc.find('it_uns_bg', node)
- ccUtils.setSpriteGray(emailListData.read, itemUnSelNode)
- // 邮件主题设置
- let emCfg = MailConfig[emailListData.subject]
- ccUtils.setLabel(emCfg.title, node, 'e_tit_lb')
- // 邮件时间设置 乘以1000转成毫秒
- let emTime = Date.Format('MM/dd/yyyy hh:mm:ss', emailListData.date * 1000)
- ccUtils.setLabel(emTime, node, 'time_lb')
- }
- }
- isRedTip(email: mail, index) {
- return (!email.read && 0 != index && !email.attach) || (!email.receive && email.attach)
- }
- resetList() {
- // this.emailData.sort((a, b) => {
- // return (
- // Number(this.isRedTip(b, this.emailData.indexOf(b))) -
- // Number(this.isRedTip(a, this.emailData.indexOf(a)))
- // )
- // })
- this.emailList.numItems = this.emailData.length
- }
- initPackageItem(node: cc.Node, index: number) {
- // 单封邮件最大附件数50
- Mgr.goods.initOneGoods(this.packageMail[index], cc.find('goods', node), this)
- // 更新附件状态,若已领取,附件图标置灰
- ccUtils.setAllGray(this.selEmail.receive, node)
- }
- initMainEmailRed() {
- Data.main.emailRed = this.emailData.some(value => !value.read || (!value.receive && value.attach))
- }
- /********************** 网络事件 **********************/
- onGetMailRsp(data: getMailRsp) {
- this.emailData = data.list
- this.selEmail = this.emailData[0]
- // 没有邮件时,右侧内容界面置空
- let isEmpty = 0 == this.emailData.length
- cc.find('emp_lb', this.node).active = isEmpty
- cc.find('e_txt', this.node).active = !isEmpty
- // 若第一封邮件未读,给服务器发送已读标记信息
- if (!isEmpty) {
- if (!this.emailData[0].read) {
- Mgr.net.send(msgCmd.cmd_flag_mail, {list: [this.selEmail.id]})
- }
- // 初始化邮件列表第一封邮件UI状态
- this.initCurEmail()
- }
- this.resetList()
- }
- onFlagMailRsp() {
- if (this.selEmail) {
- this.selEmail.read = true
- this.resetList()
- // 如果当前标记已读的邮件没有附件,需要更新领取按钮为删除按钮
- this.initContentUI()
- }
- this.initMainEmailRed()
- }
- onGetMailGoodsRsp(data: getMailGoodsRsp, rewardNty: IRewardNty) {
- Mgr.ui.showReward(rewardNty)
- // 领取全部的时候,将有附件的邮件领取和已读标志置为true
- if (this.receiveAll) {
- this.emailData.forEach(v => {
- if (v.attach) {
- v.receive = true
- v.read = true
- }
- })
- } else {
- if (this.selEmail) {
- this.selEmail.receive = true
- }
- }
- this.resetList()
- this.initContentUI()
- Data.main.emailRed = false
- this.initMainEmailRed()
- }
- onDelMailRsp() {
- Mgr.net.send(msgCmd.cmd_get_mail)
- }
- /********************** 触发事件 **********************/
- /********************** 点击事件 **********************/
- onSelectedEmail(e) {
- this.selEmail = this.emailData.find(v => v.id == e.target['emID'])
- if (!this.selEmail.read) {
- Mgr.net.send(msgCmd.cmd_flag_mail, {list: [this.selEmail.id]})
- } else {
- this.emailList.numItems = this.emailData.length
- }
- this.initCurEmail()
- }
- onClaimClick() {
- this.receiveAll = false
- Mgr.net.send(msgCmd.cmd_get_mail_goods, {list: [this.selEmail.id]})
- }
- onDeleteClick() {
- Mgr.net.send(msgCmd.cmd_del_mail, {list: [this.selEmail.id]})
- }
- onClaimAllClick() {
- this.receiveAll = true
- // 获取所有未领取附件的邮件id并打包成对象(isGet false),发送全部领取的消息
- let emailIds = this.emailData.filter(obj => obj.attach && !obj.receive).map(obj => obj.id)
- if (emailIds.length > 0) {
- Mgr.net.send(msgCmd.cmd_get_mail_goods, {list: emailIds})
- }
- }
- onDeleteAllClick() {
- let emailIds = this.emailData.map(obj => obj.id)
- if (this.emailData.length > 0) {
- Mgr.net.send(msgCmd.cmd_del_mail, {list: emailIds})
- }
- }
- }
|