vue小技巧

vue获取url数据

getUrlParam() {
  let url = location.search;
  let theRequest = {};
  if (url.indexOf('?') !== -1) {
    let strs = url.substr(1).split('&');
    for (let i = 0; i < strs.length; i++) {
     theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return theRequest;
}

时分秒


dateFormat({ time } = {}) {
  // console.log(time);
  let newTime = time ? time.split('.')[0].replace('T', ' ') : '';
  // console.log(newTime);
  let date = new Date(Date.parse(newTime.replace(/-/g, '/')));
  // console.log(date);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
import { clearCookie } from '@/assets/js/utils/cookie';
import cookieKey from '@/assets/js/config/cookie-key';
import areas from '../config/area.js';

export function getUrlParam() {
  let url = location.search;
  let theRequest = {};
  if (url.indexOf('?') !== -1) {
    let strs = url.substr(1).split('&');
    for (let i = 0; i < strs.length; i++) {
     theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return theRequest;
}

export function getArea({ provinceId = 0, cityId = 0, areaId = 0 }) {
  let areaName = {};
  for (let item of areas) {
    if (provinceId === Number(item.id)) areaName.provinceName = item.name;
    if (cityId === Number(item.id)) areaName.cityName = item.name;
    if (areaId === Number(item.id)) areaName.areaName = item.name;
  }
  return areaName;
}

export function dateFormat({ time } = {}) {
  // console.log(time);
  let newTime = time ? time.split('.')[0].replace('T', ' ') : '';
  // console.log(newTime);
  let date = new Date(Date.parse(newTime.replace(/-/g, '/')));
  // console.log(date);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

export function dataSub(time) {
  if (!time) return '';
  let year = time.substring(0, 4);
  let month = time.substring(4, 6);
  let day = time.substring(6, 8);
  let hour = time.substring(8, 10);
  let minute = time.substring(10, 12);
  let second = time.substring(12, 14);
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}

export function loginOut() {
  clearCookie(cookieKey.userToken);
  clearCookie(cookieKey.userDealerId);
  location.href = '/login.html';
}

export function toFixed(num, len) {
  // console.log(num);
  let number = num.toString();
  let numArr = number.split('.');
  let rightNum = numArr[1];
  // let returnNum = num;
  if (rightNum && rightNum.length > len && len > 1) {
    let posNum = rightNum.substr(len, 1);
    let zs = rightNum.substr(len, 1);
    if (zs === '5') {
      rightNum = rightNum.substr(0, len) + (Number(posNum) + 1) + rightNum.substr(len + 1);
    }
    // let posNum = rightNum.substr(len, 1);
    // if (zs === '5') {
    //   // console.log(rightNum);
    //   let centerNum = Number(posNum);
    //   if (centerNum % 2 === 0) {
    //     rightNum = rightNum.substr(0, len) + (Number(posNum) + 1) + rightNum.substr(len);
    //   }
    //   // console.log(rightNum);
    // }
  }
  // console.log(parseFloat(numArr[0] + '.' + rightNum));
  // console.log(parseFloat(numArr[0] + '.' + rightNum).toFixed(len));
  return parseFloat(parseFloat(numArr[0] + '.' + rightNum).toFixed(len));
}

// export function dateFormat({ time } = {}) {
//   let date = new Date(time);
//   let year = date.getFullYear();
//   let month = date.getMonth() + 1;
//   let day = date.getDate();
//   let hour = date.getHours();
//   let minute = date.getMinutes();
//   let second = date.getSeconds();
//   return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
// }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值