Complete core mini program flows
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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 });
|
||||
},
|
||||
back() {
|
||||
wx.navigateBack();
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user