@bchr/bchr-front-lib 0.4.5 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ const CNKits = {
2
+
3
+ /**
4
+ * 是否有效的中国大陆身份证号码(含澳门)
5
+ */
6
+ isCNMainLandIDCardNo(strOfIDCardNo) {
7
+ const provincesAndCities = [
8
+ '11', // 北京
9
+ '12', // 天津
10
+ '13', // 河北
11
+ '14', // 山西
12
+ '15', // 内蒙古
13
+ '21', // 辽宁
14
+ '22', // 吉林
15
+ '23', // 黑龙江
16
+ '31', // 上海
17
+ '32', // 江苏
18
+ '33', // 浙江
19
+ '34', // 安徽
20
+ '35', // 福建
21
+ '36', // 江西
22
+ '37', // 山东
23
+ '41', // 河南
24
+ '42', // 湖北
25
+ '43', // 湖南
26
+ '44', // 广东
27
+ '45', // 广西
28
+ '46', // 海南
29
+ '50', // 重庆
30
+ '51', // 四川
31
+ '52', // 贵州
32
+ '53', // 云南
33
+ '54', // 西藏
34
+ '61', // 陕西
35
+ '62', // 甘肃
36
+ '63', // 青海
37
+ '64', // 宁夏
38
+ '65', // 新疆
39
+ '71', // 台湾
40
+ '81', // 香港
41
+ '82', // 澳门
42
+ '91', // 国外
43
+ ];
44
+
45
+ const powers = ['7', '9', '10', '5', '8', '4', '2', '1', '6', '3', '7', '9', '10', '5', '8', '4', '2'];
46
+
47
+ const parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
48
+
49
+ const checkAddressCode = addressCode => provincesAndCities.includes(addressCode);
50
+
51
+ const checkBirthDayCode = (birDayCode) => {
52
+ const yyyy = parseInt(birDayCode.substring(0, 4), 10);
53
+ const mm = parseInt(birDayCode.substring(4, 6), 10);
54
+ const dd = parseInt(birDayCode.substring(6), 10);
55
+ const xdata = new Date(yyyy, mm - 1, dd);
56
+ if (xdata > new Date()) {
57
+ return false;
58
+ // eslint-disable-next-line max-len
59
+ } else if ((xdata.getFullYear() === yyyy) && (xdata.getMonth() === mm - 1) && (xdata.getDate() === dd)) {
60
+ return true;
61
+ }
62
+ return false;
63
+ };
64
+
65
+ const getParityBit = (idCardNo) => {
66
+ let id17 = idCardNo.substring(0, 17);
67
+
68
+ let power = 0;
69
+ for (let i = 0; i < 17; i++) {
70
+ power += parseInt(id17.charAt(i), 10) * parseInt(powers[i], 10);
71
+ }
72
+
73
+ let mod = power % 11;
74
+ return parityBit[mod];
75
+ };
76
+
77
+ const checkParityBit = idCardNo => getParityBit(idCardNo) === idCardNo.charAt(17).toUpperCase();
78
+
79
+
80
+ const check15IdCardNo = (idCardNo) => {
81
+ let check = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}$/.test(idCardNo);
82
+ if (!check) return false;
83
+ let addressCode = idCardNo.substring(0, 2);
84
+ check = checkAddressCode(addressCode);
85
+ if (!check) return false;
86
+ let birDayCode = `19${idCardNo.substring(6, 12)}`;
87
+ check = checkBirthDayCode(birDayCode);
88
+ if (!check) return false;
89
+ return true;
90
+ };
91
+
92
+ const check18IdCardNo = (idCardNo) => {
93
+ let check = /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/.test(idCardNo);
94
+ if (!check) return false;
95
+ let addressCode = idCardNo.substring(0, 2);
96
+ check = checkAddressCode(addressCode);
97
+ if (!check) return false;
98
+ let birDayCode = idCardNo.substring(6, 14);
99
+ check = checkBirthDayCode(birDayCode);
100
+ if (!check) return false;
101
+ return checkParityBit(idCardNo);
102
+ };
103
+
104
+ const checkIdCardNo = (idCardNo) => {
105
+ if (idCardNo === null || idCardNo === void 0) {
106
+ return false;
107
+ }
108
+
109
+ idCardNo = idCardNo + "";
110
+
111
+ let check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo);
112
+ if (!check) return false;
113
+
114
+ if (idCardNo.length === 15) {
115
+ return check15IdCardNo(idCardNo);
116
+ }
117
+
118
+ return check18IdCardNo(idCardNo);
119
+ };
120
+
121
+ return checkIdCardNo(strOfIDCardNo);
122
+ },
123
+
124
+ /**
125
+ * 是否有效的中国香港身份证号码
126
+ */
127
+ isHongKongIDCardNo(strOfIDCardNo) {
128
+ if (strOfIDCardNo === null || strOfIDCardNo === void 0) {
129
+ return false;
130
+ }
131
+
132
+ strOfIDCardNo = strOfIDCardNo + "";
133
+
134
+ // sanitize user input
135
+ strOfIDCardNo = strOfIDCardNo.trim();
136
+
137
+ // HKID number starts with 1 or 2 letters, followed by 6 digits,
138
+ // then a checksum contained in square / round brackets or nothing
139
+ const regexHKID = /^[A-Z]{1,2}[0-9]{6}((\([0-9A]\))|(\[[0-9A]\])|([0-9A]))$/;
140
+ const regexIsDigit = /^[0-9]$/;
141
+
142
+ // convert the user input to all uppercase and apply regex
143
+ strOfIDCardNo = strOfIDCardNo.toUpperCase();
144
+ if (!regexHKID.test(strOfIDCardNo)) return false;
145
+ strOfIDCardNo = strOfIDCardNo.replace(/\[|\]|\(|\)/g, '');
146
+
147
+ if (strOfIDCardNo.length === 8) strOfIDCardNo = `3${strOfIDCardNo}`;
148
+ let checkSumVal = 0;
149
+ for (let i = 0; i <= 7; i++) {
150
+ let convertedChar;
151
+ if (!regexIsDigit.test(strOfIDCardNo[i])) convertedChar = (strOfIDCardNo[i].charCodeAt(0) - 55) % 11;
152
+ else convertedChar = strOfIDCardNo[i];
153
+ checkSumVal += (convertedChar * (9 - i));
154
+ }
155
+ checkSumVal %= 11;
156
+
157
+ let checkSumConverted;
158
+ if (checkSumVal === 0) checkSumConverted = '0';
159
+ else if (checkSumVal === 1) checkSumConverted = 'A';
160
+ else checkSumConverted = String(11 - checkSumVal);
161
+ if (checkSumConverted === strOfIDCardNo[strOfIDCardNo.length - 1]) return true;
162
+ return false;
163
+ },
164
+
165
+ /**
166
+ * 是否有效的中国台湾身份证号码
167
+ */
168
+ isTaiWanIDCardNo(strOfIDCardNo) {
169
+ if (strOfIDCardNo === null || strOfIDCardNo === void 0) {
170
+ return false;
171
+ }
172
+
173
+ strOfIDCardNo = strOfIDCardNo + "";
174
+
175
+ const ALPHABET_CODES = {
176
+ A: 10,
177
+ B: 11,
178
+ C: 12,
179
+ D: 13,
180
+ E: 14,
181
+ F: 15,
182
+ G: 16,
183
+ H: 17,
184
+ I: 34,
185
+ J: 18,
186
+ K: 19,
187
+ L: 20,
188
+ M: 21,
189
+ N: 22,
190
+ O: 35,
191
+ P: 23,
192
+ Q: 24,
193
+ R: 25,
194
+ S: 26,
195
+ T: 27,
196
+ U: 28,
197
+ V: 29,
198
+ W: 32,
199
+ X: 30,
200
+ Y: 31,
201
+ Z: 33,
202
+ };
203
+
204
+ const sanitized = strOfIDCardNo.trim().toUpperCase();
205
+
206
+ if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;
207
+
208
+ return Array.from(sanitized).reduce((sum, number, index) => {
209
+ if (index === 0) {
210
+ const code = ALPHABET_CODES[number];
211
+
212
+ return ((code % 10) * 9) + Math.floor(code / 10);
213
+ }
214
+
215
+ if (index === 9) {
216
+ return ((10 - (sum % 10)) - Number(number)) % 10 === 0;
217
+ }
218
+
219
+ return sum + (Number(number) * (9 - index));
220
+ }, 0);
221
+ },
222
+
223
+ isHasChineseWord(str) {
224
+ return /[\u4E00-\u9FFF]+/g.test(str)
225
+ },
226
+
227
+ isAllChineseWord(str) {
228
+ return /^[\u4E00-\u9FFF]+$/.test(str)
229
+ },
230
+
231
+ isCNMainLandMobilePhone(str) {
232
+ return /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/.test(str)
233
+ }
234
+ }
235
+
236
+ export default CNKits;
@@ -0,0 +1,99 @@
1
+ const encryptionMap = {
2
+ 'a': 'R',
3
+ 'b': '5',
4
+ 'c': 'g',
5
+ 'd': 'N',
6
+ 'e': '7',
7
+ 'f': 'y',
8
+ 'g': '1',
9
+ 'h': 'W',
10
+ 'i': 'b',
11
+ 'j': '9',
12
+ 'k': 'K',
13
+ 'l': '2',
14
+ 'm': 'E',
15
+ 'n': '4',
16
+ 'o': 'i',
17
+ 'p': 'L',
18
+ 'q': '8',
19
+ 'r': 'x',
20
+ 's': 'Q',
21
+ 't': '3',
22
+ 'u': 't',
23
+ 'v': 'P',
24
+ 'w': 'D',
25
+ 'x': 'f',
26
+ 'y': 'Z',
27
+ 'z': 'e',
28
+ 'A': '0',
29
+ 'B': 'm',
30
+ 'C': 'H',
31
+ 'D': '6',
32
+ 'E': 'u',
33
+ 'F': 'J',
34
+ 'G': 'a',
35
+ 'H': 'C',
36
+ 'I': 'v',
37
+ 'J': 's',
38
+ 'K': 'p',
39
+ 'L': 'U',
40
+ 'M': 'j',
41
+ 'N': 'M',
42
+ 'O': 'o',
43
+ 'P': 'd',
44
+ 'Q': 'T',
45
+ 'R': 'k',
46
+ 'S': 'V',
47
+ 'T': 'z',
48
+ 'U': 'I',
49
+ 'V': 'h',
50
+ 'W': 'L',
51
+ 'X': 'W',
52
+ 'Y': 'A',
53
+ 'Z': 'q',
54
+ '0': 'S',
55
+ '1': 'B',
56
+ '2': 'n',
57
+ '3': 'F',
58
+ '4': 'X',
59
+ '5': 'r',
60
+ '6': 'c',
61
+ '7': 'G',
62
+ '8': 'O',
63
+ '9': 'Y'
64
+ };
65
+
66
+ // 解密字符映射表,与加密映射表相反
67
+ const decryptionMap = {};
68
+ for (let key in encryptionMap) {
69
+ decryptionMap[encryptionMap[key]] = key;
70
+ }
71
+
72
+ const CharReplaceUtils = {
73
+ encrypt(data){
74
+ let encrypted = '';
75
+ for (let i = 0; i < data.length; i++) {
76
+ let char = data[i];
77
+ if (encryptionMap.hasOwnProperty(char)) {
78
+ encrypted += encryptionMap[char];
79
+ } else {
80
+ encrypted += char;
81
+ }
82
+ }
83
+ return encrypted;
84
+ },
85
+ decrypt(data){
86
+ let decrypted = '';
87
+ for (let i = 0; i < data.length; i++) {
88
+ let char = data[i];
89
+ if (decryptionMap.hasOwnProperty(char)) {
90
+ decrypted += decryptionMap[char];
91
+ } else {
92
+ decrypted += char;
93
+ }
94
+ }
95
+ return decrypted;
96
+ }
97
+ }
98
+
99
+ export default CharReplaceUtils;
@@ -0,0 +1,54 @@
1
+ import lodash from "lodash";
2
+
3
+ const DES24JS = {
4
+
5
+ supportDES24JS() {
6
+ return lodash.isFunction(window.strEnc) && lodash.isFunction(window.strDec);
7
+ },
8
+
9
+ decrypt(encodedString) {
10
+ if (!lodash.isFunction(window.strDec)) {
11
+ throw new Error("Current not support DES Decryption.");
12
+ }
13
+
14
+ if (encodedString === null || encodedString === void 0) {
15
+ return null;
16
+ }
17
+
18
+ if (!lodash.isString(encodedString)) {
19
+ throw new Error("Invalid input value for DES Decryption.");
20
+ }
21
+
22
+ try {
23
+ return window.strDec(encodedString);
24
+ }
25
+ catch (e) {
26
+ console.error(e);
27
+ return null;
28
+ }
29
+ },
30
+
31
+ encrypt(rawString) {
32
+ if (!lodash.isFunction(window.strEnc)) {
33
+ throw new Error("Current not support DES Encryption.");
34
+ }
35
+
36
+ if (rawString === null || rawString === void 0) {
37
+ return null;
38
+ }
39
+
40
+ if (!lodash.isString(rawString)) {
41
+ throw new Error("Invalid input value for DES Encryption.");
42
+ }
43
+
44
+ try {
45
+ return window.Error(rawString);
46
+ }
47
+ catch (e) {
48
+ console.error(e);
49
+ return null;
50
+ }
51
+ }
52
+ }
53
+
54
+ export default DES24JS;
@@ -0,0 +1,136 @@
1
+ import moment from "moment";
2
+ import lodash from "lodash";
3
+ import LangTypes from "./LangTypes"
4
+
5
+
6
+ const DateTimes = {
7
+
8
+ strDateOfLen10(date) {
9
+ if (date === null || date === void 0) {
10
+ return "";
11
+ }
12
+
13
+ let dateTime = null;
14
+ if (lodash.isInteger(date)) {
15
+ dateTime = moment(new Date(date));
16
+ } else {
17
+ dateTime = moment(date);
18
+ }
19
+
20
+ if (dateTime.isValid()) {
21
+ return dateTime.format("YYYY-MM-DD")
22
+ }
23
+
24
+ return "";
25
+ },
26
+
27
+ strDateOfLen8(date) {
28
+ if (date === null || date === void 0) {
29
+ return "";
30
+ }
31
+
32
+ let dateTime = null;
33
+ if (lodash.isInteger(date)) {
34
+ dateTime = moment(new Date(date));
35
+ } else {
36
+ dateTime = moment(date);
37
+ }
38
+
39
+ if (dateTime.isValid()) {
40
+ return dateTime.format("YYYYMMDD")
41
+ }
42
+
43
+ return "";
44
+ },
45
+
46
+ strDateTimeOfLen19(date) {
47
+ if (date === null || date === void 0) {
48
+ return "";
49
+ }
50
+
51
+ let dateTime = null;
52
+ if (lodash.isInteger(date)) {
53
+ dateTime = moment(new Date(date));
54
+ } else {
55
+ dateTime = moment(date);
56
+ }
57
+
58
+ if (dateTime.isValid()) {
59
+ return dateTime.format("YYYY-MM-DD HH:mm:ss")
60
+ }
61
+
62
+ return "";
63
+ },
64
+
65
+ strDateTimeOfLen14(date) {
66
+ if (date === null || date === void 0) {
67
+ return "";
68
+ }
69
+
70
+ let dateTime = null;
71
+ if (lodash.isInteger(date)) {
72
+ dateTime = moment(new Date(date));
73
+ } else {
74
+ dateTime = moment(date);
75
+ }
76
+
77
+ if (dateTime.isValid()) {
78
+ return dateTime.format("YYYYMMDDHHmmss")
79
+ }
80
+
81
+ return "";
82
+ },
83
+
84
+ asHumanReadablePastTime(dateValue, format = 'YYYY-MM-DD HH:mm:ss') {
85
+ let pastDate = LangTypes.asDateValue(dateValue);
86
+ if (!pastDate) {
87
+ return "";
88
+ }
89
+
90
+ let currTS = new Date().getTime()
91
+ let pastTS = pastDate.getTime();
92
+ let time = currTS - pastTS;
93
+ if (time < 10 * 1000) {
94
+ return '刚刚'
95
+ } else if (time < 60 * 1000 && time >= 10 * 1000) {
96
+ let s = Math.floor(time / 1000)
97
+ return `${s}秒前`
98
+ } else if (time < 60 * 60 * 1000 && time >= 60 * 1000) {
99
+ let s = Math.floor(time / (60 * 1000))
100
+ return `${s}分钟前`
101
+ } else if (time < 24 * 60 * 60 * 1000 && time >= 60 * 60 * 1000) {
102
+ let s = Math.floor(time / (60 * 60 * 1000))
103
+ return `${s}小时前`
104
+ } else if (time < 7 * 24 * 60 * 60 * 1000 && time >= 24 * 60 * 60 * 1000) {
105
+ let s = Math.floor(time / (24 * 60 * 60 * 1000))
106
+ return `${s}天前`
107
+ } else {
108
+ return moment(pastDate).format(format);
109
+ }
110
+ },
111
+
112
+ timeDiff(startDate, endDate) {
113
+ let startTime = new Date(startDate).getTime();
114
+ let endTime = new Date(endDate).getTime();
115
+ let usedTime = endTime - startTime;
116
+
117
+ let day = Math.floor(usedTime / (24 * 3600 * 1000));
118
+ let leave1 = usedTime % (24 * 3600 * 1000);
119
+ let hours = Math.floor(leave1 / (3600 * 1000));
120
+ //计算相差分钟数
121
+ let leave2 = leave1 % (3600 * 1000);
122
+ let minutes = Math.floor(leave2 / (60 * 1000));
123
+ //计算相差秒数
124
+ let leave3 = leave2 % (60 * 1000);
125
+ let second = Math.round(leave3 / 1000);
126
+
127
+ if (day) {
128
+ return {day, hours, minutes, second};
129
+ } else if (!day && hours) {
130
+ return {hours, minutes, second};
131
+ }
132
+ return {minutes, second};
133
+ }
134
+ }
135
+
136
+ export default DateTimes;
@@ -0,0 +1,103 @@
1
+ import lodash, {isPlainObject} from "lodash";
2
+ import LangTypes from "./LangTypes";
3
+
4
+ const convertFormValues2VOUsingVOFormat = function (values) {
5
+ if (!values) {
6
+ return null;
7
+ }
8
+
9
+ let vo = {};
10
+ let attrs = Object.keys(values);
11
+ for (let i = 0, length = attrs.length; i < length; i++) {
12
+ let attr = attrs[i];
13
+ let attrValue = values[attr];
14
+ if (attrValue === null || attrValue === void 0) {
15
+ continue;
16
+ }
17
+
18
+ if (lodash.isString(attrValue)) {
19
+ vo[attr] = attrValue;
20
+ }
21
+ else if (lodash.isNumber(attrValue)) {
22
+ if (!Number.isNaN(attrValue)) {
23
+ vo[attr] = attrValue;
24
+ }
25
+ }
26
+ else if (lodash.isBoolean(attrValue)) {
27
+ vo[attr] = attrValue;
28
+ }
29
+ else if (lodash.isDate(attrValue)) {
30
+ vo[attr] = attrValue;
31
+ }
32
+ else if (lodash.isArray(attrValue)) {
33
+ vo[attr] = attrValue.join(",");
34
+ }
35
+ else if (lodash.isObject(attrValue)
36
+ && attr.endsWith("_pk")
37
+ && (attrValue.label !== null)
38
+ && (attrValue.value !== null)
39
+ && Object.keys(attrValue)) {
40
+ if (attrValue._inputCreateOnly === true) {
41
+ vo[attr] = -1;
42
+ vo[attr.substring(0, attr.length - 3) + "_ext_name"] = attrValue.label;
43
+ }
44
+ else {
45
+ vo[attr] = attrValue.value;
46
+ vo[attr.substring(0, attr.length - 3) + "_ext_name"] = attrValue.label;
47
+ }
48
+
49
+ }
50
+ }
51
+ return vo;
52
+ }
53
+
54
+ const convertVO2FormValuesUsingFormValueFormat = function (voValue, attrNameArrayForMultiSelect) {
55
+ if (!voValue) {
56
+ return null;
57
+ }
58
+
59
+ let formValues = {};
60
+ let attrs = Object.keys(voValue);
61
+ for (let i = 0, length = attrs.length; i < length; i++) {
62
+ let attr = attrs[i];
63
+ let attrValue = voValue[attr];
64
+ if (attrValue === null) {
65
+ continue;
66
+ }
67
+
68
+ if (isPlainObject(attrValue)) {
69
+ formValues[attr] = convertVO2FormValuesUsingFormValueFormat(attrValue, attrNameArrayForMultiSelect);
70
+ }
71
+ else if (attr.endsWith("_pk")) {
72
+ formValues[attr] = {
73
+ value: attrValue,
74
+ label: voValue[attr.substring(0, attr.length - 3) + "_ext_name"]
75
+ };
76
+ }
77
+ else if (attr.endsWith("status")) {
78
+ formValues[attr] = LangTypes.asBooleanValue(attrValue);
79
+ }
80
+ else {
81
+ formValues[attr] = attrValue;
82
+ }
83
+ }
84
+
85
+ if (attrNameArrayForMultiSelect && attrNameArrayForMultiSelect.length > 0) {
86
+ for (let i = 0, length = attrNameArrayForMultiSelect.length; i < length; i++) {
87
+ let attrName = attrNameArrayForMultiSelect[i];
88
+ if (attrName) {
89
+ if (formValues[attrName] && formValues[attrName].split) {
90
+ formValues[attrName] = formValues[attrName].split(",");
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ return formValues;
97
+ }
98
+
99
+
100
+ export {
101
+ convertFormValues2VOUsingVOFormat,
102
+ convertVO2FormValuesUsingFormValueFormat
103
+ }