Blame view

src/utils/util.js 2.98 KB
yangwei committed
1
import cookies from './util.cookies'
yuanbo committed
2 3 4 5 6
/**
 * @description: getUrlParam
 * @param {*} paraName
 * @author: renchao
 */
“miaofang committed
7 8 9
export function getUrlParam(paraName) {
  let url = document.location.toString();
  let arrObj = url.split('?');
yangwei committed
10

“miaofang committed
11 12 13
  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split('&');
    let arr;
yangwei committed
14

“miaofang committed
15 16
    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split('=');
yangwei committed
17

“miaofang committed
18 19 20 21 22
      if (arr != null && arr[0] === paraName) {
        // 截取#之前的内容
        let result = arr[1].endsWith('#/') ? arr[1].substr(0, arr[1].indexOf('#')) : arr[1];
        return result;
      }
yangwei committed
23
    }
“miaofang committed
24 25 26 27
    return '';
  } else {
    return '';
  }
yangwei committed
28
}
yuanbo committed
29 30 31 32 33
/**
 * @description: setToken
 * @param {*} token
 * @author: renchao
 */
“miaofang committed
34 35 36 37 38 39 40 41 42 43
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);
yangwei committed
44
    } else {
“miaofang committed
45
      cookies.set('ACCESS_TOKEN', token)
yangwei committed
46
    }
“miaofang committed
47
  }
yangwei committed
48
}
yuanbo committed
49 50 51 52
/**
 * @description: getToken
 * @author: renchao
 */
“miaofang committed
53 54 55 56 57
export function getToken() {
  if (process.env.NODE_ENV === 'development') {
    return sessionStorage.getItem('token')
  }
  return cookies.get('ACCESS_TOKEN')
yangwei committed
58 59
}

60
// 获取当前时间
yuanbo committed
61 62 63 64 65
/**
 * @description: 获取当前时间
 * @param {*} type
 * @author: renchao
 */
“miaofang committed
66 67 68 69 70 71 72 73 74 75 76 77 78
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}秒`
  }
yuanbo committed
79
}
xiaomiao committed
80

“miaofang committed
81
export function getNewDatesh() {
xiaomiao committed
82 83 84 85 86 87 88 89 90
  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');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
“miaofang committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

// 近一个月时间
export function getdatamonth() {
  var tempDate = new Date();
  var year = tempDate.getFullYear();
  var month = tempDate.getMonth();
  var arr = []
  arr.push(
    formatDate(new Date(year, month, 1)),
    formatDate(tempDate)
  );
  return arr
}
// 格式化日期
export function formatDate(date) {
  var year = date.getFullYear();
  var month = changeNum(date.getMonth() + 1);
  var day = changeNum(date.getDate());
  return `${year}-${month}-${day}`;
}
// 数字转换
export function changeNum(num) {
  if (num >= 10) {
    return num;
  } else {
    return "0" + num;
  }
}