EmailUI.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /** @format */
  2. import {UI} from '../enums/UI'
  3. import {BaseUI} from './BaseUI'
  4. import {Data, Mgr} from '../GameControl'
  5. import {ccUtils} from '../utils/ccUtils'
  6. import {label, list, node, observer, render} from '../mobx/observer'
  7. import {msgCmd} from '../proto/msg_cmd'
  8. import List from '../uiutils/List'
  9. import {MailConfig} from '../config/MailConfig'
  10. import {idNum, mail, packageMail} from '../proto/typedef'
  11. import {getMailGoodsRsp, getMailRsp, sendMail} from '../proto/game'
  12. import {EVENT} from '../enums/Enum'
  13. import {IRewardNty} from '../interface/UIInterface'
  14. import {i18nLabel} from '../uiutils/i18nLabel'
  15. const {ccclass, property} = cc._decorator
  16. @ccclass
  17. @observer
  18. export class Example extends BaseUI {
  19. emailData: mail[]
  20. packageMail: idNum[]
  21. selEmail: mail
  22. @list('em_sv')
  23. emailList: List
  24. @list('e_txt/item_sv')
  25. emItemList: List
  26. @node('e_txt/txt_sv/view/content/txt_lb')
  27. emTxtLbNode: cc.Node
  28. @node('e_txt/cl_btn')
  29. claimBtnNode: cc.Node
  30. @node('e_txt/del_btn')
  31. delBtnNode: cc.Node
  32. receiveAll: boolean = false
  33. /********************** 周期事件 **********************/
  34. onLoad() {}
  35. onEnable() {
  36. Mgr.net.add(msgCmd.cmd_get_mail_rsp, this, this.onGetMailRsp)
  37. Mgr.net.add(msgCmd.cmd_flag_mail_rsp, this, this.onFlagMailRsp)
  38. Mgr.net.add(msgCmd.cmd_get_mail_goods_rsp, this, this.onGetMailGoodsRsp)
  39. Mgr.net.add(msgCmd.cmd_del_mail_rsp, this, this.onDelMailRsp)
  40. Mgr.net.send(msgCmd.cmd_get_mail)
  41. }
  42. onShow(args, fromUI: number) {}
  43. onHide(): any {
  44. Mgr.event.removeAll(this)
  45. }
  46. initContentUI() {
  47. // 根据附件是否领取(或者是否有附件)判断激活哪个按钮
  48. let emData = this.selEmail
  49. if (null != emData) {
  50. this.claimBtnNode.active = !emData.receive
  51. this.delBtnNode.active = emData.receive || !emData.attach
  52. // 若有附件,根据领取与否,更新附件状态(是否置灰)
  53. if (emData.attach) {
  54. this.packageMail = Mgr.goods.getGoodsList(emData.attachment)
  55. this.emItemList.numItems = this.packageMail.length
  56. }
  57. }
  58. }
  59. initCurEmail() {
  60. let emCfg = MailConfig[this.selEmail.subject]
  61. // 设置右侧邮件标题
  62. let emTitleNode = cc.find('e_txt', this.node)
  63. ccUtils.setLabel(emCfg.title, emTitleNode, 'tit_lb')
  64. // 设置右侧邮件内容
  65. let str = emCfg.content
  66. if (str == '%s') {
  67. str = this.selEmail.body
  68. }
  69. this.emTxtLbNode.getComponent(i18nLabel).init(str, this.selEmail.body.split('^'))
  70. // 更新UI(附件图标+领取/删除按钮)状态
  71. this.initContentUI()
  72. }
  73. initEmailItem(node: cc.Node, index: number) {
  74. // 左侧邮件列表标题和时间初始化,加载上限1000
  75. if (undefined != this.emailData && index < 1000) {
  76. let emailListData = this.emailData[index]
  77. // 将邮件的唯一标识绑定到邮件列表Item node
  78. node['emID'] = emailListData.id
  79. // 设置被选中邮件背景框为放大的高亮框
  80. let itemSelNode = cc.find('it_s_bg', node)
  81. itemSelNode.active = this.selEmail.id == emailListData.id
  82. // itemSelNode.scale = itemSelNode.active ? 1.2 : 1
  83. // 根据邮件是否已读设置右上角红点显示与否 优化成领取后消失
  84. let itemRedDots = cc.find('red_dot', node)
  85. itemRedDots.active = this.isRedTip(emailListData, index)
  86. // 若列表中的邮件已读,且未被选中,将邮件的默认背景框置灰
  87. let itemUnSelNode = cc.find('it_uns_bg', node)
  88. ccUtils.setSpriteGray(emailListData.read, itemUnSelNode)
  89. // 邮件主题设置
  90. let emCfg = MailConfig[emailListData.subject]
  91. ccUtils.setLabel(emCfg.title, node, 'e_tit_lb')
  92. // 邮件时间设置 乘以1000转成毫秒
  93. let emTime = Date.Format('MM/dd/yyyy hh:mm:ss', emailListData.date * 1000)
  94. ccUtils.setLabel(emTime, node, 'time_lb')
  95. }
  96. }
  97. isRedTip(email: mail, index) {
  98. return (!email.read && 0 != index && !email.attach) || (!email.receive && email.attach)
  99. }
  100. resetList() {
  101. // this.emailData.sort((a, b) => {
  102. // return (
  103. // Number(this.isRedTip(b, this.emailData.indexOf(b))) -
  104. // Number(this.isRedTip(a, this.emailData.indexOf(a)))
  105. // )
  106. // })
  107. this.emailList.numItems = this.emailData.length
  108. }
  109. initPackageItem(node: cc.Node, index: number) {
  110. // 单封邮件最大附件数50
  111. Mgr.goods.initOneGoods(this.packageMail[index], cc.find('goods', node), this)
  112. // 更新附件状态,若已领取,附件图标置灰
  113. ccUtils.setAllGray(this.selEmail.receive, node)
  114. }
  115. initMainEmailRed() {
  116. Data.main.emailRed = this.emailData.some(value => !value.read || (!value.receive && value.attach))
  117. }
  118. /********************** 网络事件 **********************/
  119. onGetMailRsp(data: getMailRsp) {
  120. this.emailData = data.list
  121. this.selEmail = this.emailData[0]
  122. // 没有邮件时,右侧内容界面置空
  123. let isEmpty = 0 == this.emailData.length
  124. cc.find('emp_lb', this.node).active = isEmpty
  125. cc.find('e_txt', this.node).active = !isEmpty
  126. // 若第一封邮件未读,给服务器发送已读标记信息
  127. if (!isEmpty) {
  128. if (!this.emailData[0].read) {
  129. Mgr.net.send(msgCmd.cmd_flag_mail, {list: [this.selEmail.id]})
  130. }
  131. // 初始化邮件列表第一封邮件UI状态
  132. this.initCurEmail()
  133. }
  134. this.resetList()
  135. }
  136. onFlagMailRsp() {
  137. if (this.selEmail) {
  138. this.selEmail.read = true
  139. this.resetList()
  140. // 如果当前标记已读的邮件没有附件,需要更新领取按钮为删除按钮
  141. this.initContentUI()
  142. }
  143. this.initMainEmailRed()
  144. }
  145. onGetMailGoodsRsp(data: getMailGoodsRsp, rewardNty: IRewardNty) {
  146. Mgr.ui.showReward(rewardNty)
  147. // 领取全部的时候,将有附件的邮件领取和已读标志置为true
  148. if (this.receiveAll) {
  149. this.emailData.forEach(v => {
  150. if (v.attach) {
  151. v.receive = true
  152. v.read = true
  153. }
  154. })
  155. } else {
  156. if (this.selEmail) {
  157. this.selEmail.receive = true
  158. }
  159. }
  160. this.resetList()
  161. this.initContentUI()
  162. Data.main.emailRed = false
  163. this.initMainEmailRed()
  164. }
  165. onDelMailRsp() {
  166. Mgr.net.send(msgCmd.cmd_get_mail)
  167. }
  168. /********************** 触发事件 **********************/
  169. /********************** 点击事件 **********************/
  170. onSelectedEmail(e) {
  171. this.selEmail = this.emailData.find(v => v.id == e.target['emID'])
  172. if (!this.selEmail.read) {
  173. Mgr.net.send(msgCmd.cmd_flag_mail, {list: [this.selEmail.id]})
  174. } else {
  175. this.emailList.numItems = this.emailData.length
  176. }
  177. this.initCurEmail()
  178. }
  179. onClaimClick() {
  180. this.receiveAll = false
  181. Mgr.net.send(msgCmd.cmd_get_mail_goods, {list: [this.selEmail.id]})
  182. }
  183. onDeleteClick() {
  184. Mgr.net.send(msgCmd.cmd_del_mail, {list: [this.selEmail.id]})
  185. }
  186. onClaimAllClick() {
  187. this.receiveAll = true
  188. // 获取所有未领取附件的邮件id并打包成对象(isGet false),发送全部领取的消息
  189. let emailIds = this.emailData.filter(obj => obj.attach && !obj.receive).map(obj => obj.id)
  190. if (emailIds.length > 0) {
  191. Mgr.net.send(msgCmd.cmd_get_mail_goods, {list: emailIds})
  192. }
  193. }
  194. onDeleteAllClick() {
  195. let emailIds = this.emailData.map(obj => obj.id)
  196. if (this.emailData.length > 0) {
  197. Mgr.net.send(msgCmd.cmd_del_mail, {list: emailIds})
  198. }
  199. }
  200. }