统一社会信用代码校验和生成

23 篇文章 0 订阅

java版

import java.util.HashMap;
import java.util.Map;

public class CompanySocialUtil {

	private static final int[] orgCodeWeight = {3, 7, 9, 10, 5, 8, 4, 2};
	
	private static final int[] socialIdWeight = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};
	
	private static final Map<String, Integer> socialIdIndex = new HashMap<String, Integer>();
	
	private static final String[] socialBasic = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
			  									 "A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
			  									 "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X", "Y"};
	
	static {
		for (int i = 0; i < socialBasic.length; i++) {
			socialIdIndex.put(socialBasic[i], i);
		}
	}
	
	/**
	 * 校验统一社会信用代码
	 * @param str 统一社会信用代码
	 * @return true 校验通过,else false
	 */
	public static boolean checkCompanySocialId(String str) {
		if (str == null || str.length() != 18) {
			return false;
		}
		return str.equals(createCompanySocialId(str));
	}
	
	/**
	 * 生成统一社会信用代码
	 * @param str 至少统一社会信用代码前16位
	 * @return 18位统一社会信用代码  if str足16位且符合规则, else 空字符串
	 */
	public static String createCompanySocialId(String str) {
		if (str == null || str.length() < 16) {
			return "";
		}
		String socialCode = str.substring(0, 16);
		if (!socialCode.matches("^(?:11|12|13|19|51|52|53|59|91|92|93|Y1)\\d{6}[\\dA-Z^IOZSV]{8}$")) {
			return "";
		}
		String orgCode = socialCode.substring(8, 16);
		int orgCheckCode = 0;
		String orgCheckCodeStr = "";
		for (int i = 0, len = orgCode.length(); i < len; i++) {
			orgCheckCode += Integer.parseInt(orgCode.substring(i, i + 1), 36) * orgCodeWeight[i];
		}
		orgCheckCode = 11 - orgCheckCode % 11;
		if (orgCheckCode == 11) {
			orgCheckCodeStr = "0";
		} else if (orgCheckCode == 10) {
			orgCheckCodeStr = "X";
		} else {
			orgCheckCodeStr = orgCheckCode + "";
		}
		socialCode += orgCheckCodeStr;
		
		int socialCheckCode = 0;
		for (int i = 0, len = socialCode.length(); i < len; i++) {
			socialCheckCode += socialIdIndex.get(socialCode.substring(i, i + 1)) * socialIdWeight[i];
		}
		socialCheckCode = 31 - socialCheckCode % 31;
		String socialCheckCodeStr = socialBasic[socialCheckCode % 31];
		socialCode += socialCheckCodeStr;
		return socialCode;
	}
	
}

javascript版

(function(window) {
			/**
			 * 生成统一社会信用代码
			 * @param str 至少统一社会信用代码前16位
			 * @return 18位统一社会信用代码  if str足16位且符合规则, else 空字符串
			 */
            window.createCompanySocialId = function (str) {
                if (!str || str.length < 16) {
                    return '';
                }
                return new _CompanySocialId()._createCompanySocialId(str);
            };

			/**
			 * 校验统一社会信用代码
			 * @param str 统一社会信用代码
			 * @return true 校验通过,else false
			 */
            window.checkCompanySocialId = function (str) {
                if (!str || str.length !== 18) {
                    return false
                }
                return new _CompanySocialId()._createCompanySocialId(str.substr(0, 16)) === str;
            };

            function _CompanySocialId() {
                this._orgCode = null;
                this._socialCode = null;
            }

            _CompanySocialId.prototype._createCompanySocialId = function (str) {
                if (!str || str.length < 16) {
                    return '';
                }
                str = str.substr(0, 16);
                if (!/^(?:11|12|13|19|51|52|53|59|91|92|93|Y1)\d{6}[\dA-Z^IOZSV]{8}$/.test(str)) {
                    return '';
                }
                var arr = str.split('');
                var thisSocialId = this;
                var orgCode = arr.slice(8, 16);
                thisSocialId._socialCode = arr.slice();

                var orgCheckCode = 0;
                for (var i = 0; i < orgCode.length; i++) {
                    orgCheckCode += window.parseInt(orgCode[i], 36) * thisSocialId._orgCodeWeight[i];
                }
                orgCheckCode = 11 - orgCheckCode % 11;
                if (orgCheckCode === 11) {
                    orgCheckCode = '0';
                } else if (orgCheckCode === 10) {
                    orgCheckCode = 'X';
                } else {
                    orgCheckCode = orgCheckCode + '';
                }
                thisSocialId._socialCode.push(orgCheckCode);

                var socialCheckCode = 0;
                for (var i = 0; i < thisSocialId._socialCode.length; i++) {
                    socialCheckCode += thisSocialId._socialIdIndex[thisSocialId._socialCode[i]] * thisSocialId._socialIdWeight[i];
                }
                socialCheckCode = 31 - socialCheckCode % 31;
                socialCheckCode = thisSocialId._socialIdBasic[socialCheckCode % 31];
                thisSocialId._socialCode.push(socialCheckCode.toUpperCase());
                return thisSocialId._socialCode.join('');
            };

            _CompanySocialId.prototype._orgCodeWeight = [3, 7, 9, 10, 5, 8, 4, 2];
            _CompanySocialId.prototype._socialIdWeight = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
            _CompanySocialId.prototype._socialIdIndex = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A' : 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27, 'W': 28, 'X': 29, 'Y': 30};
            _CompanySocialId.prototype._socialIdBasic = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'];
        })(window);
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值