index.js 4.02 KB
Newer Older
潘琦 committed
1 2
import Qs from 'qs'

潘琦 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * 取消滚动条
 * @param {*} s
 */
export function noscroll () {
  document.getElementById('app').classList.add('noscroll')
}

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

/**
潘琦 committed
20
 * 根据身份证获取性别 1=男 2=女
潘琦 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 * @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
}
潘琦 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

/**
 * 获取域名后参数
 * @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
  }
}