JS/VUE 自定义效验 统一社会信用代码 营业执照注册号

// 统一社会信用代码

export const socialUniformCreditCode = (creditCode) => {
  if (creditCode.length != 18) {
    return false;
  }
  var sum = 0;
  var arr = new Array();
  arr = creditCode.split('');
  for (var i = 0; i < arr.length - 1; i++) {
    var c = arr[i];
    if (typeof c == 'undefined') {
      return false;
    }
    var a = getMapC(c); //获得代码对应数值
    if (typeof a == 'undefined') {
      return false;
    }
    var b = getMapW((i + 1).toString()); //获得当前位数权重
    if (typeof b == 'undefined') {
      return false;
    }
    sum += a * b;
    if (typeof sum == 'undefined') {
      return false;
    }
  }

  var mod = sum % 31;
  var res = getMapR((31 - mod).toString()); //计算校验码
  if (typeof res == 'undefined') {
    return false;
  }
  if (arr[arr.length - 1] != res.toString()) {
    return false;
  }
  return true;
};

function getMapC(pa) {
  var result;
  switch (pa) {
    case '0':
      result = 0;
      break;
    case '1':
      result = 1;
      break;
    case '2':
      result = 2;
      break;
    case '3':
      result = 3;
      break;
    case '4':
      result = 4;
      break;
    case '5':
      result = 5;
      break;
    case '6':
      result = 6;
      break;
    case '7':
      result = 7;
      break;
    case '8':
      result = 8;
      break;
    case '9':
      result = 9;
      break;
    case 'A':
      result = 10;
      break;
    case 'B':
      result = 11;
      break;
    case 'C':
      result = 12;
      break;
    case 'D':
      result = 13;
      break;
    case 'E':
      result = 14;
      break;
    case 'F':
      result = 15;
      break;
    case 'G':
      result = 16;
      break;
    case 'H':
      result = 17;
      break;
    case 'J':
      result = 18;
      break;
    case 'K':
      result = 19;
      break;
    case 'L':
      result = 20;
      break;
    case 'M':
      result = 21;
      break;
    case 'N':
      result = 22;
      break;
    case 'P':
      result = 23;
      break;
    case 'Q':
      result = 24;
      break;
    case 'R':
      result = 25;
      break;
    case 'T':
      result = 26;
      break;
    case 'U':
      result = 27;
      break;
    case 'W':
      result = 28;
      break;
    case 'X':
      result = 29;
      break;
    case 'Y':
      result = 30;
      break;
  }
  return result;
}

function getMapW(pa) {
  var result;
  switch (pa) {
    case '1':
      result = 1;
      break;
    case '2':
      result = 3;
      break;
    case '3':
      result = 9;
      break;
    case '4':
      result = 27;
      break;
    case '5':
      result = 19;
      break;
    case '6':
      result = 26;
      break;
    case '7':
      result = 16;
      break;
    case '8':
      result = 17;
      break;
    case '9':
      result = 20;
      break;
    case '10':
      result = 29;
      break;
    case '11':
      result = 25;
      break;
    case '12':
      result = 13;
      break;
    case '13':
      result = 8;
      break;
    case '14':
      result = 24;
      break;
    case '15':
      result = 10;
      break;
    case '16':
      result = 30;
      break;
    case '17':
      result = 28;
      break;
  }
  return result;
}

function getMapR(pa) {
  var result;
  switch (pa) {
    case '0':
      result = '0';
      break;
    case '1':
      result = '1';
      break;
    case '2':
      result = '2';
      break;
    case '3':
      result = '3';
      break;
    case '4':
      result = '4';
      break;
    case '5':
      result = '5';
      break;
    case '6':
      result = '6';
      break;
    case '7':
      result = '7';
      break;
    case '8':
      result = '8';
      break;
    case '9':
      result = '9';
      break;
    case '10':
      result = 'A';
      break;
    case '11':
      result = 'B';
      break;
    case '12':
      result = 'C';
      break;
    case '13':
      result = 'D';
      break;
    case '14':
      result = 'E';
      break;
    case '15':
      result = 'F';
      break;
    case '16':
      result = 'G';
      break;
    case '17':
      result = 'H';
      break;
    case '18':
      result = 'J';
      break;
    case '19':
      result = 'K';
      break;
    case '20':
      result = 'L';
      break;
    case '21':
      result = 'M';
      break;
    case '22':
      result = 'N';
      break;
    case '23':
      result = 'P';
      break;
    case '24':
      result = 'Q';
      break;
    case '25':
      result = 'R';
      break;
    case '26':
      result = 'T';
      break;
    case '27':
      result = 'U';
      break;
    case '28':
      result = 'W';
      break;
    case '29':
      result = 'X';
      break;
    case '30':
      result = 'Y';
      break;
    case '31':
      result = '0';
      break;
  }
  return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值