util.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const config_config = require("../config/config.js");
  4. const framework_log = require("./log.js");
  5. const framework_tools = require("./tools.js");
  6. class util {
  7. /**
  8. * 获取本地数据
  9. */
  10. static getStorage(key) {
  11. let d = common_vendor.index.getStorageSync(key);
  12. if (util.isNull(d)) {
  13. return null;
  14. }
  15. return d;
  16. }
  17. /**
  18. * 设置本地数据
  19. */
  20. static setStorage(key, value) {
  21. common_vendor.index.setStorageSync(key, value);
  22. }
  23. /**
  24. * 清除本地key数据
  25. */
  26. static clearStorageForKey(key) {
  27. let d = util.getStorage(key);
  28. if (util.isNull(d)) {
  29. framework_log.log.Error(`${key} is null!`);
  30. } else {
  31. util.setStorage(key, null);
  32. }
  33. }
  34. /**
  35. * 清除本地数据
  36. */
  37. static clearAllStorage() {
  38. common_vendor.index.clearStorageSync();
  39. }
  40. static isNull(v) {
  41. if (v == null || v == void 0 || v == "") {
  42. return true;
  43. }
  44. return false;
  45. }
  46. static alert(v) {
  47. let Platform = framework_tools.tools.getCurPlatform();
  48. if (Platform == config_config.config.Platform.H5) {
  49. alert(v);
  50. } else if (Platform == config_config.config.Platform.TOUTIAO) {
  51. framework_log.log.Debug("TOUTIAO", v);
  52. } else if (Platform == config_config.config.Platform.WEIXIN) {
  53. framework_log.log.Debug("WEIXIN", v);
  54. }
  55. }
  56. // Loadding
  57. static showLoading(title = "加载中...", mask = false) {
  58. common_vendor.index.showLoading({
  59. title,
  60. mask
  61. });
  62. }
  63. static hideLoading() {
  64. common_vendor.index.hideLoading();
  65. }
  66. // Toast
  67. static showInfoToast(title, duration = 1500, mask = false) {
  68. common_vendor.index.showToast({
  69. title,
  70. icon: "none",
  71. duration,
  72. mask
  73. });
  74. }
  75. static showSuccessToast(title, duration = 1500, mask = false) {
  76. common_vendor.index.showToast({
  77. title,
  78. icon: "success",
  79. duration,
  80. mask
  81. });
  82. }
  83. static showErrorToast(title, duration = 1500, mask = false) {
  84. common_vendor.index.showToast({
  85. title,
  86. icon: "error",
  87. duration,
  88. mask
  89. });
  90. }
  91. // Modal
  92. static showModal(title, content, confirm_cb = null, cancel_cb = null, confirmText = "确定", showCancel = true, cancelText = "取消") {
  93. common_vendor.index.showModal({
  94. title,
  95. content,
  96. confirmText,
  97. showCancel,
  98. cancelText,
  99. success: function(res) {
  100. if (res.confirm) {
  101. confirm_cb && confirm_cb();
  102. } else if (res.cancel) {
  103. cancel_cb && cancel_cb();
  104. }
  105. }
  106. });
  107. }
  108. static showModalNoCancel(title, content, confirm_cb = null, cancel_cb = null, confirmText = "确定") {
  109. this.showModal(title, content, confirm_cb, cancel_cb, confirmText, false);
  110. }
  111. // ActionSheet
  112. static showActionSheet(alertText, itemList, confirm_cb, cancel_cb = null) {
  113. common_vendor.index.showActionSheet({
  114. alertText,
  115. itemList,
  116. success: function(res) {
  117. confirm_cb && confirm_cb(res.tapIndex);
  118. },
  119. fail: function(err) {
  120. cancel_cb && cancel_cb();
  121. }
  122. });
  123. }
  124. }
  125. exports.util = util;