mailbox_view.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
  2. import { base_ui } from '../../fw/base_ui';
  3. import { mailbox_read_item } from './mailbox_read_item';
  4. import { http } from '../../http';
  5. import { config } from '../../config';
  6. import { mail_item_data } from '../../data';
  7. import { uiManager } from '../../manager/uiManager';
  8. import { mailbox_details } from './mailbox_details';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('mailbox_view')
  11. export class mailbox_view extends base_ui {
  12. @property(Node) btn_back:Node = null
  13. @property(Node) img_empty:Node = null
  14. @property(Node) content_bg:Node = null
  15. @property(Node) content:Node = null
  16. @property(Prefab) read_item:Prefab = null
  17. @property(Node) btn_delete_read:Node = null
  18. @property(Node) btn_get_all:Node = null
  19. @property(Node) details_bg:Node = null
  20. private data_list:mail_item_data[] = []
  21. private page:number = 1
  22. private limit:number = 15
  23. start() {
  24. this.onButtonListen(this.btn_back, ()=>{
  25. this.close()
  26. })
  27. this.onButtonListen(this.btn_delete_read, ()=>{
  28. uiManager.showModal('删除已读?', '', (res)=>{
  29. if(res.confirm) {
  30. this.requestDelete(1,0, ()=>{
  31. this.requestListData(()=>{
  32. uiManager.showToast('删除成功')
  33. })
  34. })
  35. }
  36. })
  37. })
  38. this.onButtonListen(this.btn_get_all, ()=>{
  39. uiManager.showModal('全部领取?', '', (res)=>{
  40. if(res.confirm) {
  41. this.requestRetrieve(1,0,()=>{
  42. this.requestListData(()=>{
  43. uiManager.showToast('领取成功')
  44. })
  45. })
  46. }
  47. })
  48. })
  49. this.requestListData()
  50. }
  51. private showEmpty(show:boolean) {
  52. if(show) {
  53. this.content_bg.active = false
  54. this.img_empty.active = true
  55. } else {
  56. this.content_bg.active = true
  57. this.img_empty.active = false
  58. }
  59. }
  60. private requestListData(suc_cb=null) {
  61. let opt = {'page':this.page, 'limit':this.limit}
  62. http.post(config.API.mail_lists,opt, (err,d)=>{
  63. if(!err){
  64. let nd = JSON.parse(d)
  65. if(nd.code === config.status.SUCCESS){
  66. // console.log("mail data", nd.content)
  67. this.data_list = []
  68. this.content.removeAllChildren()
  69. this.data_list = this.data_list.concat(nd.content)
  70. if(this.data_list.length > 0) {
  71. this.showEmpty(false)
  72. for (let index = 0; index < this.data_list.length; index++) {
  73. const element = this.data_list[index];
  74. let item = instantiate(this.read_item)
  75. item.parent = this.content
  76. item.getComponent(mailbox_read_item).initView(element,this.onClickItem.bind(this))
  77. }
  78. } else {
  79. this.showEmpty(true)
  80. }
  81. }
  82. suc_cb && suc_cb()
  83. }
  84. })
  85. }
  86. private onClickItem(item:mailbox_read_item) {
  87. this.details_bg.active = true
  88. this.details_bg.getComponent(mailbox_details).initView(item.getData(),
  89. (v:mailbox_details)=>{
  90. uiManager.showModal('删除已读?', '', (res)=>{
  91. if(res.confirm) {
  92. this.details_bg.active = false
  93. this.requestDelete(0,v.getData().id, ()=>{
  94. this.deleteDataLocalHandle(item)
  95. })
  96. }
  97. })
  98. }, (v:mailbox_details)=>{
  99. this.details_bg.active = false
  100. let data = v.getData()
  101. if(data.state==0) {
  102. this.requestRetrieve(0,v.getData().id, ()=>{
  103. data.state = 1
  104. item.setData(data)
  105. })
  106. }
  107. })
  108. }
  109. // 领取邮件 stype 0:普通领取 1:一键领取
  110. private requestRetrieve(stype:number, id:number, success_cb) {
  111. let opt = {'stype':stype, 'id':id}
  112. uiManager.Instance().showLoading('正在领取...')
  113. http.post(config.API.mail_retrieve,opt, (err,d)=>{
  114. uiManager.Instance().hideLoading()
  115. if(!err){
  116. let nd = JSON.parse(d)
  117. if(nd.code === config.status.SUCCESS){
  118. success_cb && success_cb()
  119. }
  120. }
  121. })
  122. }
  123. // 删除邮件 stype 0:普通删除 1:一键删除
  124. private requestDelete(stype:number, id:number, success_cb) {
  125. let opt = {'stype':stype, 'id':id}
  126. uiManager.Instance().showLoading('正在删除...')
  127. http.post(config.API.mail_del,opt, (err,d)=>{
  128. uiManager.Instance().hideLoading()
  129. if(!err){
  130. let nd = JSON.parse(d)
  131. if(nd.code === config.status.SUCCESS){
  132. success_cb && success_cb()
  133. }
  134. }
  135. })
  136. }
  137. private deleteDataLocalHandle(item:mailbox_read_item) {
  138. if(item.node.isChildOf(this.content)) {
  139. this.content.removeChild(item.node)
  140. for (let index = 0; index < this.data_list.length; index++) {
  141. const element = this.data_list[index];
  142. if(element.id==item.getData().id) {
  143. this.data_list.splice(index,1)
  144. break
  145. }
  146. }
  147. if(this.data_list.length<=0) {
  148. this.showEmpty(true)
  149. }
  150. }
  151. }
  152. }