MailController.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. console.log('123456');
  119. var e = { "code": 0, "message": "SUCCESS", "data": [] };
  120. if ((console.log("后台返回邮件信息:", e), 0 === e.code)) {
  121. if (e.data && e.data.length)
  122. for (const t of e.data) this.serverMailData[t.id] = t;
  123. break;
  124. }
  125. } while ((t--, 0 < t));
  126. });
  127. }
  128. getMailInfo() {
  129. return a(this, void 0, void 0, function*() {
  130. var t = yield l.default.Http.requestAsync(
  131. l.default.ConfigManager.gameConf.serverInfos.interface + "/api/emails", { api_token: l.default.GameDataMsr.playerInfo.token },
  132. "GET"
  133. ),
  134. t = JSON.parse(t);
  135. return console.log("后台返回邮件信息:", t), t.data && t.data.length ? t.data : null;
  136. });
  137. }
  138. readMailById(i) {
  139. return a(this, void 0, void 0, function*() {
  140. if (this.serverMailData[i] && this.serverMailData[i].awards) return this.serverMailData[i];
  141. const t = this.saveData.localMailData[i];
  142. if (t) return (t.state = 1), t;
  143. var e = yield l.default.Http.requestAsync(
  144. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/read/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  145. "GET"
  146. ),
  147. e = JSON.parse(e);
  148. return 0 == e.code ? ((this.serverMailData[e.data.id] = e.data), e.data) : null;
  149. });
  150. }
  151. gainMailById(i) {
  152. return a(this, void 0, void 0, function*() {
  153. const t = this.saveData.localMailData[i];
  154. if (t) return (0 === t.state || 1 === t.state) && ((t.state = o.MailStatus.read_get), !0);
  155. var e = yield l.default.Http.requestAsync(
  156. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/receive/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  157. "GET"
  158. );
  159. return 0 == JSON.parse(e).code && ((this.serverMailData[i].state = o.MailStatus.read_get), !0);
  160. });
  161. }
  162. gainAndSenReward(r, t = !0) {
  163. return a(this, void 0, void 0, function*() {
  164. let a = this.saveData.localMailData[r] || this.serverMailData[r];
  165. if (a.state == o.MailStatus.read_get || a.state == o.MailStatus.geted) return null;
  166. if ((a.awards || (a = yield this.readMailById(a.id)), !(yield this.gainMailById(r)))) return null;
  167. const n = [];
  168. if (a.awards)
  169. for (let o = 0; o < a.awards.length; o++) {
  170. let t = Number(a.awards[o].item),
  171. e = Number(a.awards[o].number),
  172. i = l.default.ControllerManager.applyFunc(
  173. h.ControllerConst.Item,
  174. d.BagConst.GET_ITEM,
  175. t
  176. ),
  177. s = new u.CongratulationData();
  178. if (i.isCostume()) {
  179. l.default.ControllerManager.applyFunc(
  180. h.ControllerConst.Item,
  181. d.BagConst.ADD_TIME,
  182. t,
  183. e
  184. );
  185. const r = l.default.ControllerManager.applyFunc(
  186. h.ControllerConst.Item,
  187. d.BagConst.USE_ITEM,
  188. t,
  189. e
  190. );
  191. s.data = r[0];
  192. } else
  193. i.data.itemType == d.ItemType.SUPER_CASH ?
  194. p.default.ins.diamondChange(e) :
  195. l.default.ControllerManager.applyFunc(
  196. h.ControllerConst.Item,
  197. d.BagConst.ADD_TIME,
  198. t,
  199. e
  200. ),
  201. (s.data = i);
  202. (s.cnt = e), n.push(s);
  203. }
  204. return (
  205. t && l.default.Platform.saveData(),
  206. t && n.length && l.default.ViewManager.open(31, n),
  207. l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST),
  208. n
  209. );
  210. });
  211. }
  212. delMailById(i, s = !1) {
  213. return a(this, void 0, void 0, function*() {
  214. const t = this.saveData.localMailData[i];
  215. if (t) return (t.state = 4), (this.saveData.localMailData[i] = null), this.updateDelMail(), !0;
  216. if (s) return !1;
  217. var e = yield l.default.Http.requestAsync(
  218. l.default.ConfigManager.gameConf.serverInfos.interface + `/api/email/delete/${i}`, { api_token: l.default.GameDataMsr.playerInfo.token },
  219. "GET"
  220. );
  221. return (
  222. 0 == JSON.parse(e).code &&
  223. (this.serverMailData[i] && (this.serverMailData[i] = null), this.updateDelMail(), !0)
  224. );
  225. });
  226. }
  227. delAllMailByReaded() {
  228. return a(this, void 0, void 0, function*() {
  229. for (var t in this.serverMailData) {
  230. t = this.serverMailData[t];
  231. (t.state != o.MailStatus.geted && t.state != o.MailStatus.read_get) ||
  232. (yield this.delMailById(t.id, !1));
  233. }
  234. for (var e in this.saveData.localMailData) {
  235. e = this.saveData.localMailData[e];
  236. (e.state != o.MailStatus.geted && e.state != o.MailStatus.read_get) ||
  237. this.delMailById(e.id, !0);
  238. }
  239. this.updateDelMail();
  240. });
  241. }
  242. updateDelMail() {
  243. let t = {};
  244. for (var e in this.serverMailData) {
  245. var i = this.serverMailData[e];
  246. i && (t[e] = i);
  247. }
  248. for (var s in ((this.serverMailData = t), this.saveData.localMailData)) {
  249. var o = this.saveData.localMailData[s];
  250. o && (t[s] = o);
  251. }
  252. this.saveData.localMailData = {};
  253. }
  254. gainAllMailReward() {
  255. return a(this, void 0, void 0, function*() {
  256. let t = [];
  257. for (var e in this.saveData.localMailData) {
  258. const s = this.saveData.localMailData[e];
  259. (s.state != o.MailStatus.new && s.state != o.MailStatus.readed) ||
  260. ((s.state = o.MailStatus.read_get),
  261. (e = yield this.gainAndSenReward(s.id, !1)),
  262. (t = t.concat(e)));
  263. }
  264. for (var i in this.serverMailData) {
  265. i = this.serverMailData[i];
  266. (i.state != o.MailStatus.new && i.state != o.MailStatus.readed) ||
  267. ((i = yield this.gainAndSenReward(i.id, !1)) && (t = t.concat(i)));
  268. }
  269. l.default.Platform.saveData(),
  270. l.default.NotificationCenter.dispatch(c.NotificationConst.UPDATE_MAIL_LIST),
  271. t.length && l.default.ViewManager.open(31, t);
  272. });
  273. }
  274. })
  275. );
  276. o.MailController = t;