util.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const formatTime1 = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('') + [hour, minute, second].map(formatNumber).join('')
  9. }
  10. const formatTime = date => {
  11. const year = date.getFullYear()
  12. const month = date.getMonth() + 1
  13. const day = date.getDate()
  14. const hour = date.getHours()
  15. const minute = date.getMinutes()
  16. const second = date.getSeconds()
  17. return [year, month, day].map(formatNumber).join('-')
  18. + ' ' + [hour, minute, second].map(formatNumber).join(':')
  19. }
  20. const formatNumber = n => {
  21. n = n.toString()
  22. return n[1] ? n : '0' + n
  23. }
  24. const Tips = function (opt, to_url) {
  25. if (typeof opt == 'string') {
  26. to_url = opt;
  27. opt = {};
  28. }
  29. var title = opt.title || '', icon = opt.icon || 'none', endtime = opt.endtime || 2000;
  30. if (title) {
  31. wx.showToast({ title: title, icon: icon, duration: endtime })
  32. // setTimeout(function () {
  33. // wx.reLaunch({
  34. // url: '/pages/back_sand/back?id=2'
  35. // });
  36. // }, 2500)
  37. }
  38. if (to_url != undefined) {
  39. if (typeof to_url == 'object') {
  40. var tab = to_url.tab || 1, url = to_url.url || '';
  41. switch (tab) {
  42. case 1:
  43. //一定时间后跳转至 table
  44. setTimeout(function () {
  45. wx.switchTab({
  46. url: url
  47. })
  48. }, endtime);
  49. break;
  50. case 2:
  51. //跳转至非table页面
  52. setTimeout(function () {
  53. wx.navigateTo({
  54. url: url,
  55. })
  56. }, endtime);
  57. break;
  58. case 3:
  59. //返回上页面
  60. setTimeout(function () {
  61. wx.navigateBack({
  62. delta: parseInt(url),
  63. })
  64. }, endtime);
  65. break;
  66. case 4:
  67. //关闭当前所有页面跳转至非table页面
  68. setTimeout(function () {
  69. wx.reLaunch({
  70. url: url,
  71. })
  72. }, endtime);
  73. break;
  74. case 5:
  75. //关闭当前页面跳转至非table页面
  76. setTimeout(function () {
  77. wx.redirectTo({
  78. url: url,
  79. })
  80. }, endtime);
  81. break;
  82. }
  83. } else if (typeof to_url == 'function') {
  84. setTimeout(function () {
  85. to_url && to_url();
  86. }, endtime);
  87. } else {
  88. //没有提示时跳转不延迟
  89. setTimeout(function () {
  90. wx.navigateTo({
  91. url: to_url,
  92. })
  93. }, title ? endtime : 0);
  94. }
  95. }
  96. }
  97. module.exports = {
  98. formatTime: formatTime,
  99. formatTime1: formatTime1,
  100. Tips: Tips,
  101. }