mailbox.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { _decorator, Component, instantiate, Node, Prefab, ScrollView, sys } 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. if(stype==1&&tools.user_red_dot_data.mail_unread_number==0) {
  149. return uiManager.showToast('邮件已全部领取')
  150. }
  151. let opt = {'stype':stype, 'id':id}
  152. uiManager.Instance().showLoading('正在领取...')
  153. http.post(config.API.mail_retrieve,opt, (err,d)=>{
  154. uiManager.Instance().hideLoading()
  155. if(!err){
  156. let nd = JSON.parse(d)
  157. if(nd.code === config.status.SUCCESS){
  158. let list = nd.content
  159. uiManager.Instance().showUi(config.UI.ui_reward_tips_view, null, (node:Node)=>{
  160. node.getComponent(reward_tips_view).initView(list)
  161. success_cb && success_cb()
  162. })
  163. this.retrieveLaterRedDotHandle(stype)
  164. }
  165. }
  166. })
  167. }
  168. private retrieveLaterRedDotHandle(stype:number) {
  169. if(stype==1) {
  170. tools.user_red_dot_data.mail_unread_number = 0
  171. } else {
  172. tools.user_red_dot_data.mail_unread_number -=1
  173. }
  174. ClientEvent.dispatchEvent(config.UI_EVENT.UPDATE_RED_DOT_STATUS,config.RED_DOT_TYPE.mail)
  175. }
  176. // 删除邮件 stype 0:普通删除 1:一键删除
  177. private requestDelete(stype:number, id:number, success_cb) {
  178. let opt = {'stype':stype, 'id':id}
  179. uiManager.Instance().showLoading('正在删除...')
  180. http.post(config.API.mail_del,opt, (err,d)=>{
  181. uiManager.Instance().hideLoading()
  182. if(!err){
  183. let nd = JSON.parse(d)
  184. if(nd.code === config.status.SUCCESS){
  185. success_cb && success_cb()
  186. }
  187. }
  188. })
  189. }
  190. private deleteDataLocalHandle(item:mailbox_read_item) {
  191. if(item.node.isChildOf(this.content)) {
  192. this.content.removeChild(item.node)
  193. for (let index = 0; index < this.data_list.length; index++) {
  194. const element = this.data_list[index];
  195. if(element.id==item.getData().id) {
  196. this.data_list.splice(index,1)
  197. break
  198. }
  199. }
  200. if(this.data_list.length<=0) {
  201. this.showEmpty(true)
  202. }
  203. }
  204. }
  205. }