import { Message } from 'element-ui' import router from '../router' import Constant from '../utils/constant' /** * 判断用户是否登录 */ export function checkUserLogin(fn) { let token = localStorage.getItem(Constant.STORAGE_KEY.TOKEN) let userType = localStorage.getItem(Constant.STORAGE_KEY.USER_TYPE) if (isNull(token) || isNull(userType)) { goToPage('console', true) return } if (fn) { fn() } } /** * 判断是否为空 * @param data * @returns {boolean} */ export function isNull(data) { if (data === undefined) { return true } else if (data === null) { return true } else if (typeof data === 'string' && (data.length === 0 || data === '' || data === 'undefined' || data === 'null')) { return true } else if ((data instanceof Array) && data.length === 0) { return true } return false } /** * 判断不为空 * @param data * @returns {boolean} */ export function isNotNull(data) { return !isNull(data) } /** * 显示顶部红色通知 * @param msg */ export function showDanger(msg) { if (isNull(msg)) { return } Message({ message: msg, type: 'error', showClose: true }) } /** * 显示顶部橙色通知 * @param msg */ export function showWarning(msg) { if (isNull(msg)) { return } Message({ message: msg, type: 'warning', showClose: true }); } /** * 显示顶部绿色通知 * @param msg */ export function showSuccess(msg) { Message({ message: msg, type: 'success', showClose: true }) } /** * 页面跳转 * @param path * @param isRepalce */ export function goToPage(path, isRepalce) { if (isRepalce) { router.replace(path) } else { router.push(path) } } /** * 获取当前vue页面名称 * @param path * @param isRepalce */ export function getCurrentPage() { let hash = location.hash.replace('#', '') if (hash.indexOf('?') > 0) { hash = hash.substring(0, hash.indexOf('?')) } return hash } /** * 生成从[min,max]的随机数 * @param min * @param max * @returns {number} */ export function randomNum(min, max) { return Math.round(Math.random() * (max - min) + min) } /** * 获取uuid */ export function getUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16) }) }