index.js 4.02 KB
import Qs from 'qs'

/**
 * 取消滚动条
 * @param {*} s
 */
export function noscroll () {
  document.getElementById('app').classList.add('noscroll')
}

/**
 * 恢复滚动条
 * @param {*} s
 */
export function renewscroll () {
  document.getElementById('app').classList.remove('noscroll')
}

/**
 * 根据身份证获取性别 1=男 2=女
 * @param {*} s
 */
export function getSexByIdCard (idNumber) {
  let sex = '3'
  if (idNumber && idNumber !== '' && idNumber.length === 18) {
    if (idNumber.substring(16, 17) % 2 === 0) {
      sex = '2'
    } else {
      sex = '1'
    }
  }
  return sex
}

/**
 * 根据身份证获取生日
 * @param {*} s
 */
export function getBirthByIdNumber (idNumber) {
  let birth = ''
  if (idNumber.length === 18) {
    birth = idNumber.substring(6, 14)
  } else if (idNumber.length === 15) {
    birth = '19' + idNumber.substring(6, 12)
  }
  if (birth !== '') {
    birth = birth.replace(/(.{4})(.{2})/, '$1-$2-')
  }
  return birth
}

/**
 * 根据出生年月获取年龄
 * @param {*} s
 */
export function getAgeByBirth (birth) {
  console.log('getAgeByBirth')
  if (!birth || birth === '') {
    return ''
  }
  const nowDate = new Date()
  let year = nowDate.getFullYear()
  let month = nowDate.getMonth() + 1
  let day = nowDate.getDate()

  let birthYear = parseInt(birth.split('-')[0])
  let birthMonth = parseInt(birth.split('-')[1])
  let birthDay = parseInt(birth.split('-')[2])

  let differYear = -1
  let differMonth = -1
  let differDay = -1
  let age = ''
  if (year === birthYear) {
    if (month === birthMonth) {
      differDay = day - birthDay
    } else if (month > birthMonth) {
      differYear = 0
      if (day > birthDay) {
        differMonth = month - birthMonth
        differDay = day - birthDay
      } else if (day < birthDay) {
        differMonth = month - birthMonth - 1
        differDay = new Date(birthYear, birthMonth, 0).getDate() - birthDay + day
      } else {
        differMonth = month - birthMonth
        differDay = 0
      }
    }
  } else if (year > birthYear) {
    differYear = year - birthYear
    if (month > birthMonth) {
      differMonth = month - birthMonth
    } else if (month < birthMonth) {
      differYear = differYear - 1
      differMonth = 12 - (birthMonth - month)
    } else {
      if (day > birthDay) {
        differMonth = birthMonth - month
      } else if (day < birthDay) {
        differYear = differYear - 1
        differMonth = 11
      } else {
        differMonth = birthMonth - month
      }
    }
  }

  if (differYear >= 0 && differMonth >= 0) {
    if (differYear > 0) {
      age = differYear + '岁'
    }
    if (differMonth > 0) {
      age = age + differMonth + '月'
    }
    if (differDay > 0) {
      age = age + differDay + '天'
    }
  } else {
    age = differDay + '天'
  }

  return age
}

/**
 * 计算当天之前多少天日期
 * @param {*} s
 */
export function SubtractDay (day) {
  let date = new Date() // 获取当前时间
  date.setDate(date.getDate() - day)
  return date
}

/**
 * 获取域名后参数
 * @param {*} s
 */
export function getParamsByUrl (url) {
  try {
    let paramstr = url.split('?')
    if (paramstr.length > 1) {
      let paramarr = paramstr[1].split('&')
      const params = {}
      for (const i in paramarr) {
        const obj = paramarr[i].split('=')
        params[obj[0]] = obj[1]
      }
      return params
    } else {
      return {}
    }
  } catch (error) {
    return {}
  }
}

/**
 * 移除域名后指定参数
 * @param {*} s
 */
export function removeUrlParam (key) {
  // let loc = window.location
  let url = window.location.href
  let paramstr = url.split('?')
  let lochref = paramstr[0]
  if (paramstr.length > 1) {
    let paramarr = paramstr[1].split('&')
    const params = {}
    for (const i in paramarr) {
      const obj = paramarr[i].split('=')
      if (key !== obj[0]) {
        params[obj[0]] = obj[1]
      }
    }
    let urlparamstr = Qs.stringify(params)
    if (urlparamstr !== '') {
      return lochref + '?' + urlparamstr
    } else {
      return lochref
    }
  } else {
    return url
  }
}