Files
sundynix-pets/pets-fe/pages/home/home.js
T
Blizzard f0ee63e642
ci-cd / verify (push) Successful in 6s
ci-cd / deploy (push) Skipped
Cache home location and weather across page navigation
2026-10-05 11:08:11 +08:00

155 lines
4.3 KiB
JavaScript

const { secure, toast } = require("../../utils/api");
const { localDate } = require("../../utils/date");
const environment = require("../../utils/environment");
function dateLabel() {
const now = new Date();
return `${now.getMonth() + 1}月${now.getDate()}日 周${"日一二三四五六"[now.getDay()]}`;
}
Page({
data: {
locationName: "当前位置",
headerSubtitle: dateLabel() + " · 轻触获取天气",
pets: [],
selected: [],
tasks: [],
walkCount: 0,
name: "铲屎官",
loading: true,
ready: false,
phase: "auth",
error: "",
},
onShow() {
this.applyEnvironment(environment.cached());
this.load();
},
onUnload() {
this.disposed = true;
},
updateEnvironmentLabel() {
this.setData({
headerSubtitle:
dateLabel() + " · " + (this.weatherLabel || "轻触获取天气"),
});
},
applyEnvironment(value) {
if (this.disposed) return;
if (value) {
this.weatherLabel = value.weatherLabel;
this.setData({ locationName: value.locationName });
}
this.updateEnvironmentLabel();
},
async refreshEnvironment(event) {
if (this.environmentBusy) return;
this.environmentBusy = true;
const tapped = !!event;
const cached = environment.cached();
if (!cached) {
this.weatherLabel = "正在获取天气";
this.updateEnvironmentLabel();
}
try {
this.applyEnvironment(await environment.refresh(tapped));
} catch (error) {
if (this.disposed) return;
if (cached) {
this.applyEnvironment(cached);
} else {
const denied = /denied|deny|auth/.test(
error.errMsg || error.message || "",
);
this.setData({ locationName: denied ? "开启定位" : "定位暂不可用" });
this.weatherLabel = denied ? "轻触开启定位" : "轻触重试";
this.updateEnvironmentLabel();
}
if (tapped) toast(error);
} finally {
this.environmentBusy = false;
}
},
async load() {
if (this.loadPromise) return this.loadPromise;
this.loadPromise = this.loadHome().finally(() => {
this.loadPromise = null;
});
return this.loadPromise;
},
async loadHome() {
this.setData({
loading: true,
error: "",
phase: this.data.ready ? "data" : "auth",
});
let authenticated = false;
try {
await getApp().login();
authenticated = true;
if (this.disposed) return;
this.setData({ phase: "data" });
const [pets, tasks, walks, me] = await Promise.all([
secure("/pets"),
secure("/tasks"),
secure("/walks"),
secure("/me"),
]);
if (this.disposed) return;
const chosen = wx.getStorageSync("walk_pet_ids") || [],
month = localDate().slice(0, 7);
this.setData({
pets,
selected: chosen.filter((id) => pets.some((p) => p.id === id)),
tasks: tasks.items,
walkCount: walks.filter((w) => w.started_at.slice(0, 7) === month)
.length,
name: me.nickname || "毛孩家长",
loading: false,
ready: true,
phase: "ready",
});
// Location permission and weather never hold up the first usable screen.
this.refreshEnvironment();
} catch (error) {
if (this.disposed) return;
this.setData({
loading: false,
phase: !authenticated && !this.data.ready ? "auth-error" : "data-error",
error: error.message || "暂时没能连接,请稍后重试",
});
}
},
addPet() {
wx.navigateTo({ url: "/pages/pet-edit/pet-edit" });
},
pick(e) {
this.setData({ selected: e.detail.selected });
wx.setStorageSync("walk_pet_ids", e.detail.selected);
},
start() {
if (!this.data.selected.length) {
wx.showToast({ title: "先选择一起出门的狗狗", icon: "none" });
return;
}
wx.redirectTo({ url: "/pages/walk/walk" });
},
goTasks() {
wx.redirectTo({ url: "/pages/tasks/tasks" });
},
goWalks() {
wx.navigateTo({ url: "/pages/walk-records/walk-records" });
},
async complete(e) {
const id = e.currentTarget.dataset.id,
task = this.data.tasks.find((t) => t.id === id);
try {
await secure("/tasks/" + id + "/completion", "PUT", {
date: localDate(),
completed: !task.completed,
});
await this.load();
} catch (err) {
toast(err);
}
},
});