123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /**
- * 获取路径参数
- * @param {*} url
- */
- export function getQueryObject (url) {
- url = url == null ? window.location.href : url
- const search = url.substring(url.lastIndexOf('?') + 1)
- const obj = {}
- const reg = /([^?&=]+)=([^?&=]*)/g
- search.replace(reg, (rs, $1, $2) => {
- const name = decodeURIComponent($1)
- let val = decodeURIComponent($2)
- val = String(val)
- obj[name] = val
- return rs
- })
- return obj
- }
- // 获取全局唯一id
- export function getUuid() {
- const s = [];
- const hexDigits = '0123456789abcdef';
- for (let i = 0; i < 36; i++) {
- s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
- }
- s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
- s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
- s[8] = '-';
- s[13] = '-';
- s[18] = '-';
- s[23] = '-';
- const uid = s.join('');
- return uid;
- }
- /**
- * @param {Sting} input value
- * @returns {number} output value
- */
- export function byteLength (str) {
- // returns the byte length of an utf8 string
- let s = str.length
- for (var i = str.length - 1; i >= 0; i--) {
- const code = str.charCodeAt(i)
- if (code > 0x7f && code <= 0x7ff) s++
- else if (code > 0x7ff && code <= 0xffff) s += 2
- if (code >= 0xDC00 && code <= 0xDFFF) i--
- }
- return s
- }
- export function html2Text (val) {
- const div = document.createElement('div')
- div.innerHTML = val
- return div.textContent || div.innerText
- }
- export function toggleClass (element, className) {
- if (!element || !className) {
- return
- }
- let classString = element.className
- const nameIndex = classString.indexOf(className)
- if (nameIndex === -1) {
- classString += ' ' + className
- } else {
- classString =
- classString.substr(0, nameIndex) +
- classString.substr(nameIndex + className.length)
- }
- element.className = classString
- }
- export function debounce (func, wait, immediate) {
- let timeout, args, context, timestamp, result
- const later = function () {
- // 据上一次触发时间间隔
- const last = +new Date() - timestamp
- // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
- if (last < wait && last > 0) {
- timeout = setTimeout(later, wait - last)
- } else {
- timeout = null
- // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
- if (!immediate) {
- result = func.apply(context, args)
- if (!timeout) context = args = null
- }
- }
- }
- return function (...args) {
- context = this
- timestamp = +new Date()
- const callNow = immediate && !timeout
- // 如果延时不存在,重新设定延时
- if (!timeout) timeout = setTimeout(later, wait)
- if (callNow) {
- result = func.apply(context, args)
- context = args = null
- }
- return result
- }
- }
- export function formatTime(time, fmt) {
- if (!time) return '';
- else {
- const date = new Date(time);
- const o = {
- 'M+': date.getMonth() + 1,
- 'd+': date.getDate(),
- 'H+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- S: date.getMilliseconds(),
- };
- if (/(y+)/.test(fmt))
- fmt = fmt.replace(
- RegExp.$1,
- (date.getFullYear() + '').substr(4 - RegExp.$1.length)
- );
- for (const k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1
- ? o[k]
- : ('00' + o[k]).substr(('' + o[k]).length)
- );
- }
- }
- return fmt;
- }
- }
|