0.0.0
This commit is contained in:
35
cloudfunctions/getHistory/index.js
Normal file
35
cloudfunctions/getHistory/index.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// 云函数 getHistory — 遛狗历史列表(跨全部狗狗,附狗名)
|
||||
// 供「遛狗历史」页与「记录」Tab 使用。
|
||||
const cloud = require('wx-server-sdk');
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
|
||||
const db = cloud.database();
|
||||
|
||||
exports.main = async (event) => {
|
||||
const { OPENID } = cloud.getWXContext();
|
||||
const limit = Math.min(event.limit || 50, 100);
|
||||
|
||||
// 狗名映射
|
||||
const dogsRes = await db.collection('dogs').where({ _openid: OPENID }).get();
|
||||
const nameMap = {};
|
||||
dogsRes.data.forEach((d) => { nameMap[d._id] = d.name; });
|
||||
|
||||
const res = await db.collection('walks')
|
||||
.where({ _openid: OPENID })
|
||||
.orderBy('startAt', 'desc')
|
||||
.limit(limit)
|
||||
.get();
|
||||
|
||||
const walks = res.data.map((w) => ({
|
||||
_id: w._id,
|
||||
dogIds: w.dogIds,
|
||||
dogNames: (w.dogIds || []).map((id) => nameMap[id] || '狗狗'),
|
||||
startAt: w.startAt,
|
||||
duration: w.duration,
|
||||
distance: w.distance,
|
||||
dogSteps: w.dogSteps,
|
||||
isManual: w.isManual,
|
||||
walkDate: w.walkDate,
|
||||
}));
|
||||
|
||||
return { ok: true, data: { walks, total: walks.length } };
|
||||
};
|
||||
Reference in New Issue
Block a user