Vue中util的工具函数

// 防抖函数
function debounce (fn, wait) {
let t
return () => {

let context = this
let args = arguments
if (t) clearTimeout(t)
t = setTimeout(() => {
  fn.apply(context, args)
}, wait)

}
}
function flatten (arr) { // 数组扁平化
return arr.reduce((result, item) => {

return result.concat(Array.isArray(item) ? flatten(item) : item)

}, [])
}
function handleMulitePerson (sPerson) {
console.log(44, sPerson.split(‘,’))
if (typeof sPerson == ‘string’) {

let personArr = []
sPerson.split(',').forEach(item => {
  let obj = { userCode: item.split('/')[0], userName: item.split('/')[1] }
  personArr.push(obj)
})
return personArr

}
}

function uniqueArray (array, key) { // json数组根据key去重
var result = [array[0]]
for (var i = 1; i < array.length; i++) {

var item = array[i]
var repeat = false
for (var j = 0; j < result.length; j++) {
  if (item[key] == result[j][key]) {
    repeat = true
    break
  }
}
if (!repeat) {
  result.push(item)
}

}
return result
}
function setSessionStorage (key, val) {
if (typeof (val) == ‘object’) {

sessionStorage.setItem(key, JSON.stringify(val))

} else {

sessionStorage.setItem(key, val)

}
}
function getSessionStorage (key) {
return sessionStorage.getItem(key)
}
function removeStorage (key) {
sessionStorage.removeItem(key)
}

function urlParams () { // 获取链接参数
var str = location.search.length > 0 ? location.search.substring(1) : ”
var items = str.length ? str.split(‘&’) : []
var args = {}
var item = null
var name = null
var value = null
for (let i = 0, len = items.length; i < len; i++) {

item = items[i].split('=')
name = decodeURIComponent(item[0])
value = decodeURIComponent(item[1])
if (name.length) {
  args[name] = value
}

};
return args
}

function urlAfterParams () {
var str = window.location.hash.length > 0 ? window.location.hash.substring(window.location.hash.indexOf(‘?’) + 1) : ”
var items = str.indexOf(‘&’) > 0 ? str.split(‘&’) : str.split(‘?’)
var args = {}
var item = null
var name = null
var value = null
for (let i = 0, len = items.length; i < len; i++) {

item = items[i].split('=')
name = decodeURIComponent(item[0])
value = decodeURIComponent(item[1])
if (name.length) {
  args[name] = value
}

};
return args
}

function parseParams (url) {
url = decodeURIComponent(url)
var params = {}
var idx = url.indexOf(‘?’)
if (idx > 0) {

var queryStr = url.substring(idx + 1)
if (queryStr.length > 0) {
  var arr = queryStr.split('&')
  for (let i = 0; i < arr.length; i++) {
    var pair = arr[i].split('=')
    if (pair.length == 2 && pair[0].length > 0) {
      params[pair[0]] = pair[1]
    }
  }
}

}
return params
}

/**

  • 选人下拉框数据:username(userCode)

*/
function getSelectUserName (userName, userCode) {
return userName + ‘(‘ + userCode + ‘)’
}

function getSelectLoginUser () {
var userInfo = getLoginUserInfo()
return getSelectUserName(userInfo.userName, userInfo.userId)
}

function getUserNameBySelectUserName (userName) {
let i = userName.indexOf(‘(‘)
return userName.substring(0, i)
}

/**

  • 登录用户信息
  • userId
  • userName
  • mobileNo
  • @returns {any}

*/
function getLoginUserInfo () {
return JSON.parse(localStorage.getItem(‘userInfo’))
}

function getLoginUserCode () {
return JSON.parse(localStorage.getItem(‘userInfo’)).userId
}

export default {
getNyr,
getYDate,
setSessionStorage,
urlParams,
urlAfterParams,
parseParams,
debounce,
handleMulitePerson,
uniqueArray,
flatten,
getSessionStorage,
removeStorage,
getSelectUserName,
getSelectLoginUser,
getLoginUserInfo,
getLoginUserCode,
getUserNameBySelectUserName
}

    原文作者:yang
    原文地址: https://segmentfault.com/a/1190000019690925
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞