mailbox.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { _decorator, Component, instantiate, Node, Prefab, ScrollView } 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')
  11. export class mailbox 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_scrollView:Node = null
  16. @property(Node) content:Node = null
  17. @property(Prefab) read_item:Prefab = null
  18. @property(Node) btn_delete_read:Node = null
  19. @property(Node) btn_get_all:Node = null
  20. @property(Node) details_bg:Node = null
  21. private data_list:mail_item_data[] = []
  22. private page:number = 1
  23. private limit:number = 15
  24. private is_loading:boolean = false
  25. private is_request:boolean = true
  26. start() {
  27. this.onButtonListen(this.btn_back, ()=>{
  28. this.close()
  29. })
  30. this.onButtonListen(this.btn_delete_read, ()=>{
  31. uiManager.showModal('删除已读?', '', (res)=>{
  32. if(res.confirm) {
  33. this.requestDelete(1,0, ()=>{
  34. this.requestListData(false,()=>{
  35. uiManager.showToast('删除成功')
  36. })
  37. })
  38. }
  39. })
  40. })
  41. this.onButtonListen(this.btn_get_all, ()=>{
  42. uiManager.showModal('全部领取?', '', (res)=>{
  43. if(res.confirm) {
  44. this.requestRetrieve(1,0,()=>{
  45. this.requestListData(false, ()=>{
  46. uiManager.showToast('领取成功')
  47. })
  48. })
  49. }
  50. })
  51. })
  52. this.content_scrollView.on(ScrollView.EventType.SCROLL_TO_BOTTOM, this.onScrollToBottom, this)
  53. this.content_bg.active = false
  54. this.img_empty.active = false
  55. this.requestListData(false)
  56. }
  57. private onScrollToBottom() {
  58. if(this.is_request==false) {
  59. return
  60. }
  61. if(this.is_loading) {
  62. return
  63. }
  64. console.log('滑动到底部开始加载数据=',this.page)
  65. this.requestListData(true)
  66. }
  67. private showEmpty(show:boolean) {
  68. if(show) {
  69. this.content_bg.active = false
  70. this.img_empty.active = true
  71. } else {
  72. this.content_bg.active = true
  73. this.img_empty.active = false
  74. }
  75. }
  76. private requestListData(is_load_more:boolean, suc_cb=null) {
  77. if(is_load_more==false) {
  78. this.page = 1
  79. }
  80. this.is_loading = true
  81. uiManager.Instance().showLoading()
  82. let opt = {'page':this.page, 'limit':this.limit}
  83. http.post(config.API.mail_lists,opt, (err,d)=>{
  84. setTimeout(()=>{
  85. this.is_loading = false
  86. uiManager.Instance().hideLoading()
  87. },500)
  88. if(!err){
  89. let nd = JSON.parse(d)
  90. if(nd.code === config.status.SUCCESS){
  91. // console.log("mail data", nd.content)
  92. if(this.page==1) {
  93. this.data_list = []
  94. this.content.removeAllChildren()
  95. }
  96. this.data_list = this.data_list.concat(nd.content)
  97. if(this.data_list.length > 0) {
  98. this.showEmpty(false)
  99. for (let index = 0; index < this.data_list.length; index++) {
  100. const element = this.data_list[index];
  101. let item = instantiate(this.read_item)
  102. item.parent = this.content
  103. item.getComponent(mailbox_read_item).initView(element,this.onClickItem.bind(this))
  104. }
  105. this.page += 1
  106. if(nd.content.length<this.limit) {
  107. this.is_request = false
  108. }
  109. } else {
  110. this.is_request = false
  111. this.showEmpty(true)
  112. }
  113. }
  114. suc_cb && suc_cb()
  115. }
  116. })
  117. }
  118. private onClickItem(item:mailbox_read_item) {
  119. this.details_bg.active = true
  120. this.details_bg.getComponent(mailbox_details).initView(item.getData(),
  121. (v:mailbox_details)=>{
  122. uiManager.showModal('删除已读?', '', (res)=>{
  123. if(res.confirm) {
  124. this.details_bg.active = false
  125. this.requestDelete(0,v.getData().id, ()=>{
  126. this.deleteDataLocalHandle(item)
  127. })
  128. }
  129. })
  130. }, (v:mailbox_details)=>{
  131. this.details_bg.active = false
  132. let data = v.getData()
  133. if(data.state==0) {
  134. this.requestRetrieve(0,v.getData().id, ()=>{
  135. data.state = 1
  136. item.setData(data)
  137. })
  138. }
  139. })
  140. }
  141. // 领取邮件 stype 0:普通领取 1:一键领取
  142. private requestRetrieve(stype:number, id:number, success_cb) {
  143. let opt = {'stype':stype, 'id':id}
  144. uiManager.Instance().showLoading('正在领取...')
  145. http.post(config.API.mail_retrieve,opt, (err,d)=>{
  146. uiManager.Instance().hideLoading()
  147. if(!err){
  148. let nd = JSON.parse(d)
  149. if(nd.code === config.status.SUCCESS){
  150. success_cb && success_cb()
  151. }
  152. }
  153. })
  154. }
  155. // 删除邮件 stype 0:普通删除 1:一键删除
  156. private requestDelete(stype:number, id:number, success_cb) {
  157. let opt = {'stype':stype, 'id':id}
  158. uiManager.Instance().showLoading('正在删除...')
  159. http.post(config.API.mail_del,opt, (err,d)=>{
  160. uiManager.Instance().hideLoading()
  161. if(!err){
  162. let nd = JSON.parse(d)
  163. if(nd.code === config.status.SUCCESS){
  164. success_cb && success_cb()
  165. }
  166. }
  167. })
  168. }
  169. private deleteDataLocalHandle(item:mailbox_read_item) {
  170. if(item.node.isChildOf(this.content)) {
  171. this.content.removeChild(item.node)
  172. for (let index = 0; index < this.data_list.length; index++) {
  173. const element = this.data_list[index];
  174. if(element.id==item.getData().id) {
  175. this.data_list.splice(index,1)
  176. break
  177. }
  178. }
  179. if(this.data_list.length<=0) {
  180. this.showEmpty(true)
  181. }
  182. }
  183. }
  184. }