background

Utils functions can be copied and used directly, so there is no need to write the latter every time you search online.

parseTime


export function parseTime(time, cFormat) { if (arguments.length === 0) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (! time || time === 0) { return '--' } if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } if ((typeof time === 'number') && (time.toString().length === 10))  { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: GetDay () returns 0 on Sunday if (key = = = 'a') {return [' day ', 'a', '2', '3', '4', '5', [value]} return value.toString().padstart (2, '0')}) return time_str}Copy the code

Get the parameters from the URL

getQueryObject


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 }Copy the code

222


export function param(json) { if (! json) return '' return cleanArray( Object.keys(json).map(key => { if (json[key] === undefined) return '' return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) }) ).join('&') }Copy the code

333


export function param2Obj(url) { const search = url.split('? ')[1] if (! search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/\+/g, ' ') + '"}' ) }Copy the code

deepClone


export function deepClone(source) { if (! source && typeof source ! == 'object') { throw new Error('error arguments', 'deepClone') } const targetObj = source.constructor === Array ? [] : {} Object.keys(source).forEach(keys => { if (source[keys] && typeof source[keys] === 'object') { targetObj[keys] = deepClone(source[keys]) } else { targetObj[keys] = source[keys] } }) return targetObj }Copy the code

uuid


export function uuid2(len, radix = 16) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') var uuid = [] var i  radix = radix || chars.length if (len) { // Compact form for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix] } else { // rfc4122, version 4 form var r // rfc4122 requires these characters uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-' uuid[14] = '4' // Fill in random data. At I ==19 set the high bits of clock sequence as // per RFC4122, sec.4.1.5 for (I = 0; i < 36; i++) { if (! uuid[i]) { r = 0 | Math.random() * 16 uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r] } } } return uuid.join('') }Copy the code

md5


var crypto = require('crypto')
export function md5(content) {
  return crypto.createHash('md5').update(content).digest('hex')
}
Copy the code

isJson


export function isJSON(str) { if (typeof str === 'string') { try { var obj = JSON.parse(str) if (typeof obj === 'object' &&obj) {return true} else {return false}} Catch (e) {console.log('error: '+ STR + '!! ' + e) return false } } console.log('It is not a string! ')}Copy the code

cache


export const getSyncData = (key) => {
  const keyname = 'admin::' + key
  const result = window.localStorage.getItem(keyname)
  return SerializerJSON.parse(result)
}

export const setSyncData = (key, data) => {
  const keyname = 'admin::' + key
  const dataStr = SerializerJSON.stringify(data)
  return window.localStorage.setItem(keyname, dataStr)
}

export const delSyncData = (key) => {
  const keyname = 'admin::' + key
  delete Cache[keyname]
  return window.localStorage.removeItem(keyname)
}
Copy the code