mailbox.ts 7.3 KB

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