mailbox.ts 7.5 KB

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