util.js 1.83 KB
import cookies from './util.cookies'
export function getUrlParam (paraName) {
    let url = document.location.toString();
    let arrObj = url.split('?');

    if (arrObj.length > 1) {
        let arrPara = arrObj[1].split('&');
        let arr;

        for (let i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split('=');

            if (arr != null && arr[0] === paraName) {
                // 截取#之前的内容
                let result = arr[1].endsWith('#/') ? arr[1].substr(0, arr[1].indexOf('#')) : arr[1];
                return result;
            }
        }
        return '';
    } else {
        return '';
    }
}

export function setToken (token) {
    if (token === undefined) {
        if (process.env.NODE_ENV === 'development') {
            sessionStorage.removeItem('token')
        } else {
            cookies.remove('ACCESS_TOKEN')
        }
    } else {
        if (process.env.NODE_ENV === 'development') {
            sessionStorage.setItem('token', token);
        } else {
            cookies.set('ACCESS_TOKEN', token)
        }
    }
}

export function getToken () {
    if (process.env.NODE_ENV === 'development') {
        return sessionStorage.getItem('token')
    }
    return cookies.get('ACCESS_TOKEN')
}

// 获取当前时间

export function getNewDate (type = 1) {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    if (type == 1) {
        return `${year}${month}${day}日`
    } else {
        return `${year}${month}${day}${hours}${minutes}${seconds}秒`
    }
}