Browse Source

添加位置授权功能到我的页面

pull/1/head
徐飞洋 3 months ago
parent
commit
d9ce767351
  1. 7
      app.json
  2. 116
      pages/profile/index.js
  3. 17
      pages/profile/index.wxml

7
app.json

@ -84,5 +84,10 @@
] ]
}, },
"style": "v2", "style": "v2",
"sitemapLocation": "sitemap.json" "sitemapLocation": "sitemap.json",
"permission": {
"scope.userLocation": {
"desc": "您的位置信息将用于提供本地化服务"
}
}
} }

116
pages/profile/index.js

@ -4,15 +4,19 @@ Page({
userInfo: {}, userInfo: {},
userType: '', userType: '',
userTags: [], userTags: [],
needPhoneAuth: false // 是否需要重新授权手机号 needPhoneAuth: false, // 是否需要重新授权手机号
locationInfo: '', // 位置信息
hasLocationAuth: false // 是否已经授权位置
}, },
onLoad() { onLoad() {
this.loadUserInfo() this.loadUserInfo();
this.checkLocationAuth();
}, },
onShow() { onShow() {
this.loadUserInfo() this.loadUserInfo();
this.checkLocationAuth();
// 更新自定义tabBar状态 // 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) { if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({ 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) { updateName(newName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

17
pages/profile/index.wxml

@ -37,6 +37,23 @@
</view> </view>
</view> </view>
<!-- 位置授权 -->
<view class="card">
<view class="title">位置信息</view>
<view style="padding: 20rpx 0;">
<view wx:if="{{hasLocationAuth}}" style="font-size: 28rpx; color: #333; margin-bottom: 20rpx;">
当前位置:{{locationInfo || '获取中...'}}
</view>
<button
class="btn"
style="background-color: {{hasLocationAuth ? '#888' : '#1677ff'}}; color: white;"
bindtap="{{hasLocationAuth ? 'getUserLocation' : 'requestLocationAuth'}}"
>
{{hasLocationAuth ? '更新位置' : '授权位置'}}
</button>
</view>
</view>
<view class="card" wx:if="{{false}}"> <view class="card" wx:if="{{false}}">
<view class="title">身份管理</view> <view class="title">身份管理</view>
<button <button

Loading…
Cancel
Save