Files
sundynix-pets/pets-fe/pages/walk-records/walk-records.js
T
Blizzard 8a3036f0db
ci-cd / verify (push) Successful in 4s
ci-cd / deploy (push) Skipped
Redesign mini program navigation and add back controls
2026-10-04 09:48:12 +08:00

45 lines
1.5 KiB
JavaScript

const { secure, toast } = require("../../utils/api");
function duration(seconds) {
const minutes = Math.floor((seconds || 0) / 60);
return `${minutes} 分 ${String((seconds || 0) % 60).padStart(2, "0")} 秒`;
}
Page({
data: { records: [], selected: null, loading: true },
onLoad() {
this.load();
},
async load() {
try {
const [walks, pets] = await Promise.all([secure("/walks"), secure("/pets")]);
const names = Object.fromEntries(pets.map((pet) => [pet.id, pet.name]));
this.setData({
records: walks.filter((walk) => walk.status === "finished").map((walk) => ({
...walk,
date: new Date(walk.started_at).toLocaleString("zh-CN"),
duration: duration(walk.duration_seconds),
distance: ((walk.distance_meters || 0) / 1000).toFixed(2),
petNames: (walk.pet_ids || []).map((id) => names[id] || "已归档宠物").join("、"),
polyline: walk.points && walk.points.length > 1 ? [{
points: walk.points.map((point) => ({ latitude: point.latitude, longitude: point.longitude })),
color: "#268e6a",
width: 5,
}] : [],
lastPoint: walk.points && walk.points.length ? walk.points[walk.points.length - 1] : null,
})),
loading: false,
});
} catch (error) {
this.setData({ loading: false });
toast(error);
}
},
open(e) {
this.setData({ selected: this.data.records[e.currentTarget.dataset.index] });
},
close() {
this.setData({ selected: null });
},
});