ipink-util
Version:
util.js
171 lines (168 loc) • 4.69 kB
JavaScript
import { isObject, isArray } from './is.mjs';
import { compareVersion } from './util.mjs';
const getPageUrl = () => {
if (typeof window !== "undefined" && window && window.location) {
return window.location.href;
}
if (typeof getCurrentPages !== "function") return "";
var pages = getCurrentPages();
var currentPage = pages[pages.length - 1];
var url = currentPage?.route || "";
var options = currentPage && currentPage.options;
currentPage = url + "?";
for (var key in options) {
var value = options[key];
currentPage += key + "=" + value + "&";
}
currentPage = currentPage.substring(0, currentPage.length - 1);
return "/" + currentPage;
};
function getAgeByBirth(birthDate) {
const r = birthDate.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return 0;
const rArr = r.map((item) => +item);
const d = new Date(rArr[1], rArr[3] - 1, rArr[4]);
if (d.getFullYear() == rArr[1] && d.getMonth() + 1 == rArr[3] && d.getDate() == rArr[4]) {
const Y = (/* @__PURE__ */ new Date()).getFullYear();
return Y - rArr[1];
}
return 0;
}
function getBirthDays(birthDate) {
let now = Date.now();
let bir = new Date(birthDate.split("-").join("/")).getTime();
let date = (now - bir) / 864e5;
return date;
}
function getBirthByID(idCard) {
let birthday = "";
if (idCard != null && idCard != "") {
if (idCard.length == 15) {
birthday = "19" + idCard.slice(6, 12);
} else if (idCard.length == 18) {
birthday = idCard.slice(6, 14);
}
birthday = birthday.replace(/(.{4})(.{2})/, "$1/$2/");
}
return birthday;
}
function getJson(target) {
let json = {};
try {
json = JSON.parse(target);
} catch (err) {
if (!isObject(json) && !isArray(json)) json = {};
}
return json;
}
function getJsonMember(target, key) {
let json = getJson(target);
return json[key];
}
function getIdCardInfo(sId, type) {
if (sId.length == 15) {
sId = sId.replace(/([\d]{6})(\d{9})/, "$119$2x");
}
const CityArray = {
11: "北京",
12: "天津",
13: "河北",
14: "山西",
15: "内蒙古",
21: "辽宁",
22: "吉林",
23: "黑龙江",
31: "上海",
32: "江苏",
33: "浙江",
34: "安徽",
35: "福建",
36: "江西",
37: "山东",
41: "河南",
42: "湖北",
43: "湖南",
44: "广东",
45: "广西",
46: "海南",
50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门",
91: "国外"
};
let iSum = 0;
if (!/^\d{17}(\d|x)$/i.test(sId)) return "";
sId = sId.replace(/x$/i, "a");
if (CityArray[parseInt(sId.substr(0, 2))] == null) {
console.warn("Error:非法地区");
return "";
}
let sBirthday = sId.substr(6, 4) + "/" + Number(sId.substr(10, 2)) + "/" + Number(sId.substr(12, 2));
const d = new Date(sBirthday);
if (sBirthday != d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()) {
console.warn("Error:非法生日");
return "";
}
for (var i = 17; i >= 0; i--) iSum += Math.pow(2, i) % 11 * parseInt(sId.charAt(17 - i), 11);
if (iSum % 11 != 1) {
console.warn("Error:非法证号");
return "";
}
if (type == 0) {
return CityArray[parseInt(sId.substr(0, 2))];
} else if (type == 1) {
return sBirthday;
} else {
return +sId.substr(16, 1) % 2 ? "男" : "女";
}
}
const getCanvas = (id, ctx) => {
try {
if (!uni || !uni.getSystemInfoSync) return Promise.resolve(null);
let context = null;
let version = uni.getSystemInfoSync().SDKVersion;
return new Promise((resolve, reject) => {
if (version && compareVersion(version, "2.9.0") >= 0) {
let view = uni.createSelectorQuery().in(ctx).select("#" + id);
view.fields({
node: true
}, () => {
}).exec(
(data) => {
let Canvas = data[0].node;
context = Canvas ? Canvas.getContext("2d") : uni.createCanvasContext(id, ctx);
resolve(context);
}
);
} else {
context = uni.createCanvasContext(id, ctx);
resolve(context);
}
});
} catch (e) {
return Promise.resolve(null);
}
};
const getStringSize = (str, key) => {
let totalBytes = new Blob([str]).size;
let kb = totalBytes / 1024;
let mb = kb / 1024;
let gb = mb / 1024;
const map = {
gb,
mb,
kb
};
return key ? map[key] : map;
};
export { getAgeByBirth, getBirthByID, getBirthDays, getCanvas, getIdCardInfo, getJson, getJsonMember, getPageUrl, getStringSize };