Files
ChowBox/miniapp/pages/explore-result/explore-result.js

80 lines
1.7 KiB
JavaScript
Raw Normal View History

const app = getApp();
const api = require('../../utils/api');
const loc = require('../../utils/location');
Page({
data: {
showAnimation: false,
dataReady: false,
result: null,
error: '',
distanceText: ''
},
onLoad() {
this.roll();
},
roll() {
this.setData({
showAnimation: true,
dataReady: false,
error: '',
result: null
});
loc.getLocation().then((pos) => {
return api.post('/api/explore/roll', {
latitude: pos.latitude,
longitude: pos.longitude,
openid: 'anonymous'
});
}).then((data) => {
this.setData({
result: data,
distanceText: ((data.distance || 0) / 1000).toFixed(1) + 'km',
dataReady: true
});
this.saveRecord(data);
}).catch((err) => {
this.setData({
showAnimation: false,
error: err.message || '附近暂无推荐好店,换片区域试试?'
});
});
},
onAnimationDone() {
this.setData({ showAnimation: false });
},
navigate() {
const shop = this.data.result;
if (!shop) return;
wx.openLocation({
latitude: shop.latitude || app.globalData.location.latitude,
longitude: shop.longitude || app.globalData.location.longitude,
name: shop.name,
address: shop.address,
scale: 16
});
},
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);
}
});