MailController.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. var t = require;
  2. var e = module;
  3. var o = exports;
  4. var e =
  5. (this && this.__decorate) ||
  6. function(t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. },
  16. a =
  17. (this && this.__awaiter) ||
  18. function(t, n, r, l) {
  19. return new(r = r || Promise)(function(i, e) {
  20. function s(t) {
  21. try {
  22. a(l.next(t));
  23. } catch (t) {
  24. e(t);
  25. }
  26. }
  27. function o(t) {
  28. try {
  29. a(l.throw(t));
  30. } catch (t) {
  31. e(t);
  32. }
  33. }
  34. function a(t) {
  35. var e;
  36. t.done ?
  37. i(t.value) :
  38. ((e = t.value) instanceof r ?
  39. e :
  40. new r(function(t) {
  41. t(e);
  42. })
  43. ).then(s, o);
  44. }
  45. a((l = l.apply(t, n || [])).next());
  46. });
  47. };
  48. Object.defineProperty(o, "__esModule", { value: !0 }), (o.MailController = o.MailStatus = void 0);
  49. const l = t("App"),
  50. i = t("BaseController"),
  51. h = t("ControllerConst"),
  52. c = t("NotificationConst"),
  53. d = (t("ViewConst"), t("BagConst")),
  54. u = t("GameConst"),
  55. p = t("CurrencyService"),
  56. s = t("MailBoxView"),
  57. n = t("MailConst"),
  58. r = t("MailListView");
  59. o.MailStatus = { new: 0, readed: 1, read_get: 3, geted: 2, deleted: 4 };
  60. var { ccclass: t } = cc._decorator,
  61. t = e(
  62. [t],
  63. (t = class extends i.default {
  64. constructor() {
  65. super(),
  66. (this.saveData = null),
  67. (this.serverMailData = null),
  68. l.default.ViewManager.register1(r.default.VIEW_INFO, this),
  69. l.default.ViewManager.register1(s.default.VIEW_INFO, this),
  70. l.default.SaveManage.add(this, "mail", !1, !0),
  71. l.default.SaveManage.load("mail"),
  72. this.initServerMailInfo(),
  73. this.initFunc();
  74. }
  75. createMemento(t) {
  76. return this.saveData;
  77. }
  78. setMemento(t, e) {
  79. t && t.id ? (this.saveData = t) : (this.saveData = { id: 1, localMailData: {} });
  80. }
  81. initFunc() {
  82. this.registerFunc(n.MailConst.CREATE_LOCAL_MAIL, this.createLocalMail, this);
  83. }
  84. createLocalMail(t, e, i) {
  85. var s = l.default.DateUtils.secondNow();
  86. this.saveData.id++;
  87. var o = -this.saveData.id;
  88. this.saveData.localMailData[o] = {
  89. isLocal: !0,
  90. id: o,
  91. title: t,
  92. content: e,
  93. expire_time: s + 2592e3,
  94. state: 0,
  95. create_time: s,
  96. awards: i
  97. };
  98. }
  99. updateLocalMail() {
  100. var t,
  101. e = l.default.DateUtils.secondNow();
  102. for (t in this.saveData.localMailData)
  103. this.saveData.localMailData[t].expire_time <= e && (this.saveData.localMailData[t] = null);
  104. }
  105. getLocalMail() {
  106. return this.updateLocalMail(), this.saveData.localMailData;
  107. }
  108. initServerMailInfo() {
  109. return a(this, void 0, void 0, function*() {
  110. this.serverMailData = {};
  111. let t = 5;
  112. do {
  113. // var e = yield l.default.Http.requestAsync(
  114. // l.default.ConfigManager.gameConf.serverInfos.interface + "/api/emails",
  115. // {api_token: l.default.GameDataMsr.playerInfo.token},
  116. // "GET"
  117. // ),
  118. var e = { "code": 0, "message": "SUCCESS", "data": [] };
  119. if ((console.log("后台返回邮件信息:", e), 0 === e.code)) {
  120. if (e.data && e.data.length)
  121. for (const t of e.data) this.serverMailData[t.id] = t;
  122. break;
  123. }
  124. } while ((t--, 0 < t));
  125. });
  126. }
  127. getMailInfo() {
  128. return a(this, void 0, void 0, function*() {
  129. var t = yield l.default.Http.requestAsync(
  130. l.default.ConfigManager.gameConf.serverInfos.interface + "/api/emails", { api_token: l.default.GameDataMsr.playerInfo.token },
  131. "GET"
  132. ),
  133. t = JSON.parse(t);
  134. return console.log("后台返回邮件信息:", t), t.data && t.data.length ? t.data : null;
  135. });
  136. }
  137. readMailById(i) {
  138. return a(this, void 0, void 0, function*() {
  139. if (this.serverMailData[i] && this.serverMailData[i].awards) return this.serverMailData[i];
  140. const t = this.saveData.localMailData[i];
  141. if (t) return (t.state = 1), t;
  142. var e = yield l.default.Http.requestAsync(
  143. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/read/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  144. "GET"
  145. ),
  146. e = JSON.parse(e);
  147. return 0 == e.code ? ((this.serverMailData[e.data.id] = e.data), e.data) : null;
  148. });
  149. }
  150. gainMailById(i) {
  151. return a(this, void 0, void 0, function*() {
  152. const t = this.saveData.localMailData[i];
  153. if (t) return (0 === t.state || 1 === t.state) && ((t.state = o.MailStatus.read_get), !0);
  154. var e = yield l.default.Http.requestAsync(
  155. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/receive/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  156. "GET"
  157. );
  158. return 0 == JSON.parse(e).code && ((this.serverMailData[i].state = o.MailStatus.read_get), !0);
  159. });
  160. }
  161. gainAndSenReward(r, t = !0) {
  162. return a(this, void 0, void 0, function*() {
  163. let a = this.saveData.localMailData[r] || this.serverMailData[r];
  164. if (a.state == o.MailStatus.read_get || a.state == o.MailStatus.geted) return null;
  165. if ((a.awards || (a = yield this.readMailById(a.id)), !(yield this.gainMailById(r)))) return null;
  166. const n = [];
  167. if (a.awards)
  168. for (let o = 0; o < a.awards.length; o++) {
  169. let t = Number(a.awards[o].item),
  170. e = Number(a.awards[o].number),
  171. i = l.default.ControllerManager.applyFunc(
  172. h.ControllerConst.Item,
  173. d.BagConst.GET_ITEM,
  174. t
  175. ),
  176. s = new u.CongratulationData();
  177. if (i.isCostume()) {
  178. l.default.ControllerManager.applyFunc(
  179. h.ControllerConst.Item,
  180. d.BagConst.ADD_TIME,
  181. t,
  182. e
  183. );
  184. const r = l.default.ControllerManager.applyFunc(
  185. h.ControllerConst.Item,
  186. d.BagConst.USE_ITEM,
  187. t,
  188. e
  189. );
  190. s.data = r[0];
  191. } else
  192. i.data.itemType == d.ItemType.SUPER_CASH ?
  193. p.default.ins.diamondChange(e) :
  194. l.default.ControllerManager.applyFunc(
  195. h.ControllerConst.Item,
  196. d.BagConst.ADD_TIME,
  197. t,
  198. e
  199. ),
  200. (s.data = i);
  201. (s.cnt = e), n.push(s);
  202. }
  203. return (
  204. t && l.default.Platform.saveData(),
  205. t && n.length && l.default.ViewManager.open(31, n),
  206. l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST),
  207. n
  208. );
  209. });
  210. }
  211. delMailById(i, s = !1) {
  212. return a(this, void 0, void 0, function*() {
  213. const t = this.saveData.localMailData[i];
  214. if (t) return (t.state = 4), (this.saveData.localMailData[i] = null), this.updateDelMail(), !0;
  215. if (s) return !1;
  216. var e = yield l.default.Http.requestAsync(
  217. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/delete/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  218. "GET"
  219. );
  220. return (
  221. 0 == JSON.parse(e).code &&
  222. (this.serverMailData[i] && (this.serverMailData[i] = null), this.updateDelMail(), !0)
  223. );
  224. });
  225. }
  226. delAllMailByReaded() {
  227. return a(this, void 0, void 0, function*() {
  228. for (var t in this.serverMailData) {
  229. t = this.serverMailData[t];
  230. (t.state != o.MailStatus.geted && t.state != o.MailStatus.read_get) ||
  231. (yield this.delMailById(t.id, !1));
  232. }
  233. for (var e in this.saveData.localMailData) {
  234. e = this.saveData.localMailData[e];
  235. (e.state != o.MailStatus.geted && e.state != o.MailStatus.read_get) ||
  236. this.delMailById(e.id, !0);
  237. }
  238. this.updateDelMail();
  239. });
  240. }
  241. updateDelMail() {
  242. let t = {};
  243. for (var e in this.serverMailData) {
  244. var i = this.serverMailData[e];
  245. i && (t[e] = i);
  246. }
  247. for (var s in ((this.serverMailData = t), this.saveData.localMailData)) {
  248. var o = this.saveData.localMailData[s];
  249. o && (t[s] = o);
  250. }
  251. this.saveData.localMailData = {};
  252. }
  253. gainAllMailReward() {
  254. return a(this, void 0, void 0, function*() {
  255. let t = [];
  256. for (var e in this.saveData.localMailData) {
  257. const s = this.saveData.localMailData[e];
  258. (s.state != o.MailStatus.new && s.state != o.MailStatus.readed) ||
  259. ((s.state = o.MailStatus.read_get),
  260. (e = yield this.gainAndSenReward(s.id, !1)),
  261. (t = t.concat(e)));
  262. }
  263. for (var i in this.serverMailData) {
  264. i = this.serverMailData[i];
  265. (i.state != o.MailStatus.new && i.state != o.MailStatus.readed) ||
  266. ((i = yield this.gainAndSenReward(i.id, !1)) && (t = t.concat(i)));
  267. }
  268. l.default.Platform.saveData(),
  269. l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST),
  270. t.length && l.default.ViewManager.open(31, t);
  271. });
  272. }
  273. })
  274. );
  275. o.MailController = t;