squ.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * 是否为微信浏览器
  3. * @returns true | false
  4. */
  5. export const isWechat = () => {
  6. if (!window || !window.navigator || !window.navigator.userAgent) {
  7. // 小程序没有Windows对象
  8. return false
  9. }
  10. const ua = window.navigator.userAgent.toLowerCase()
  11. return ua.match(/micromessenger/i) == 'micromessenger'
  12. }
  13. /**
  14. * 是否为IOS浏览器
  15. * @returns true | false
  16. */
  17. export const isIOS = () => {
  18. let isIphone = navigator.userAgent.includes('iPhone')
  19. let isIpad = navigator.userAgent.includes('iPad')
  20. return isIphone || isIpad
  21. }
  22. /**
  23. * 日期格式化
  24. *
  25. * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  26. * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  27. * 例子:
  28. * (new Date()).Format("yyyy-MM-dd h:m:s.S") ==> 2006-07-02 8:9:4.423
  29. * (new Date()).Format("yyyy-M-d hh:mm:ss") ==> 2006-7-2 08:09:04.18
  30. * (new Date()).Format("yyyy年MM月dd日 hh:mm") ==> 2006年7月2日 08:09
  31. * (new Date()).Format("hh:mm") ==> 08:09
  32. */
  33. export const dateFormat = (date, fmt) => {
  34. let o = {
  35. "M+": date.getMonth() + 1, // 月份
  36. "d+": date.getDate(), // 日
  37. "h+": date.getHours(), // 小时
  38. "m+": date.getMinutes(), // 分
  39. "s+": date.getSeconds(), // 秒
  40. "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
  41. "S": date.getMilliseconds() // 毫秒
  42. };
  43. if (/(y+)/.test(fmt))
  44. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  45. for (let k in o)
  46. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
  47. "00" + o[k]).substr(("" + o[k]).length)));
  48. return fmt;
  49. }
  50. /**
  51. * 获取url参数. uni-app可参考squni.js中的 getCurQuery
  52. */
  53. export const getUrlQuery = (name, href) => {
  54. // return (
  55. // decodeURIComponent(
  56. // (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || [, ''])[1].replace(
  57. // /\+/g, '%20')
  58. // ) || null
  59. // )
  60. href = href || window.location.href
  61. let urlStr = href.split('?')[1]
  62. const urlSearchParams = new URLSearchParams(urlStr)
  63. const params = Object.fromEntries(urlSearchParams.entries())
  64. Object.keys(params).forEach(key => params[key] = decodeURIComponent(params[key]))
  65. return name ? params[name] : params
  66. }
  67. /**
  68. * Map转成url参数: k1=v1&k2=v2
  69. */
  70. export const objToUrlQuery = (obj, ignoreFields) => {
  71. if (!ignoreFields) {
  72. ignoreFields = []
  73. }
  74. return Object.keys(obj)
  75. .filter(key => obj[key] != null && ignoreFields.indexOf(key) === -1)
  76. .map(key => key + '=' + encodeURIComponent(obj[key])).join('&')
  77. }
  78. /**
  79. * 定时执行,需要再回调中清除定时
  80. */
  81. export const interval = (callback, timer = null, interval = 1000) => {
  82. //清除原定时器
  83. clearInterval(timer)
  84. //开启定时器定时
  85. timer = setInterval(() => {
  86. callback && callback()
  87. }, interval)
  88. return timer
  89. }
  90. /**
  91. * 生成uuid. eg: bb8263ae-ce73-4e3b-82de-67c105bc1a4b
  92. */
  93. export const uuid = (removeMidline) => {
  94. let s = [];
  95. let hexDigits = "0123456789abcdef";
  96. for (let i = 0; i < 36; i++) {
  97. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  98. }
  99. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  100. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  101. s[8] = s[13] = s[18] = s[23] = "-";
  102. let uuid = s.join("");
  103. return removeMidline ? uuid.replace(/\-/g, '') : uuid;
  104. }
  105. export default {
  106. isWechat,
  107. isIOS,
  108. dateFormat,
  109. getUrlQuery,
  110. objToUrlQuery,
  111. interval,
  112. uuid
  113. }