const app = getApp(); const api = require('../../utils/api'); const loc = require('../../utils/location'); const storage = require('../../utils/storage'); Page({ data: { showAnimation: false, dataReady: false, result: null, error: '', distanceText: '', deliveryInfo: '' }, onLoad() { this.roll(); }, roll() { this.setData({ showAnimation: true, dataReady: false, error: '', result: null }); const prefs = storage.get('user_prefs', {}); loc.getLocation().then((pos) => { return api.post('/api/takeout/roll', { latitude: pos.latitude, longitude: pos.longitude, openid: 'anonymous', taste: prefs.taste || '都可以', priceRange: prefs.priceRange || 'all', allergies: prefs.allergies || '' }); }).then((data) => { const distKm = ((data.distance || 0) / 1000).toFixed(1); const deliveryTime = data.deliveryTime || ''; const minOrder = data.minOrder || ''; const deliveryFee = data.deliveryFee ? ('配送费' + data.deliveryFee) : ''; const parts = [deliveryTime, minOrder, deliveryFee].filter(Boolean); this.setData({ result: data, distanceText: distKm + 'km', deliveryInfo: parts.join(' · '), dataReady: true }); }).catch((err) => { this.setData({ showAnimation: false, error: err.message || '附近喵星人占领了,换片区域试试?' }); }); }, onAnimationDone() { this.setData({ showAnimation: false }); }, goOrder() { const shop = this.data.result; if (!shop) return; const shopName = shop.name || ''; const shopAddress = shop.address || ''; const lat = app.globalData.location ? app.globalData.location.latitude : 0; const lng = app.globalData.location ? app.globalData.location.longitude : 0; wx.showActionSheet({ itemList: ['美团外卖', '饿了么', '查看地图位置'], success: (res) => { if (res.tapIndex === 0) { this.openMeituan(shopName); } else if (res.tapIndex === 1) { this.openEleme(shopName); } else if (res.tapIndex === 2) { wx.openLocation({ latitude: shop.latitude || lat, longitude: shop.longitude || lng, name: shopName, address: shopAddress, scale: 16 }); } }, fail: () => { this.openMeituan(shopName); } }); }, openMeituan(name) { wx.navigateToMiniProgram({ appId: 'wxde8ac0a21135c07d', path: '', extraData: { query: name }, success: () => { this.saveRecord(this.data.result); }, fail: () => { wx.showToast({ title: '请安装美团外卖 APP', icon: 'none', duration: 2000 }); } }); }, openEleme(name) { wx.navigateToMiniProgram({ appId: 'wxece3a9a4c82f58c9', path: '', extraData: { query: name }, success: () => { this.saveRecord(this.data.result); }, fail: () => { wx.showToast({ title: '请安装饿了么 APP', icon: 'none', duration: 2000 }); } }); }, retry() { this.roll(); }, saveRecord(data) { const history = wx.getStorageSync('box_history') || []; history.push({ id: Date.now().toString(), icon: '🛵', name: data.name, time: new Date().toLocaleString(), typeName: '外卖盲盒' }); wx.setStorageSync('box_history', history); } });