business-tool.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var businessType = require('../public/business-type.js')
  2. var _debug =false
  3. function isDebug() {
  4. return _debug
  5. }
  6. function debugLog(e) {
  7. if (_debug) {
  8. console.log(e)
  9. }
  10. }
  11. function action(f, t) {
  12. var after = t || 500
  13. return function () {
  14. var app = getApp()
  15. if (app.globalData.isClicked) {
  16. return
  17. }
  18. app.globalData.isClicked = true
  19. f.apply(this, arguments)
  20. setTimeout(function() {
  21. app.globalData.isClicked = false
  22. }, after)
  23. }
  24. }
  25. function getPhone() {
  26. var tel
  27. try {
  28. tel = swan.getStorageSync(key.StorageKey.servicePhone)
  29. } catch (e) {
  30. }
  31. if (!tel) {
  32. tel = '400-881-9211'
  33. }
  34. return tel
  35. }
  36. function changeTimeToNumber(t) {
  37. return t.slice(0, 4) + t.slice(5, 7)
  38. }
  39. Date.prototype.format = function (format) {
  40. var date = {
  41. "M+": this.getMonth() + 1,
  42. "d+": this.getDate(),
  43. "h+": this.getHours(),
  44. "m+": this.getMinutes(),
  45. "s+": this.getSeconds(),
  46. "q+": Math.floor((this.getMonth() + 3) / 3),
  47. "S+": this.getMilliseconds()
  48. };
  49. if (/(y+)/i.test(format)) {
  50. format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  51. }
  52. for (var k in date) {
  53. if (new RegExp("(" + k + ")").test(format)) {
  54. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  55. ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
  56. }
  57. }
  58. return format;
  59. }
  60. /*时间差比较
  61. *interval :D表示查询精确到天数的之差
  62. interval :H表示查询精确到小时之差
  63. interval :M表示查询精确到分钟之差
  64. interval :S表示查询精确到秒之差
  65. interval :T表示查询精确到毫秒之差
  66. */
  67. function dateDiff(interval, date1, date2) {
  68. var objInterval = { 'D': 1000 * 60 * 60 * 24, 'H': 1000 * 60 * 60, 'M': 1000 * 60, 'S': 1000, 'T': 1 };
  69. interval = interval.toUpperCase();
  70. var dt1 = new Date(Date.parse(date1.replace(/-/g, '/')));
  71. var dt2 = new Date(Date.parse(date2.replace(/-/g, '/')));
  72. try {
  73. // debugLog(dt2.getTime() - dt1.getTime());
  74. // debugLog('objInterval.'+interval);
  75. // debugLog((dt2.getTime() - dt1.getTime()) / eval_r('objInterval.'+interval));
  76. var d = (dt2.getTime() - dt1.getTime()) / objInterval[interval]
  77. var _d = d | 0
  78. if (d != _d) {
  79. return _d + 1;
  80. }
  81. return _d;
  82. }
  83. catch (e) {
  84. return 0;
  85. }
  86. }
  87. function showFailTips (err, p) {
  88. if (err.code == businessType.NetworkErrorCode.netwrokMiss) {
  89. swan.showToast({
  90. title: err.msg,
  91. icon: 'none'
  92. })
  93. } else if (err.code == businessType.NetworkErrorCode.connectErrorMessage) {
  94. swan.showToast({
  95. title: err.msg,
  96. icon: 'none'
  97. })
  98. } else if (p) {
  99. swan.showToast({
  100. title: p,
  101. icon: 'none'
  102. })
  103. }
  104. }
  105. function selectFrom(lowerValue, upperValue) {
  106. var choice = upperValue - lowerValue + 1;
  107. return Math.floor(Math.random() * choice + lowerValue);
  108. }
  109. module.exports = {
  110. debugLog: debugLog,
  111. action: action,
  112. isDebug: isDebug,
  113. getPhone: getPhone,
  114. changeTimeToNumber: changeTimeToNumber,
  115. showFailTips: showFailTips,
  116. dateDiff: dateDiff,
  117. selectFrom: selectFrom
  118. }