138 lines
4.0 KiB
JavaScript
138 lines
4.0 KiB
JavaScript
const { ObjectId } = require("mongodb");
|
|
const Sm4Util = require("../../utils/sm4-util");
|
|
|
|
const CARD_STRING_FIELDS = [
|
|
"CardIDCode",
|
|
"CardNum",
|
|
"CardType",
|
|
"Gender",
|
|
"IDNum",
|
|
"Name",
|
|
"PsamTN",
|
|
"Message",
|
|
"flag",
|
|
"ylbxtcqbm",
|
|
];
|
|
|
|
function normalizeCardData(value) {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return null;
|
|
}
|
|
|
|
const data = {
|
|
CanOpen: value.CanOpen === true,
|
|
Success: value.Success === true,
|
|
};
|
|
|
|
for (const field of CARD_STRING_FIELDS) {
|
|
if (value[field] === undefined || value[field] === null) continue;
|
|
if (typeof value[field] !== "string") return null;
|
|
const text = value[field].trim();
|
|
if (text.length > 256) return null;
|
|
data[field] = text;
|
|
}
|
|
|
|
if (!data.CanOpen || !data.Success) return null;
|
|
if (!data.IDNum && !data.CardNum && !data.CardIDCode) return null;
|
|
return data;
|
|
}
|
|
|
|
function normalizeString(value, maxLength) {
|
|
if (typeof value !== "string") return "";
|
|
const text = value.trim();
|
|
return text.length <= maxLength ? text : "";
|
|
}
|
|
|
|
async function saveScannedCard(item, db) {
|
|
const cardData = normalizeCardData(item.cardData);
|
|
if (!cardData) {
|
|
return { success: false, message: "卡片数据无效" };
|
|
}
|
|
|
|
try {
|
|
const encryptedCardData = Sm4Util.encryptDataForSm3(JSON.stringify(cardData));
|
|
const storeIds = Array.isArray(item.storeIds)
|
|
? [...new Set(item.storeIds.map((value) => normalizeString(value, 100)).filter(Boolean))]
|
|
: [];
|
|
const record = {
|
|
encryptedCardData,
|
|
encryption: "SM4",
|
|
dataVersion: 1,
|
|
readerType: normalizeString(item.readerType, 50) || "ZJ_HZ_310000",
|
|
corpId: normalizeString(item.corpId, 100),
|
|
storeIds,
|
|
createTime: Date.now(),
|
|
};
|
|
const result = await db.collection("scanned-card-archive").insertOne(record);
|
|
return {
|
|
success: true,
|
|
id: result.insertedId.toString(),
|
|
message: "卡片数据保存成功",
|
|
};
|
|
} catch (error) {
|
|
console.error("保存扫码建档数据失败:", error.message);
|
|
return { success: false, message: "保存卡片数据失败" };
|
|
}
|
|
}
|
|
|
|
async function getScannedCard(item, db) {
|
|
const id = normalizeString(item.id, 24);
|
|
if (!ObjectId.isValid(id)) {
|
|
return { success: false, message: "二维码无效" };
|
|
}
|
|
|
|
try {
|
|
const record = await db.collection("scanned-card-archive").findOne({
|
|
_id: new ObjectId(id),
|
|
});
|
|
if (!record) {
|
|
return { success: false, message: "未找到本次扫码信息" };
|
|
}
|
|
|
|
const storeId = normalizeString(item.storeId, 100);
|
|
const corpId = normalizeString(item.corpId, 100);
|
|
if (record.corpId && record.corpId !== corpId) {
|
|
return { success: false, message: "无权读取本次扫码信息" };
|
|
}
|
|
if (
|
|
Array.isArray(record.storeIds) &&
|
|
record.storeIds.length > 0 &&
|
|
!record.storeIds.includes(storeId)
|
|
) {
|
|
return { success: false, message: "无权读取本次扫码信息" };
|
|
}
|
|
if (!record.encryptedCardData || record.encryption !== "SM4") {
|
|
return { success: false, message: "扫码信息无效" };
|
|
}
|
|
|
|
const decrypted = Sm4Util.decryptDataForSm3(record.encryptedCardData);
|
|
const cardData = normalizeCardData(JSON.parse(decrypted));
|
|
if (!cardData) {
|
|
return { success: false, message: "扫码信息无效" };
|
|
}
|
|
return {
|
|
success: true,
|
|
data: cardData,
|
|
message: "获取扫码信息成功",
|
|
};
|
|
} catch (error) {
|
|
console.error("获取扫码建档数据失败:", error.message);
|
|
return { success: false, message: "获取扫码信息失败" };
|
|
}
|
|
}
|
|
|
|
module.exports = async (item, db) => {
|
|
switch (item.type) {
|
|
case "saveScannedCard":
|
|
return saveScannedCard(item, db);
|
|
case "getScannedCard":
|
|
return getScannedCard(item, db);
|
|
default:
|
|
return { success: false, message: "未找到接口" };
|
|
}
|
|
};
|
|
|
|
module.exports.normalizeCardData = normalizeCardData;
|
|
module.exports.saveScannedCard = saveScannedCard;
|
|
module.exports.getScannedCard = getScannedCard;
|