vue 常见的js方法

/**
 * 从身份证获取生日
 */
export function getBirthDayFromId(idCardNum) {
  let value = idCardNum + '';
  // sex 0未知  1男  2女
  let birthday = '';
  if (value.length === 15) {
    birthday = `19${value.substr(6, 2)}/${value.substr(8, 2)}/${value.substr(
      10,
      2
    )}`;
  }
  if (value.length === 18) {
    birthday = `${value.substr(6, 4)}/${value.substr(10, 2)}/${value.substr(
      12,
      2
    )}`;
  }
  return birthday;
}
 
 
/**
 * 获取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
    );
  });
}
 
 
 
 
/**
 * 时间戳转日期格式
 * @timeStamp {number} 时间戳
 * @format {string} 格式[完整格式:YYYY-MM-DD HH:mm:ss]
 */
Vue.prototype.$formatDate = function(timeStamp, format) {
  if (timeStamp) {
    format = format || 'YYYY-MM-DD';
    let week = [
      '星期日',
      '星期一',
      '星期二',
      '星期三',
      '星期四',
      '星期五',
      '星期六'
    ];
    let date = new Date(timeStamp);
    let o = {
      'M+': date.getMonth() + 1,
      'D+': date.getDate(),
      'h+': date.getHours() % 12,
      'H+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      'S+': date.getMilliseconds(),
      'W+': week[date.getDay()]
    };

    if (/(Y+)/.test(format))
      format = format.replace(
        RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length)
      );
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(format))
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1
            ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length)
        );
    }
    // 中文分割时,去除多余的0
    if (new RegExp(/[\u4e00-\u9fa5]/).test(format)) {
      format = format.replace('年0', '年').replace('月0', '月');
    }
    return format;
  }
};
 
 
 
import Vue from 'vue';
import router from '@/router';
/**
 * jump 页面跳转
 * @path {string} 页面路径或url地址
 * @query {object} 参数对象
 * @newTab {boolean} 是否新页面打开
 */
Vue.prototype.$jump = function(path, query = {}, newTab) {
  if (!path) return false;
  if (path.match(new RegExp('^(http|www)'))) {
    location.href = path;
    return false;
  }
  let routerData = {
    path,
    query
  };
  !newTab ? router.push(routerData) : window.open(routerData.href);
};
this.$jump('/rule');
原型里面封装方法

猜你喜欢

转载自www.cnblogs.com/kittyjingjing/p/13384177.html