Foundation.js 4.8 KB

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