123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import ViewObject from "../../main/ViewObject";
- import { HttpStateType, ReveData } from "../../util/CHttp";
- import RedPoint from "../data/RedPoint";
- import SManage, { RewardData } from "../data/sdata/SManage";
- import MailInfo from "./MailInfo";
- import MailItem from "./MailItem";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class Mail extends ViewObject {
- @property(cc.Node)
- mNull1: cc.Node = null;
- @property(cc.Node)
- mNull2: cc.Node = null;
- @property(cc.Node)
- mContent1: cc.Node = null;
- @property(cc.Node)
- mContent2: cc.Node = null;
- @property(cc.Prefab)
- mItem: cc.Prefab = null;
- @property(cc.Node)
- mInfoNode: cc.Node = null;
- @property(MailInfo)
- mMailInfo: MailInfo = null;
- @property(cc.Toggle)
- mToggle1: cc.Toggle = null;
- @property(cc.Toggle)
- mToggle2: cc.Toggle = null;
- itemList: cc.Node[] = [];
- clickIndex: number = 0;
- onLoad() {
- this.itemList = [];
- this.mNull1.active = false;
- this.mNull2.active = false;
- this.mInfoNode.active = false
- this.getMail();
- }
-
- public show(prev?: ViewObject) {
- if (prev) {
- this.prev = prev;
- this.prev.__close();
- }
- this.main.viewManage.popView1(this.node);
- if (this.main && this.main.gameHttp) {
- this.main.gameHttp.pushEvent(this);
- }
- }
- private getMail() {
- this.main.gameHttp.sendJson('email/v1/data', {}, (state, reve: ReveData) => {
- this.main.stopLoad();
- if (state == HttpStateType.SUCCESS) {
- if (reve.retCode == 0) {
- this.initList(reve.data);
- } else {
- this.main.showTips(reve.message);
- }
- } else {
- this.main.showTips('网络异常');
- }
- });
- }
- private initList(list: Array<any>) {
- for (let i = 0; i < list.length; i++) {
- const element = list[i];
- let node = cc.instantiate(this.mItem);
- let item = node.getComponent(MailItem);
- this.itemList.push(node);
- item.init(element, i)
- if (!element.check && !element.receive) {
- RedPoint.addRed(this.main, node)
- }
- item.setCallback((mailItem: MailItem) => {
- RedPoint.removeRed(mailItem.node)
- this.openInfo(mailItem);
- this.refreshStatus(mailItem);
- });
- if (element.type == 1) {
- node.parent = this.mContent2;
- } else {
- node.parent = this.mContent1;
- }
- }
- if (this.mContent1.children.length > 0) {
- this.mNull1.active = false
- } else {
- this.mNull1.active = true
- }
- if (this.mContent2.children.length > 0) {
- this.mNull2.active = false
- } else {
- this.mNull2.active = true
- }
- }
- private openInfo(mailItem: MailItem) {
- if (this.mMailInfo.mailItem == mailItem) {
- return
- }
- this.mMailInfo.setMail(mailItem)
- this.mMailInfo.getMailInfo();
- }
- public onclickOnekey() {
- let list: Array<RewardData> = []
- this.onkeyGetMail(0, list, () => {
- this.main.showRewardList(list)
- })
- }
-
- public onkeyGetMail(index: number, list: Array<RewardData>, callback: () => void) {
- let content: cc.Node = null
- if (this.mToggle1.isChecked) {
- content = this.mContent1
- } else {
- content = this.mContent2
- }
- let nodes = content.children
- if (index >= nodes.length) {
- callback()
- return
- }
- let mailItem = nodes[index].getComponent(MailItem)
- if (mailItem.data.receive) {
- index++
- this.onkeyGetMail(index, list, callback)
- } else {
- this.getMailInfo(mailItem, (temps) => {
- if (temps) {
- for (let i = 0; i < temps.length; i++) {
- const element = temps[i];
- Mail.addReward(list, element)
- }
- }
- index++
- this.onkeyGetMail(index, list, callback)
- })
- }
- }
- private static addReward(list: Array<RewardData>, data: RewardData) {
- if (data.type == 1 || data.type == 2) {
- for (let i = 0; i < list.length; i++) {
- const element = list[i];
- if (element.type == data.type) {
- element.count += data.count
- return
- }
- }
- list.push(data)
- } else if (data.type == 3) {
- list.push(data)
- } else {
- list.push(data)
- }
- }
- public getMailInfo(mailItem: MailItem, callback: (list) => void) {
- let msg = {
- mailId: mailItem.data.id
- }
- this.main.gameHttp.sendJson('email/v1/receive', msg, (state, reve: ReveData) => {
- this.main.stopLoad();
- if (state == HttpStateType.SUCCESS) {
- if (reve.retCode == 0) {
- mailItem.data.receive = true;
- mailItem.tipsIcon.active = false;
- RedPoint.removeRed(mailItem.node)
- if (this.mMailInfo.mailItem == mailItem) {
- this.mMailInfo.flush();
- }
- callback(this.main.sManage.getRewards(reve))
- } else {
- this.main.showTips(reve.message);
- callback(null)
- }
- } else {
- this.main.showTips('网络异常');
- callback(null)
- }
- });
- }
- public refreshStatus(mailItem: MailItem) {
- if (this.clickIndex == mailItem.index) {
- mailItem.node.color = cc.color(173, 173, 173, 255);
- } else {
- this.itemList.forEach(item => {
- if (item.getComponent(MailItem).index == this.clickIndex) {
- item.color = cc.color(255, 255, 255, 255);
- }
- })
- mailItem.node.color = cc.color(173, 173, 173, 255);
- this.clickIndex = mailItem.index;
- }
- }
- }
|