tools.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import configs from "@/config/config";
  2. import Foundation from "@/utils/Foundation.js";
  3. const getNetworkType = () => {
  4. uni.getNetworkType({
  5. success: (res) => {
  6. if (res.networkType === "none") {
  7. uni.showToast({
  8. title: "网络好像有点问题,请检查后重试!",
  9. duration: 2000,
  10. icon: "none",
  11. });
  12. let pages = getCurrentPages();
  13. if (pages.length) {
  14. let route = pages[pages.length - 1].route;
  15. if (route !== "pages/empty/empty") {
  16. uni.navigateTo({
  17. url: `/pages/empty/empty?type=wifi`,
  18. });
  19. }
  20. } else {
  21. uni.navigateTo({
  22. url: `/pages/empty/empty?type=wifi`,
  23. });
  24. }
  25. }
  26. },
  27. });
  28. };
  29. const throttle = (fn, that, gapTime) => {
  30. // export function throttle(fn, gapTime) {
  31. if (gapTime == null || gapTime == undefined) {
  32. gapTime = 1800;
  33. }
  34. let _lastTime = that.lastTime;
  35. let _nowTime = +new Date();
  36. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  37. fn.apply(that, arguments); //将this和参数传给原函数
  38. that.lastTime = _nowTime;
  39. }
  40. };
  41. /**
  42. * 计算传秒数的倒计时【天、时、分、秒】
  43. * @param seconds
  44. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  45. */
  46. const countTimeDown = (seconds) => {
  47. const leftTime = (time) => {
  48. if (time < 10) time = "0" + time;
  49. return time + "";
  50. };
  51. return {
  52. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  53. hours: leftTime(parseInt((seconds / 60 / 60) % 24, 10)),
  54. minutes: leftTime(parseInt((seconds / 60) % 60, 10)),
  55. seconds: leftTime(parseInt(seconds % 60, 10)),
  56. };
  57. };
  58. /**
  59. * 计算当前时间到第二天0点的倒计时[秒]
  60. * @returns {number}
  61. */
  62. const theNextDayTime = () => {
  63. const nowDate = new Date();
  64. const time =
  65. new Date(
  66. nowDate.getFullYear(),
  67. nowDate.getMonth(),
  68. nowDate.getDate() + 1,
  69. 0,
  70. 0,
  71. 0
  72. ).getTime() - nowDate.getTime();
  73. return parseInt(time / 1000);
  74. };
  75. const mpScan = () => {
  76. uni.scanCode({
  77. success: function (res) {
  78. let path = encodeURIComponent(res.result);
  79. // WX_CODE 为小程序码
  80. if (res.scanType == "WX_CODE") {
  81. uni.navigateTo({
  82. url: `/${res.path}`,
  83. });
  84. }
  85. // QR_CODE 为字符串
  86. else if (res.scanType == "QR_CODE" || res.scanType == "QR") {
  87. // 如果匹配qrLink,说明是枪编号,直接跳转到枪详情
  88. if (
  89. res.result != undefined
  90. ) {
  91. let connectorNum = Foundation.urlParse(res.result).n;
  92. uni.navigateTo({
  93. url: `/pages/station/charge/charging?id=${connectorNum}`,
  94. });
  95. } else {
  96. uni.navigateTo({
  97. url: `${res.result}`,
  98. });
  99. }
  100. }
  101. },
  102. });
  103. };
  104. const serviceCall = (tel) => {
  105. uni.makePhoneCall({
  106. phoneNumber: tel
  107. })
  108. };
  109. export { getNetworkType, throttle, countTimeDown, theNextDayTime, mpScan, serviceCall };