util.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const formatTime = 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 formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. const formatHtml = content => {
  15. content = content.replace(/\<img/gi, '<img style="width:100% !important;height:auto !important;margin:0;display:flex;" ');
  16. content = content.replace(/style="/gi, 'style="max-width:100% !important;table-layout:fixed;word-wrap:break-word;word-break;break-all;');
  17. content = content.replace(/\<table/gi, '<table style="table-layout:fixed;word-wrap:break-word;word-break;break-all;" ');
  18. content = content.replace(/\<td/gi, '<td cellspacing="0" cellpadding="0" border="0" style="display:block;vertical-align:top;margin: 0px; padding: 0px; border: 0px;outline-width:0px;" ');
  19. content = content.replace(/width=/gi, 'sss=');
  20. content = content.replace(/height=/gi, 'sss=');
  21. content = content.replace(/ \/\>/gi, ' style="max-width:100% !important;height:auto !important;margin:0;display:block;" \/\>');
  22. return content;
  23. }
  24. const endOfStartTime = (startTime, endTime) => {
  25. let result = {
  26. day: '00',
  27. hou: '00',
  28. min: '00',
  29. sec: '00'
  30. }
  31. if (endTime - startTime > 0) {
  32. let time = (endTime - startTime) / 1000;
  33. // 获取天、时、分、秒
  34. let day = parseInt(time / (60 * 60 * 24));
  35. let hou = parseInt(time % (60 * 60 * 24) / 3600);
  36. let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  37. let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  38. result = {
  39. day: `${timeFormat(day)}`,
  40. hou: `${timeFormat(hou)}`,
  41. min: `${timeFormat(min)}`,
  42. sec: `${timeFormat(sec)}`
  43. }
  44. }
  45. return result
  46. }
  47. // 小于10的格式化函数
  48. const timeFormat = (times) => {
  49. return times < 10 ? '0' + times : times;
  50. }
  51. const dateToTimestamp = (dateStr) => {
  52. if (!dateStr) {
  53. return ''
  54. }
  55. let newDataStr = dateStr.replace(/\.|\-/g, '/')
  56. let date = new Date(newDataStr);
  57. let timestamp = date.getTime();
  58. return timestamp
  59. }
  60. // 检查是否授权
  61. const checkAuthInfo = (fn, unNeedAuth) => {
  62. if (wx.getStorageSync('loginResult').userId) {
  63. fn()
  64. } else if (!unNeedAuth) {
  65. wx.hideLoading()
  66. wx.navigateTo({
  67. url: '/pages/login/login'
  68. })
  69. }
  70. }
  71. /**
  72. * 手机号正则校验
  73. */
  74. const checkPhoneNumber = (phoneNumber) => {
  75. var regexp = /^[1]([3-9])[0-9]{9}$/
  76. return regexp.test(phoneNumber)
  77. }
  78. /**
  79. * 用户名正则校验
  80. */
  81. const checkUserName = (userName) => {
  82. var regexp = /^([a-zA-Z0-9_]{4,16})$/
  83. return regexp.test(userName)
  84. }
  85. /**
  86. * 时间戳转化为年 月 日 时 分 秒
  87. * number: 传入时间戳
  88. * format:返回格式,支持自定义,但参数必须与formateArr里保持一致
  89. */
  90. function timestampToDate(timestamp) {
  91. if (timestamp) {
  92. const date = new Date(timestamp * 1000);
  93. const year = date.getFullYear();
  94. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  95. const day = date.getDate().toString().padStart(2, '0');
  96. const hours = date.getHours().toString().padStart(2, '0');
  97. const minutes = date.getMinutes().toString().padStart(2, '0');
  98. const seconds = date.getSeconds().toString().padStart(2, '0');
  99. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  100. }
  101. return "";
  102. }
  103. //经纬度转距离
  104. function getDistance(point1, point2) {
  105. let R = 6371000; // 地球平均半径,单位:米
  106. let lat1 = point1.latitude * Math.PI / 180;
  107. let lat2 = point2.latitude * Math.PI / 180;
  108. let lon1 = point1.longitude * Math.PI / 180;
  109. let lon2 = point2.longitude * Math.PI / 180;
  110. // Haversine公式
  111. let a = Math.sin((lat2 - lat1) / 2) * Math.sin((lat2 - lat1) / 2) +
  112. Math.cos(lat1) * Math.cos(lat2) * Math.sin((lon2 - lon1) / 2) * Math.sin((lon2 - lon1) / 2);
  113. let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  114. let d = R * c; // 初始计算得到的距离,单位:米
  115. if (d >= 1000) {
  116. return (d / 1000).toFixed(2) + "千米";
  117. } else {
  118. return d + "米";
  119. }
  120. }
  121. module.exports = {
  122. dateToTimestamp: dateToTimestamp,
  123. formatTime: formatTime,
  124. formatHtml: formatHtml,
  125. endOfStartTime: endOfStartTime,
  126. checkAuthInfo: checkAuthInfo,
  127. checkPhoneNumber: checkPhoneNumber,
  128. checkUserName: checkUserName,
  129. timestampToDate:timestampToDate,
  130. getDistance:getDistance
  131. }