You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const leftColor = {
HZ: "#008c5e",
HL: "#008c5e",
HO: "#008c5e",
HF: "#008c5e",
NZ: "#7f8c95",
NL: "#7f8c95",
NO: "#7f8c95",
NF: "#7f8c95",
}
const rightTopColor = {
HZ: "#008c5e",
HL: "red",
HO: "#7f8c95",
HF: "#008c5e",
NZ: "#008c5e",
NL: "red",
NO: "#7f8c95",
NF: "#008c5e",
}
//颜色处理
export function handleColor(data) {
if (checkType(data) == "array") {
data.map((item) => {
if (!item.color) {
item.leftColor = "#7b75ff";
item.rightTopColor = "#7b75ff";
return;
}
for (let key in leftColor) {
if (item.color == key) {
item.leftColor = leftColor[key];
item.rightTopColor = rightTopColor[key];
}
}
});
return data;
} else if (checkType(data) == "object") {
if (!data.color) {
data.leftColor = "#7b75ff";
data.rightTopColor = "#7b75ff";
return;
}
for (let key in leftColor) {
if (data.color == key) {
data.leftColor = leftColor[key];
data.rightTopColor = rightTopColor[key];
}
}
return data;
} else {
return data;
}
}
function checkType(value) {
if (Array.isArray(value)) {
return "array";
} else if (value !== null && typeof value === "object") {
return "object";
} else {
return "neither";
}
}
//根据身份证号算年纪/岁数等
export function validateAndParseIDCard(idCard, type) {
// 验证身份证号格式
const idCardPattern =
/^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}([0-9Xx])$/;
if (!idCardPattern.test(idCard)) {
return "-";
}
// 提取出生年月
const birthYear =
idCard.length === 18 ?
idCard.substring(6, 10) :
`19${idCard.substring(6, 8)}`;
const birthMonth =
idCard.length === 18 ? idCard.substring(10, 12) : idCard.substring(8, 10);
const birthDay =
idCard.length === 18 ? idCard.substring(12, 14) : idCard.substring(10, 12);
// 计算年龄
const today = new Date();
const birthDate = new Date(`${birthYear}-${birthMonth}-${birthDay}`);
let age = today.getFullYear() - birthDate.getFullYear();
if (
today.getMonth() < birthDate.getMonth() ||
(today.getMonth() === birthDate.getMonth() &&
today.getDate() < birthDate.getDate())
) {
age--;
}
// 提取性别第17位奇数为男偶数为女
const genderCode =
idCard.length === 18 ? idCard.substring(16, 17) : idCard.substring(14, 15);
const gender = genderCode % 2 === 0 ? "女" : "男";
// 根据type返回对应的信息
switch (type) {
case "年龄":
return age;
case "性别":
return gender;
case "出生年月":
return `${birthYear}-${birthMonth}-${birthDay}`;
default:
return "无效的type参数";
}
}
//版本信息
export function getVersions() {
return new Promise((resolve, reject) => {
uni.request({
url: 'http://www.jichuanglanhai.com/demo/appVersion.json',
data: {
time: new Date().getTime(),
},
success: (res) => {
resolve(res.data.taicangVersion)
},
fail: (error) => {
uni.showToast({
title: '获取版本信息失败!',
icon: 'none'
})
reject()
}
})
})
}