Foundation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * 一些常用的基础方法
  3. * whetherNavigate 登录后跳转判断
  4. * unixToDate 将unix时间戳转换为指定格式
  5. * dateToUnix 将时间转unix时间戳
  6. * deepClone 对一个对象进行深拷贝
  7. * formatPrice 货币格式化
  8. * secrecyMobile 手机号隐私保护
  9. * randomString 随机生成指定长度的字符串
  10. */
  11. /**
  12. * 登录后跳转判断
  13. * 计算出当前router路径
  14. * 1.如果跳转的链接为登录页面或跳转的链接为空页面。则会重新跳转到首页
  15. * 2.都不满足返回跳转页面
  16. */
  17. export function whetherNavigate() {
  18. if (getCurrentPages().length > 1) {
  19. if ((getCurrentPages().length - 2).route == "pages/passport/login") {
  20. uni.switchTab({
  21. url: "/pages/tabbar/home/index",
  22. });
  23. } else {
  24. if (
  25. !(getCurrentPages().length - 2).route ||
  26. (getCurrentPages().length - 2).route == "undefined"
  27. ) {
  28. uni.switchTab({
  29. url: "/pages/tabbar/home/index",
  30. });
  31. } else {
  32. uni.navigateBack({
  33. delta: getCurrentPages().length - 2,
  34. });
  35. }
  36. }
  37. } else {
  38. uni.switchTab({
  39. url: "/pages/tabbar/home/index",
  40. });
  41. }
  42. }
  43. /**
  44. * 将unix时间戳转换为指定格式
  45. * @param unix 时间戳【秒】
  46. * @param format 转换格式
  47. * @returns {*|string}
  48. */
  49. export function unixToDate(unix, format) {
  50. if (!unix) return unix;
  51. let _format = format || "yyyy-MM-dd hh:mm:ss";
  52. const d = new Date(unix);
  53. const o = {
  54. "M+": d.getMonth() + 1,
  55. "d+": d.getDate(),
  56. "h+": d.getHours(),
  57. "m+": d.getMinutes(),
  58. "s+": d.getSeconds(),
  59. "q+": Math.floor((d.getMonth() + 3) / 3),
  60. S: d.getMilliseconds(),
  61. };
  62. if (/(y+)/.test(_format))
  63. _format = _format.replace(
  64. RegExp.$1,
  65. (d.getFullYear() + "").substr(4 - RegExp.$1.length)
  66. );
  67. for (const k in o)
  68. if (new RegExp("(" + k + ")").test(_format))
  69. _format = _format.replace(
  70. RegExp.$1,
  71. RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  72. );
  73. return _format;
  74. }
  75. /**
  76. * 将时间转unix时间戳
  77. * @param date
  78. * @returns {number} 【秒】
  79. */
  80. export function dateToUnix(date) {
  81. let newStr = date.replace(/:/g, "-");
  82. newStr = newStr.replace(/ /g, "-");
  83. const arr = newStr.split("-");
  84. const datum = new Date(
  85. Date.UTC(
  86. arr[0],
  87. arr[1] - 1,
  88. arr[2],
  89. arr[3] - 8 || -8,
  90. arr[4] || 0,
  91. arr[5] || 0
  92. )
  93. );
  94. return parseInt(datum.getTime() / 1000);
  95. }
  96. /**
  97. * 货币格式化
  98. * @param price
  99. * @returns {string}
  100. */
  101. export function formatPrice(price) {
  102. if (typeof price !== "number") return price;
  103. return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  104. }
  105. /**
  106. * 手机号隐私保护
  107. * 隐藏中间四位数字
  108. * @param mobile
  109. * @returns {*}
  110. */
  111. export function secrecyMobile(mobile) {
  112. mobile = String(mobile);
  113. if (!/\d{11}/.test(mobile)) {
  114. return mobile;
  115. }
  116. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
  117. }
  118. /**
  119. * 随机生成指定长度的字符串
  120. * @param length
  121. * @returns {string}
  122. */
  123. export function randomString(length = 32) {
  124. const chars =
  125. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  126. const maxPos = chars.length;
  127. let _string = "";
  128. for (let i = 0; i < length; i++) {
  129. _string += chars.charAt(Math.floor(Math.random() * maxPos));
  130. }
  131. return _string;
  132. }
  133. /**
  134. * 计算传秒数的倒计时【天、时、分、秒】
  135. * @param seconds
  136. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  137. */
  138. export function countTimeDown(seconds) {
  139. const leftTime = (time) => {
  140. if (time < 10) time = "0" + time;
  141. return time + "";
  142. };
  143. return {
  144. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  145. hours: leftTime(parseInt((seconds / 60 / 60) % 24, 10)),
  146. minutes: leftTime(parseInt((seconds / 60) % 60, 10)),
  147. seconds: leftTime(parseInt(seconds % 60, 10)),
  148. };
  149. }
  150. /**
  151. * 计算当前时间到第二天0点的倒计时[秒]
  152. * @returns {number}
  153. */
  154. export function theNextDayTime() {
  155. const nowDate = new Date();
  156. const time =
  157. new Date(
  158. nowDate.getFullYear(),
  159. nowDate.getMonth(),
  160. nowDate.getDate() + 1,
  161. 0,
  162. 0,
  163. 0
  164. ).getTime() - nowDate.getTime();
  165. return parseInt(time / 1000);
  166. }
  167. module.exports = {
  168. unixToDate,
  169. dateToUnix,
  170. formatPrice,
  171. secrecyMobile,
  172. randomString,
  173. countTimeDown,
  174. theNextDayTime,
  175. whetherNavigate
  176. };