AlertMgr.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * 进用作参考,不需要继承该类
  3. * 主要目的:规范 Mgr 的几个方法命名
  4. */
  5. let AlertMgr = {
  6. /**
  7. * 初始化
  8. *
  9. * @author Pyden
  10. * @date 2019-03-28
  11. */
  12. init () {
  13. if (CC_EDITOR) {
  14. return;
  15. }
  16. cc.game.on('e_mgr_load_config_done', this.initLocalConfig, this);
  17. },
  18. /**
  19. * 初始化本地配置
  20. *
  21. * @author Pyden
  22. * @date 2019-03-28
  23. */
  24. initLocalConfig () {
  25. this.alertConfigMap = {};
  26. G.MgrUtils.loadConfig(this, 'AlertConfig', (list) => {
  27. for (let i = 0, len = list.length; i < len; i++) {
  28. let alert = list[i];
  29. alert.tmpMsg = alert.msg;
  30. this.alertConfigMap[alert.id] = alert;
  31. }
  32. });
  33. },
  34. /**
  35. * 获取弹框配置
  36. *
  37. * @author Pyden
  38. * @date 2019-03-28
  39. * @param {JMC.ALERT_ID} alertId 弹框 Id
  40. * @param {any} userdata 用户数据,替换##间的配置
  41. * @returns {AlertConfig}
  42. */
  43. getConfig (alertId, userdata) {
  44. let config = this.alertConfigMap[alertId];
  45. if (config && userdata) {
  46. if (config.tmpMsg) {
  47. config.msg = config.tmpMsg;
  48. for (let key in userdata) {
  49. let value = userdata[key];
  50. if (G.FuncUtils.isNumber(value)) {
  51. value = value.toString();
  52. } else if (!G.FuncUtils.isString(value)) {
  53. continue;
  54. }
  55. config.msg = config.msg.replace('#' + key + '#', value);
  56. }
  57. }
  58. }
  59. if (!config) {
  60. G.LogUtils.error('Bug: not found alert config', alertId);
  61. }
  62. return config;
  63. }
  64. };
  65. module.exports = AlertMgr;