mailbox.ts 7.8 KB

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