
export default {
  name: 'myUtil',

  /**
   * 根据传入的身份证,自动生成生日，性别，年龄,并回调传入的方法
   * @param idCard
   * @param fn
   */
  parseIdCard (idCard, fn) {
    let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (!reg.test(idCard)) {//身份证合法校验
      return false
    }
    let iden = idCard
    let sex = null
    let birth = null
    let myDate = new Date()
    let month = myDate.getMonth() + 1
    let day = myDate.getDate()
    let age = 0

    if (iden.length === 18) {
      age = myDate.getFullYear() - iden.substring(6, 10) - 1
      sex = iden.substring(16, 17)
      birth = iden.substring(6, 10) + '.' + iden.substring(10, 12) + '.' + iden.substring(12, 14)
      if (iden.substring(10, 12) < month || iden.substring(10, 12) == month && iden.substring(12, 14) <= day) age++

    }
    if (iden.length === 15) {
      age = myDate.getFullYear() - iden.substring(6, 8) - 1901
      sex = iden.substring(13, 14)
      birth = '19' + iden.substring(6, 8) + '.' + iden.substring(8, 10) + '.' + iden.substring(10, 12)
      if (iden.substring(8, 10) < month || iden.substring(8, 10) == month && iden.substring(10, 12) <= day) age++
    }

    if (sex % 2 === 0) {
      sex = '2'
    } else {
      sex = '1'
    }

    //最后回调传入的方法
    fn(sex, age, birth);
  },
  /**
   * 判断当前用户是否拥有某个角色
   * @param roleCode
   */
  userHasRole (roleCode) {
    let authorities = this.$store.state.user.authorities
    if (authorities && authorities.length>0){
      authorities.forEach(one =>{
        if (one === 'roleCode'){
          return true;
        }
      })
    }
    return false;
  }

}

