Mail.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import ViewObject from "../../main/ViewObject";
  2. import { HttpStateType, ReveData } from "../../util/CHttp";
  3. import RedPoint from "../data/RedPoint";
  4. import SManage, { RewardData } from "../data/sdata/SManage";
  5. import MailInfo from "./MailInfo";
  6. import MailItem from "./MailItem";
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class Mail extends ViewObject {
  10. @property(cc.Node)
  11. mNull1: cc.Node = null;
  12. @property(cc.Node)
  13. mNull2: cc.Node = null;
  14. @property(cc.Node)
  15. mContent1: cc.Node = null;
  16. @property(cc.Node)
  17. mContent2: cc.Node = null;
  18. @property(cc.Prefab)
  19. mItem: cc.Prefab = null;
  20. @property(cc.Node)
  21. mInfoNode: cc.Node = null;//邮件详情节点
  22. @property(MailInfo)
  23. mMailInfo: MailInfo = null;
  24. @property(cc.Toggle)
  25. mToggle1: cc.Toggle = null;
  26. @property(cc.Toggle)
  27. mToggle2: cc.Toggle = null;
  28. itemList: cc.Node[] = [];
  29. clickIndex: number = 0;
  30. onLoad() {
  31. this.itemList = [];
  32. this.mNull1.active = false;
  33. this.mNull2.active = false;
  34. this.mInfoNode.active = false
  35. this.getMail();
  36. }
  37. /**
  38. *
  39. * @param prev 父界面
  40. */
  41. public show(prev?: ViewObject) {
  42. if (prev) {
  43. this.prev = prev;
  44. this.prev.__close();
  45. }
  46. this.main.viewManage.popView1(this.node);
  47. if (this.main && this.main.gameHttp) {
  48. this.main.gameHttp.pushEvent(this);
  49. }
  50. }
  51. private getMail() {
  52. this.main.gameHttp.sendJson('email/v1/data', {}, (state, reve: ReveData) => {
  53. this.main.stopLoad();
  54. if (state == HttpStateType.SUCCESS) {
  55. if (reve.retCode == 0) {
  56. this.initList(reve.data);
  57. } else {
  58. this.main.showTips(reve.message);
  59. }
  60. } else {
  61. this.main.showTips('网络异常');
  62. }
  63. });
  64. }
  65. private initList(list: Array<any>) {
  66. for (let i = 0; i < list.length; i++) {
  67. const element = list[i];
  68. let node = cc.instantiate(this.mItem);
  69. let item = node.getComponent(MailItem);
  70. this.itemList.push(node);
  71. item.init(element, i)
  72. if (!element.check && !element.receive) {
  73. RedPoint.addRed(this.main, node)
  74. }
  75. item.setCallback((mailItem: MailItem) => {
  76. RedPoint.removeRed(mailItem.node)
  77. this.openInfo(mailItem);
  78. this.refreshStatus(mailItem);
  79. });
  80. if (element.type == 1) {
  81. node.parent = this.mContent2;
  82. } else {
  83. node.parent = this.mContent1;
  84. }
  85. }
  86. if (this.mContent1.children.length > 0) {
  87. this.mNull1.active = false
  88. } else {
  89. this.mNull1.active = true
  90. }
  91. if (this.mContent2.children.length > 0) {
  92. this.mNull2.active = false
  93. } else {
  94. this.mNull2.active = true
  95. }
  96. }
  97. private openInfo(mailItem: MailItem) {
  98. if (this.mMailInfo.mailItem == mailItem) {
  99. return
  100. }
  101. this.mMailInfo.setMail(mailItem)
  102. this.mMailInfo.getMailInfo();
  103. }
  104. public onclickOnekey() {
  105. let list: Array<RewardData> = []
  106. this.onkeyGetMail(0, list, () => {
  107. this.main.showRewardList(list)
  108. })
  109. }
  110. /**
  111. * 一键领取所有邮件
  112. */
  113. public onkeyGetMail(index: number, list: Array<RewardData>, callback: () => void) {
  114. let content: cc.Node = null
  115. if (this.mToggle1.isChecked) {
  116. content = this.mContent1
  117. } else {
  118. content = this.mContent2
  119. }
  120. let nodes = content.children
  121. if (index >= nodes.length) {
  122. callback()
  123. return
  124. }
  125. let mailItem = nodes[index].getComponent(MailItem)
  126. if (mailItem.data.receive) {//已经领取了
  127. index++
  128. this.onkeyGetMail(index, list, callback)
  129. } else {
  130. this.getMailInfo(mailItem, (temps) => {
  131. if (temps) {
  132. for (let i = 0; i < temps.length; i++) {
  133. const element = temps[i];
  134. Mail.addReward(list, element)
  135. }
  136. }
  137. index++
  138. this.onkeyGetMail(index, list, callback)
  139. })
  140. }
  141. }
  142. private static addReward(list: Array<RewardData>, data: RewardData) {
  143. if (data.type == 1 || data.type == 2) {
  144. for (let i = 0; i < list.length; i++) {
  145. const element = list[i];
  146. if (element.type == data.type) {
  147. element.count += data.count
  148. return
  149. }
  150. }
  151. list.push(data)
  152. } else if (data.type == 3) {//道具
  153. list.push(data)
  154. } else {
  155. list.push(data)
  156. }
  157. }
  158. public getMailInfo(mailItem: MailItem, callback: (list) => void) {
  159. let msg = {
  160. mailId: mailItem.data.id
  161. }
  162. this.main.gameHttp.sendJson('email/v1/receive', msg, (state, reve: ReveData) => {
  163. this.main.stopLoad();
  164. if (state == HttpStateType.SUCCESS) {
  165. if (reve.retCode == 0) {
  166. mailItem.data.receive = true;
  167. mailItem.tipsIcon.active = false;
  168. RedPoint.removeRed(mailItem.node)
  169. if (this.mMailInfo.mailItem == mailItem) {
  170. this.mMailInfo.flush();
  171. }
  172. callback(this.main.sManage.getRewards(reve))
  173. } else {
  174. this.main.showTips(reve.message);
  175. callback(null)
  176. }
  177. } else {
  178. this.main.showTips('网络异常');
  179. callback(null)
  180. }
  181. });
  182. }
  183. public refreshStatus(mailItem: MailItem) {
  184. if (this.clickIndex == mailItem.index) {
  185. mailItem.node.color = cc.color(173, 173, 173, 255);
  186. } else {
  187. this.itemList.forEach(item => {
  188. if (item.getComponent(MailItem).index == this.clickIndex) {
  189. item.color = cc.color(255, 255, 255, 255);
  190. }
  191. })
  192. mailItem.node.color = cc.color(173, 173, 173, 255);
  193. this.clickIndex = mailItem.index;
  194. }
  195. }
  196. }