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);
}
}
public static boolean checkCompanySocialId(String str) {
if (str == null || str.length() != 18) {
return false;
}
return str.equals(createCompanySocialId(str));
}
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) {
window.createCompanySocialId = function (str) {
if (!str || str.length < 16) {
return '';
}
return new _CompanySocialId()._createCompanySocialId(str);
};
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);