38 lines
1.6 KiB
JavaScript
Raw Normal View History

2026-07-27 11:28:33 +08:00
async function addAlipayMiniAppLaunchLog(ctx, db) {
try {
const accountId = typeof ctx.accountId === 'string' ? ctx.accountId.trim() : '';
const scene = typeof ctx.scene === 'string' ? ctx.scene.trim() : '';
const storeId = typeof ctx.storeId === 'string' ? ctx.storeId.trim() : '';
const sceneValid = scene !== '' && Number.isInteger(Number(scene));//场景值是整数
if (!accountId || !scene || !storeId || !sceneValid) {
return { success: false, message: '参数错误' }
}
const record = await db.collection('mini-app-log').findOne({ accountId, platform: 'alipay' });
const queue = [{ accountId, platform: 'alipay', scene, storeId, createTime: Date.now(), type: 'launchByScanQrcode' }]
if (record) {
await db.collection('mini-app-log').insertOne(queue[0])
} else {
const firstConsult = await db.collection('consult-order')
.findOne({ accountId, drugStoreNo: { $exists: true } }, {
projection: { _id: 1, drugStoreNo: 1, createTime: 1 },
sort: { createTime: 1 }
});
if (firstConsult) {
queue.unshift({
accountId,
platform: 'alipay',
scene: firstConsult._id.toString(),
storeId: firstConsult.drugStoreNo,
createTime: firstConsult.createTime,
type: 'supplementaryConsult'
})
}
await db.collection('mini-app-log').insertMany(queue)
}
return { success: true, message: '添加成功' }
} catch (e) {
return { success: false, message: e.message }
}
}
module.exports = { addAlipayMiniAppLaunchLog }