var t = require; var e = module; var o = exports; var e = (this && this.__decorate) || function(t, e, i, s) { var o, a = arguments.length, n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s); else for (var r = t.length - 1; 0 <= r; r--) (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n); return 3 < a && n && Object.defineProperty(e, i, n), n; }, a = (this && this.__awaiter) || function(t, n, r, l) { return new(r = r || Promise)(function(i, e) { function s(t) { try { a(l.next(t)); } catch (t) { e(t); } } function o(t) { try { a(l.throw(t)); } catch (t) { e(t); } } function a(t) { var e; t.done ? i(t.value) : ((e = t.value) instanceof r ? e : new r(function(t) { t(e); }) ).then(s, o); } a((l = l.apply(t, n || [])).next()); }); }; Object.defineProperty(o, "__esModule", { value: !0 }), (o.MailController = o.MailStatus = void 0); const l = t("App"), i = t("BaseController"), h = t("ControllerConst"), c = t("NotificationConst"), d = (t("ViewConst"), t("BagConst")), u = t("GameConst"), p = t("CurrencyService"), s = t("MailBoxView"), n = t("MailConst"), r = t("MailListView"); o.MailStatus = { new: 0, readed: 1, read_get: 3, geted: 2, deleted: 4 }; var { ccclass: t } = cc._decorator, t = e( [t], (t = class extends i.default { constructor() { super(), (this.saveData = null), (this.serverMailData = null), l.default.ViewManager.register1(r.default.VIEW_INFO, this), l.default.ViewManager.register1(s.default.VIEW_INFO, this), l.default.SaveManage.add(this, "mail", !1, !0), l.default.SaveManage.load("mail"), this.initServerMailInfo(), this.initFunc(); } createMemento(t) { return this.saveData; } setMemento(t, e) { t && t.id ? (this.saveData = t) : (this.saveData = { id: 1, localMailData: {} }); } initFunc() { this.registerFunc(n.MailConst.CREATE_LOCAL_MAIL, this.createLocalMail, this); } createLocalMail(t, e, i) { var s = l.default.DateUtils.secondNow(); this.saveData.id++; var o = -this.saveData.id; this.saveData.localMailData[o] = { isLocal: !0, id: o, title: t, content: e, expire_time: s + 2592e3, state: 0, create_time: s, awards: i }; } updateLocalMail() { var t, e = l.default.DateUtils.secondNow(); for (t in this.saveData.localMailData) this.saveData.localMailData[t].expire_time <= e && (this.saveData.localMailData[t] = null); } getLocalMail() { return this.updateLocalMail(), this.saveData.localMailData; } initServerMailInfo() { return a(this, void 0, void 0, function*() { this.serverMailData = {}; let t = 5; do { // var e = yield l.default.Http.requestAsync( // l.default.ConfigManager.gameConf.serverInfos.interface + "/api/emails", // {api_token: l.default.GameDataMsr.playerInfo.token}, // "GET" // ), console.log('123456'); var e = { "code": 0, "message": "SUCCESS", "data": [] }; if ((console.log("后台返回邮件信息:", e), 0 === e.code)) { if (e.data && e.data.length) for (const t of e.data) this.serverMailData[t.id] = t; break; } } while ((t--, 0 < t)); }); } getMailInfo() { return a(this, void 0, void 0, function*() { var t = yield l.default.Http.requestAsync( l.default.ConfigManager.gameConf.serverInfos.interface + "/api/emails", { api_token: l.default.GameDataMsr.playerInfo.token }, "GET" ), t = JSON.parse(t); return console.log("后台返回邮件信息:", t), t.data && t.data.length ? t.data : null; }); } readMailById(i) { return a(this, void 0, void 0, function*() { if (this.serverMailData[i] && this.serverMailData[i].awards) return this.serverMailData[i]; const t = this.saveData.localMailData[i]; if (t) return (t.state = 1), t; var e = yield l.default.Http.requestAsync( l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/read/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token }, "GET" ), e = JSON.parse(e); return 0 == e.code ? ((this.serverMailData[e.data.id] = e.data), e.data) : null; }); } gainMailById(i) { return a(this, void 0, void 0, function*() { const t = this.saveData.localMailData[i]; if (t) return (0 === t.state || 1 === t.state) && ((t.state = o.MailStatus.read_get), !0); var e = yield l.default.Http.requestAsync( l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/receive/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token }, "GET" ); return 0 == JSON.parse(e).code && ((this.serverMailData[i].state = o.MailStatus.read_get), !0); }); } gainAndSenReward(r, t = !0) { return a(this, void 0, void 0, function*() { let a = this.saveData.localMailData[r] || this.serverMailData[r]; if (a.state == o.MailStatus.read_get || a.state == o.MailStatus.geted) return null; if ((a.awards || (a = yield this.readMailById(a.id)), !(yield this.gainMailById(r)))) return null; const n = []; if (a.awards) for (let o = 0; o < a.awards.length; o++) { let t = Number(a.awards[o].item), e = Number(a.awards[o].number), i = l.default.ControllerManager.applyFunc( h.ControllerConst.Item, d.BagConst.GET_ITEM, t ), s = new u.CongratulationData(); if (i.isCostume()) { l.default.ControllerManager.applyFunc( h.ControllerConst.Item, d.BagConst.ADD_TIME, t, e ); const r = l.default.ControllerManager.applyFunc( h.ControllerConst.Item, d.BagConst.USE_ITEM, t, e ); s.data = r[0]; } else i.data.itemType == d.ItemType.SUPER_CASH ? p.default.ins.diamondChange(e) : l.default.ControllerManager.applyFunc( h.ControllerConst.Item, d.BagConst.ADD_TIME, t, e ), (s.data = i); (s.cnt = e), n.push(s); } return ( t && l.default.Platform.saveData(), t && n.length && l.default.ViewManager.open(31, n), l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST), n ); }); } delMailById(i, s = !1) { return a(this, void 0, void 0, function*() { const t = this.saveData.localMailData[i]; if (t) return (t.state = 4), (this.saveData.localMailData[i] = null), this.updateDelMail(), !0; if (s) return !1; var e = yield l.default.Http.requestAsync( l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/delete/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token }, "GET" ); return ( 0 == JSON.parse(e).code && (this.serverMailData[i] && (this.serverMailData[i] = null), this.updateDelMail(), !0) ); }); } delAllMailByReaded() { return a(this, void 0, void 0, function*() { for (var t in this.serverMailData) { t = this.serverMailData[t]; (t.state != o.MailStatus.geted && t.state != o.MailStatus.read_get) || (yield this.delMailById(t.id, !1)); } for (var e in this.saveData.localMailData) { e = this.saveData.localMailData[e]; (e.state != o.MailStatus.geted && e.state != o.MailStatus.read_get) || this.delMailById(e.id, !0); } this.updateDelMail(); }); } updateDelMail() { let t = {}; for (var e in this.serverMailData) { var i = this.serverMailData[e]; i && (t[e] = i); } for (var s in ((this.serverMailData = t), this.saveData.localMailData)) { var o = this.saveData.localMailData[s]; o && (t[s] = o); } this.saveData.localMailData = {}; } gainAllMailReward() { return a(this, void 0, void 0, function*() { let t = []; for (var e in this.saveData.localMailData) { const s = this.saveData.localMailData[e]; (s.state != o.MailStatus.new && s.state != o.MailStatus.readed) || ((s.state = o.MailStatus.read_get), (e = yield this.gainAndSenReward(s.id, !1)), (t = t.concat(e))); } for (var i in this.serverMailData) { i = this.serverMailData[i]; (i.state != o.MailStatus.new && i.state != o.MailStatus.readed) || ((i = yield this.gainAndSenReward(i.id, !1)) && (t = t.concat(i))); } l.default.Platform.saveData(), l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST), t.length && l.default.ViewManager.open(31, t); }); } }) ); o.MailController = t;