From ec4d5c1827487f4c901b69bd9eae58e111e82b32 Mon Sep 17 00:00:00 2001 From: 徐旺旺 <11530253@qq.com> Date: 星期四, 20 五月 2021 18:05:25 +0800 Subject: [PATCH] Merge branch 'develop' of http://xearth.cn:6600/r/wuyushui/SewerAndRainNetwork into develop --- src/utils/navigation.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 167 insertions(+), 0 deletions(-) diff --git a/src/utils/navigation.js b/src/utils/navigation.js new file mode 100644 index 0000000..1c09633 --- /dev/null +++ b/src/utils/navigation.js @@ -0,0 +1,167 @@ +import Cookies from 'js-cookie' +const appConfig = require('@/app.config') +const { cookieExpires } = appConfig + +export const TOKEN_KEY = 'token' +export const ACCOUNT_KEY = 'account' +export const setToken = (token) => { + Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 }) +} + +export const getToken = () => { + const token = Cookies.get(TOKEN_KEY) + if (token !== 'undefined') return token + else return false +} + +export const setAccount = (account) => { + Cookies.set(ACCOUNT_KEY, account, { expires: cookieExpires || 1 }) +} + +export const getAccount = () => { + const account = Cookies.get(ACCOUNT_KEY) + if (account) return account + else return false +} + +export const hasChild = (item) => { + return item.children && item.children.length !== 0 +} + +/** + * @description 鏈湴瀛樺偍鍜岃幏鍙栨爣绛惧鑸垪琛� + */ +export const setTagNavListToLocalstorage = list => { + localStorage.tagNaveList = JSON.stringify(list) +} + +/** + * @returns {Array} 鍏朵腑鐨勬瘡涓厓绱犲彧鍖呭惈璺敱鍘熶俊鎭腑鐨刵ame, path, meta涓夐」 + */ +export const getTagNavListFromLocalstorage = () => { + const list = localStorage.tagNaveList + return list ? JSON.parse(list) : [] +} + +/** + * @param {Array} routers 璺敱鍒楄〃鏁扮粍 + * @description 鐢ㄤ簬鎵惧埌璺敱鍒楄〃涓璶ame涓篽ome鐨勫璞� + */ +export const getHomeRouter = (routers, homeName = 'Home') => { + let i = -1 + const len = routers.length + let homeRoute = {} + while (++i < len) { + const item = routers[i] + if (item.children && item.children.length) { + const res = getHomeRouter(item.children, homeName) + if (res.name) return res + } else { + if (item.name === homeName) homeRoute = item + } + } + return homeRoute +} + +/** + * @param {*} list 鐜版湁鏍囩瀵艰埅鍒楄〃 + * @param {*} newRoute 鏂版坊鍔犵殑璺敱鍘熶俊鎭璞� + * @description 濡傛灉璇ewRoute宸茬粡瀛樺湪鍒欎笉鍐嶆坊鍔� + */ +export const getNewTagList = (list, newRoute) => { + const { name, path, meta } = newRoute + const newList = [...list] + if (newList.findIndex(item => item.name === name) >= 0) return newList + else newList.push({ name, path, meta }) + return newList +} + +/** + * @param {Number} times 鍥炶皟鍑芥暟闇�瑕佹墽琛岀殑娆℃暟 + * @param {Function} callback 鍥炶皟鍑芥暟 + */ +export const doCustomTimes = (times, callback) => { + let i = -1 + while (++i < times) { + callback(i) + } +} + +export const findNodeUpper = (ele, tag) => { + if (ele.parentNode) { + if (ele.parentNode.tagName === tag.toUpperCase()) { + return ele.parentNode + } else { + return findNodeUpper(ele.parentNode, tag) + } + } +} + +export const findNodeUpperByClasses = (ele, classes) => { + const parentNode = ele.parentNode + if (parentNode) { + const classList = parentNode.classList + if (classList && classes.every(className => classList.contains(className))) { + return parentNode + } else { + return findNodeUpperByClasses(parentNode, classes) + } + } +} + +export const findNodeDownward = (ele, tag) => { + const tagName = tag.toUpperCase() + if (ele.childNodes.length) { + let i = -1 + const len = ele.childNodes.length + while (++i < len) { + const child = ele.childNodes[i] + if (child.tagName === tagName) return child + else return findNodeDownward(child, tag) + } + } +} + +export const localSave = (key, value) => { + localStorage.setItem(key, value) +} + +export const localRead = (key) => { + return localStorage.getItem(key) || '' +} + +// scrollTop animation +export const scrollTop = (el, from = 0, to, duration = 500, endCallback) => { + if (!window.requestAnimationFrame) { + window.requestAnimationFrame = ( + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.msRequestAnimationFrame || + function (callback) { + return window.setTimeout(callback, 1000 / 60) + } + ) + } + const difference = Math.abs(from - to) + const step = Math.ceil(difference / duration * 50) + + const scroll = (start, end, step) => { + if (start === end) { + endCallback && endCallback() + return + } + + let d = (start + step > end) ? end : start + step + if (start > end) { + d = (start - step < end) ? end : start - step + } + + if (el === window) { + window.scrollTo(d, d) + } else { + el.scrollTop = d + } + window.requestAnimationFrame(() => scroll(d, end, step)) + } + scroll(from, to, step) +} -- Gitblit v1.8.0