|
|
|
@ -4,15 +4,19 @@ Page({ |
|
|
|
userInfo: {}, |
|
|
|
userType: '', |
|
|
|
userTags: [], |
|
|
|
needPhoneAuth: false // 是否需要重新授权手机号
|
|
|
|
needPhoneAuth: false, // 是否需要重新授权手机号
|
|
|
|
locationInfo: '', // 位置信息
|
|
|
|
hasLocationAuth: false // 是否已经授权位置
|
|
|
|
}, |
|
|
|
|
|
|
|
onLoad() { |
|
|
|
this.loadUserInfo() |
|
|
|
this.loadUserInfo(); |
|
|
|
this.checkLocationAuth(); |
|
|
|
}, |
|
|
|
|
|
|
|
onShow() { |
|
|
|
this.loadUserInfo() |
|
|
|
this.loadUserInfo(); |
|
|
|
this.checkLocationAuth(); |
|
|
|
// 更新自定义tabBar状态
|
|
|
|
if (typeof this.getTabBar === 'function' && this.getTabBar()) { |
|
|
|
this.getTabBar().setData({ |
|
|
|
@ -777,6 +781,112 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 检查位置授权状态
|
|
|
|
checkLocationAuth() { |
|
|
|
const that = this; |
|
|
|
wx.getSetting({ |
|
|
|
success(res) { |
|
|
|
if (res.authSetting['scope.userLocation']) { |
|
|
|
// 用户已经授权位置
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
that.getUserLocation(); |
|
|
|
} else { |
|
|
|
// 用户未授权位置
|
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 请求位置授权
|
|
|
|
requestLocationAuth() { |
|
|
|
const that = this; |
|
|
|
wx.authorize({ |
|
|
|
scope: 'scope.userLocation', |
|
|
|
success() { |
|
|
|
// 授权成功
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
that.getUserLocation(); |
|
|
|
}, |
|
|
|
fail() { |
|
|
|
// 授权失败
|
|
|
|
wx.showToast({ |
|
|
|
title: '您已拒绝位置授权', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
// 引导用户打开设置页面
|
|
|
|
wx.showModal({ |
|
|
|
title: '位置授权', |
|
|
|
content: '需要您的位置信息,请前往设置开启', |
|
|
|
success(res) { |
|
|
|
if (res.confirm) { |
|
|
|
wx.openSetting({ |
|
|
|
success(settingRes) { |
|
|
|
if (settingRes.authSetting['scope.userLocation']) { |
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
that.getUserLocation(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 获取用户当前位置
|
|
|
|
getUserLocation() { |
|
|
|
const that = this; |
|
|
|
wx.showLoading({ title: '获取位置中...' }); |
|
|
|
wx.getLocation({ |
|
|
|
type: 'gcj02', |
|
|
|
success(res) { |
|
|
|
const latitude = res.latitude; |
|
|
|
const longitude = res.longitude; |
|
|
|
// 调用逆地理编码API获取详细地址
|
|
|
|
that.reverseGeocode(latitude, longitude); |
|
|
|
}, |
|
|
|
fail() { |
|
|
|
wx.hideLoading(); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取位置失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 逆地理编码获取详细地址
|
|
|
|
reverseGeocode(latitude, longitude) { |
|
|
|
const that = this; |
|
|
|
wx.request({ |
|
|
|
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`, |
|
|
|
success(res) { |
|
|
|
wx.hideLoading(); |
|
|
|
if (res.data.status === 0) { |
|
|
|
const address = res.data.result.address; |
|
|
|
that.setData({ locationInfo: address }); |
|
|
|
// 可以将位置信息保存到本地存储
|
|
|
|
wx.setStorageSync('locationInfo', address); |
|
|
|
} else { |
|
|
|
wx.showToast({ |
|
|
|
title: '获取地址失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail() { |
|
|
|
wx.hideLoading(); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取地址失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 更新用户名称
|
|
|
|
updateName(newName) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|