const { secure, toast } = require("../../utils/api"); Page({ data: { pets: [], selected: [], title: "", description: "", place_name: "", latitude: 0, longitude: 0, date: "", time: "", max_people: 8, saving: false, }, onLoad() { this.load(); }, async load() { try { this.setData({ pets: await secure("/pets") }); } catch (e) { toast(e); } }, field(e) { this.setData({ [e.currentTarget.dataset.key]: e.detail.value }); }, pick(e) { this.setData({ selected: e.detail.selected }); }, date(e) { this.setData({ date: e.detail.value }); }, time(e) { this.setData({ time: e.detail.value }); }, async location() { try { const r = await wx.chooseLocation(); this.setData({ place_name: r.name || r.address, latitude: r.latitude, longitude: r.longitude, }); } catch (e) { toast(e); } }, async save() { if (this.data.saving) return; const d = this.data; if ( !d.title.trim() || !d.place_name || !d.date || !d.time || !d.selected.length ) { wx.showToast({ title: "请填完时间地点并选择狗狗", icon: "none" }); return; } const startsAt = new Date(d.date + "T" + d.time + ":00+08:00"); if (startsAt <= new Date()) { wx.showToast({ title: "请选择未来的时间", icon: "none" }); return; } if (Number(d.max_people) < 2 || Number(d.max_people) > 100) { wx.showToast({ title: "人数应为 2 到 100", icon: "none" }); return; } this.setData({ saving: true }); try { await secure("/meetups", "POST", { title: d.title, description: d.description, place_name: d.place_name, latitude: d.latitude, longitude: d.longitude, starts_at: startsAt.toISOString(), max_people: Number(d.max_people), pet_ids: d.selected, }); wx.navigateBack(); } catch (e) { toast(e); } finally { this.setData({ saving: false }); } }, });