mailbox.ts 8.0 KB

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