const { secure, toast } = require("../../utils/api"); Page({ data: { meetups: [], pets: [], selected: [], active: null, userId: "" }, onShow() { this.load(); }, async load() { try { const [meetups, pets, me] = await Promise.all([ secure("/meetups"), secure("/pets"), secure("/me"), ]); this.setData({ meetups: meetups.map((m) => ({ ...m, date: new Date(m.starts_at).toLocaleString("zh-CN"), })), pets, userId: me.id, }); } catch (e) { toast(e); } }, create() { wx.navigateTo({ url: "/pages/meetup-edit/meetup-edit" }); }, open(e) { this.setData({ active: this.data.meetups[e.currentTarget.dataset.index], selected: [], }); }, close() { this.setData({ active: null }); }, pick(e) { this.setData({ selected: e.detail.selected }); }, async join() { if (!this.data.selected.length) { wx.showToast({ title: "请选择同行的狗狗", icon: "none" }); return; } try { await secure("/meetups/" + this.data.active.id + "/join", "POST", { pet_ids: this.data.selected, }); wx.showToast({ title: "报名成功", icon: "success" }); this.close(); this.load(); } catch (e) { toast(e); } }, async cancel() { const r = await wx.showModal({ title: "取消约遛", content: "确定取消这场约遛吗?", }); if (!r.confirm) return; try { await secure("/meetups/" + this.data.active.id + "/cancel", "POST"); this.close(); this.load(); } catch (e) { toast(e); } }, });