tools.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * 解析url参数
  3. * @example ?id=12345&a=b
  4. * @return Object {id:12345,a:b}
  5. */
  6. function urlParse(url) {
  7. let obj = {};
  8. let reg = /[?&][^?&]+=[^?&]+/g;
  9. let arr = url.match(reg);
  10. if (arr) {
  11. arr.forEach((item) => {
  12. let tempArr = item.substring(1).split("=");
  13. let key = decodeURIComponent(tempArr[0]);
  14. let val = decodeURIComponent(tempArr.splice(1).join("="));
  15. obj[key] = val;
  16. });
  17. }
  18. return obj;
  19. }
  20. const getNetworkType = () => {
  21. uni.getNetworkType({
  22. success: (res) => {
  23. if (res.networkType === "none") {
  24. uni.showToast({
  25. title: "网络好像有点问题,请检查后重试!",
  26. duration: 2000,
  27. icon: "none",
  28. });
  29. let pages = getCurrentPages();
  30. if (pages.length) {
  31. let route = pages[pages.length - 1].route;
  32. if (route !== "pages/empty/empty") {
  33. uni.navigateTo({
  34. url: `/pages/empty/empty?type=wifi`,
  35. });
  36. }
  37. } else {
  38. uni.navigateTo({
  39. url: `/pages/empty/empty?type=wifi`,
  40. });
  41. }
  42. }
  43. },
  44. });
  45. };
  46. const throttle = (fn, that, gapTime) => {
  47. // export function throttle(fn, gapTime) {
  48. if (gapTime == null || gapTime == undefined) {
  49. gapTime = 1800;
  50. }
  51. let _lastTime = that.lastTime;
  52. let _nowTime = +new Date();
  53. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  54. fn.apply(that, arguments); //将this和参数传给原函数
  55. that.lastTime = _nowTime;
  56. }
  57. };
  58. /**
  59. * 计算传秒数的倒计时【天、时、分、秒】
  60. * @param seconds
  61. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  62. */
  63. const countTimeDown = (seconds) => {
  64. const leftTime = (time) => {
  65. if (time < 10) time = "0" + time;
  66. return time + "";
  67. };
  68. return {
  69. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  70. hours: leftTime(parseInt((seconds / 60 / 60) % 24, 10)),
  71. minutes: leftTime(parseInt((seconds / 60) % 60, 10)),
  72. seconds: leftTime(parseInt(seconds % 60, 10)),
  73. };
  74. };
  75. /**
  76. * 计算当前时间到第二天0点的倒计时[秒]
  77. * @returns {number}
  78. */
  79. const theNextDayTime = () => {
  80. const nowDate = new Date();
  81. const time =
  82. new Date(
  83. nowDate.getFullYear(),
  84. nowDate.getMonth(),
  85. nowDate.getDate() + 1,
  86. 0,
  87. 0,
  88. 0
  89. ).getTime() - nowDate.getTime();
  90. return parseInt(time / 1000);
  91. };
  92. export {
  93. getNetworkType,
  94. throttle,
  95. countTimeDown,
  96. theNextDayTime,
  97. };