const app = getApp(); function getLocation() { return new Promise((resolve, reject) => { const cached = app.globalData.location; if (cached) { resolve(cached); return; } wx.getLocation({ type: 'gcj02', success: (res) => { const loc = { latitude: res.latitude, longitude: res.longitude }; app.globalData.location = loc; resolve(loc); }, fail: (err) => { // 常见错误:未授权、隐私协议未同意 const msg = err.errMsg || ''; if (msg.indexOf('auth deny') >= 0 || msg.indexOf('authorize') >= 0) { wx.showModal({ title: '需要定位权限', content: '请在设置中允许小程序获取位置,或点击右上角手动选择位置', confirmText: '去设置', success: (res) => { if (res.confirm) wx.openSetting(); } }); } reject(new Error('定位失败')); } }); }); } module.exports = { getLocation };