const app = getApp(); const tips = [ '今天宜尝试酸辣口味', '唯有美食与爱不可辜负', '厨房里的秘密,全在一勺之间', '一日三餐,每一餐都值得认真对待', '今天适合来点甜的犒劳自己', '好心情从一顿好饭开始', '美食是治愈一切的力量' ]; Page({ data: { greeting: '', locationText: '点击获取位置', dailyTip: '' }, onShow() { this.updateGreeting(); this.updateLocation(); this.setData({ dailyTip: tips[Math.floor(Math.random() * tips.length)] }); }, updateGreeting() { const h = new Date().getHours(); let g = '晚上好'; if (h < 9) g = '早上好'; else if (h < 12) g = '上午好'; else if (h < 14) g = '中午好'; else if (h < 18) g = '下午好'; this.setData({ greeting: g }); }, updateLocation() { const loc = app.globalData.location; if (loc) { this.setData({ locationText: `已定位 (${loc.latitude.toFixed(2)}, ${loc.longitude.toFixed(2)})` }); } else { // 尝试获取定位 this.requestLocation(); } }, requestLocation() { app.getLocation((err, loc) => { if (loc) { this.setData({ locationText: `已定位 (${loc.latitude.toFixed(2)}, ${loc.longitude.toFixed(2)})` }); } else { this.setData({ locationText: '点击选择位置' }); } }); }, chooseLocation() { wx.chooseLocation({ success: (res) => { app.globalData.location = { latitude: res.latitude, longitude: res.longitude }; this.setData({ locationText: `已定位 (${res.latitude.toFixed(2)}, ${res.longitude.toFixed(2)})` }); } }); }, onOpenBox(e) { // 开盒前检查定位 if (!app.globalData.location) { wx.showToast({ title: '请先获取位置', icon: 'none' }); this.requestLocation(); return; } const type = e.detail.type; const routes = { takeout: '/pages/takeout-result/takeout-result', fridge: '/pages/fridge-input/fridge-input', explore: '/pages/explore-result/explore-result' }; wx.navigateTo({ url: routes[type] || routes.takeout }); } });