获取位置失败版本
This commit is contained in:
parent
93348e9d89
commit
186103e8cf
|
|
@ -37,7 +37,9 @@
|
||||||
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
|
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
|
||||||
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
|
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
|
||||||
"<uses-feature android:name=\"android.hardware.camera\"/>",
|
"<uses-feature android:name=\"android.hardware.camera\"/>",
|
||||||
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
|
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>",
|
||||||
|
"<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"/>",
|
||||||
|
"<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
/* ios打包配置 */
|
/* ios打包配置 */
|
||||||
|
|
@ -54,16 +56,28 @@
|
||||||
"setting": {
|
"setting": {
|
||||||
"urlCheck": false
|
"urlCheck": false
|
||||||
},
|
},
|
||||||
"usingComponents": true
|
"usingComponents": true,
|
||||||
|
"requiredPrivateInfos": [
|
||||||
|
"getLocation"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"mp-alipay": {
|
"mp-alipay": {
|
||||||
"usingComponents": true
|
"usingComponents": true,
|
||||||
|
"requiredPrivateInfos": [
|
||||||
|
"getLocation"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"mp-baidu": {
|
"mp-baidu": {
|
||||||
"usingComponents": true
|
"usingComponents": true,
|
||||||
|
"requiredPrivateInfos": [
|
||||||
|
"getLocation"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"mp-toutiao": {
|
"mp-toutiao": {
|
||||||
"usingComponents": true
|
"usingComponents": true,
|
||||||
|
"requiredPrivateInfos": [
|
||||||
|
"getLocation"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"uniStatistics": {
|
"uniStatistics": {
|
||||||
"enable": false
|
"enable": false
|
||||||
|
|
|
||||||
|
|
@ -59,5 +59,10 @@
|
||||||
"navigationBarBackgroundColor": "#F8F8F8",
|
"navigationBarBackgroundColor": "#F8F8F8",
|
||||||
"backgroundColor": "#F8F8F8"
|
"backgroundColor": "#F8F8F8"
|
||||||
},
|
},
|
||||||
"uniIdRouter": {}
|
"uniIdRouter": {},
|
||||||
|
"mp-weixin": {
|
||||||
|
"requiredPrivateInfos": [
|
||||||
|
"getLocation"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,8 @@
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<!-- 当前定位 -->
|
<!-- 当前定位 -->
|
||||||
<view v-if="currentLocation" class="location-suggestion">
|
<view class="location-suggestion">
|
||||||
<view class="location-card">
|
<view v-if="currentLocation" class="location-card">
|
||||||
<view class="location-content">
|
<view class="location-content">
|
||||||
<view class="location-header">
|
<view class="location-header">
|
||||||
<text class="location-title">当前定位</text>
|
<text class="location-title">当前定位</text>
|
||||||
|
|
@ -66,6 +66,16 @@
|
||||||
</view>
|
</view>
|
||||||
<button class="use-location-btn" @click="useCurrentLocation">使用</button>
|
<button class="use-location-btn" @click="useCurrentLocation">使用</button>
|
||||||
</view>
|
</view>
|
||||||
|
<view v-else class="location-card">
|
||||||
|
<view class="location-content">
|
||||||
|
<view class="location-header">
|
||||||
|
<text class="location-title">获取位置</text>
|
||||||
|
<text class="location-company">点击获取当前位置</text>
|
||||||
|
</view>
|
||||||
|
<text class="location-address">需要定位权限</text>
|
||||||
|
</view>
|
||||||
|
<button class="get-location-btn" @click="getCurrentLocation">获取</button>
|
||||||
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<!-- 详细位置 -->
|
<!-- 详细位置 -->
|
||||||
|
|
@ -122,6 +132,7 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import commonEnum from '../../enum/commonEnum'
|
import commonEnum from '../../enum/commonEnum'
|
||||||
|
import { getLocationWithPermission, handleLocationError } from '@/utils/permission.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'LeasePage',
|
name: 'LeasePage',
|
||||||
|
|
@ -130,6 +141,10 @@ export default {
|
||||||
return commonEnum
|
return commonEnum
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
onLoad() {
|
||||||
|
// 页面加载时获取当前位置
|
||||||
|
this.getCurrentLocation()
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
formData: {
|
formData: {
|
||||||
|
|
@ -140,15 +155,70 @@ export default {
|
||||||
equipment: '',
|
equipment: '',
|
||||||
period: '1年',
|
period: '1年',
|
||||||
},
|
},
|
||||||
currentLocation: {
|
currentLocation: null,
|
||||||
company: '福鼎创特物联科技有限公司',
|
|
||||||
address: '福建宁德市福鼎市',
|
|
||||||
},
|
|
||||||
showDetails: false,
|
showDetails: false,
|
||||||
totalAmount: '100.10',
|
totalAmount: '100.10',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
// 获取当前位置信息
|
||||||
|
async getCurrentLocation() {
|
||||||
|
try {
|
||||||
|
uni.showLoading({
|
||||||
|
title: '获取位置中...'
|
||||||
|
})
|
||||||
|
|
||||||
|
const location = await getLocationWithPermission()
|
||||||
|
console.log('位置信息:', location)
|
||||||
|
this.reverseGeocode(location.latitude, location.longitude)
|
||||||
|
} catch (err) {
|
||||||
|
uni.hideLoading()
|
||||||
|
handleLocationError(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 处理位置信息
|
||||||
|
reverseGeocode(latitude, longitude) {
|
||||||
|
uni.hideLoading()
|
||||||
|
|
||||||
|
// 直接使用坐标信息,避免依赖外部API
|
||||||
|
this.currentLocation = {
|
||||||
|
company: '当前位置',
|
||||||
|
address: `纬度: ${latitude.toFixed(6)}, 经度: ${longitude.toFixed(6)}`,
|
||||||
|
latitude: latitude,
|
||||||
|
longitude: longitude
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用uni-app的地理编码(如果平台支持)
|
||||||
|
// #ifdef APP-PLUS || MP-WEIXIN
|
||||||
|
uni.reverseGeocoder({
|
||||||
|
location: {
|
||||||
|
latitude: latitude,
|
||||||
|
longitude: longitude
|
||||||
|
},
|
||||||
|
success: (res) => {
|
||||||
|
console.log('逆地理编码结果:', res)
|
||||||
|
if (res.result) {
|
||||||
|
this.currentLocation = {
|
||||||
|
company: res.result.addressComponent?.city || '当前位置',
|
||||||
|
address: res.result.formatted_addresses?.recommend || res.result.address || '未知地址',
|
||||||
|
latitude: latitude,
|
||||||
|
longitude: longitude
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: (err) => {
|
||||||
|
console.log('逆地理编码不可用,使用坐标信息')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
uni.showToast({
|
||||||
|
title: '位置获取成功',
|
||||||
|
icon: 'success'
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
selectAddress() {
|
selectAddress() {
|
||||||
// 选择地址逻辑
|
// 选择地址逻辑
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
|
|
@ -156,7 +226,13 @@ export default {
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
useCurrentLocation() {
|
useCurrentLocation() {
|
||||||
|
if (!this.currentLocation) {
|
||||||
|
this.getCurrentLocation()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.formData.address = this.currentLocation.company + ' ' + this.currentLocation.address
|
this.formData.address = this.currentLocation.company + ' ' + this.currentLocation.address
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '已使用当前定位',
|
title: '已使用当前定位',
|
||||||
|
|
@ -462,6 +538,23 @@ export default {
|
||||||
box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.4);
|
box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.get-location-btn {
|
||||||
|
background: #ff6b6b;
|
||||||
|
color: #ffffff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 24rpx;
|
||||||
|
padding: 12rpx 24rpx;
|
||||||
|
font-size: 24rpx;
|
||||||
|
font-weight: 500;
|
||||||
|
box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.3);
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
|
||||||
|
&:active {
|
||||||
|
transform: translateY(2rpx);
|
||||||
|
box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.4);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
126
utils/permission.js
Normal file
126
utils/permission.js
Normal file
|
|
@ -0,0 +1,126 @@
|
||||||
|
/**
|
||||||
|
* 权限处理工具
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查位置权限
|
||||||
|
* @returns {Promise<boolean>} 是否有权限
|
||||||
|
*/
|
||||||
|
export function checkLocationPermission() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.getSetting({
|
||||||
|
success: (res) => {
|
||||||
|
const hasPermission = res.authSetting['scope.userLocation'] !== false
|
||||||
|
resolve(hasPermission)
|
||||||
|
},
|
||||||
|
fail: () => {
|
||||||
|
resolve(true) // 默认允许
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求位置权限
|
||||||
|
* @returns {Promise<boolean>} 是否获得权限
|
||||||
|
*/
|
||||||
|
export function requestLocationPermission() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
uni.showModal({
|
||||||
|
title: '需要位置权限',
|
||||||
|
content: '为了提供更好的服务,需要获取您的位置信息。请在设置中开启位置权限。',
|
||||||
|
confirmText: '去设置',
|
||||||
|
cancelText: '取消',
|
||||||
|
success: (modalRes) => {
|
||||||
|
if (modalRes.confirm) {
|
||||||
|
uni.openSetting({
|
||||||
|
success: (settingRes) => {
|
||||||
|
const hasPermission = settingRes.authSetting['scope.userLocation']
|
||||||
|
resolve(hasPermission)
|
||||||
|
},
|
||||||
|
fail: () => {
|
||||||
|
resolve(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
resolve(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取位置信息(包含权限检查)
|
||||||
|
* @returns {Promise<Object>} 位置信息
|
||||||
|
*/
|
||||||
|
export function getLocationWithPermission() {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
// 检查权限
|
||||||
|
const hasPermission = await checkLocationPermission()
|
||||||
|
|
||||||
|
if (!hasPermission) {
|
||||||
|
// 请求权限
|
||||||
|
const granted = await requestLocationPermission()
|
||||||
|
if (!granted) {
|
||||||
|
reject(new Error('位置权限被拒绝'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取位置
|
||||||
|
uni.getLocation({
|
||||||
|
type: 'gcj02',
|
||||||
|
success: (res) => {
|
||||||
|
resolve(res)
|
||||||
|
},
|
||||||
|
fail: (err) => {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理位置获取错误
|
||||||
|
* @param {Object} err 错误对象
|
||||||
|
*/
|
||||||
|
export function handleLocationError(err) {
|
||||||
|
console.error('获取位置失败:', err)
|
||||||
|
|
||||||
|
let errorMsg = '获取位置失败'
|
||||||
|
let showModal = false
|
||||||
|
|
||||||
|
if (err.errMsg.includes('auth deny') || err.errMsg.includes('no permission')) {
|
||||||
|
errorMsg = '定位权限被拒绝,请在设置中开启'
|
||||||
|
showModal = true
|
||||||
|
} else if (err.errMsg.includes('timeout')) {
|
||||||
|
errorMsg = '定位超时,请重试'
|
||||||
|
} else if (err.errMsg.includes('unsupported')) {
|
||||||
|
errorMsg = '当前设备不支持定位功能'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showModal) {
|
||||||
|
uni.showModal({
|
||||||
|
title: '权限被拒绝',
|
||||||
|
content: '请在微信设置中开启位置权限,或点击确定前往设置页面。',
|
||||||
|
confirmText: '去设置',
|
||||||
|
cancelText: '取消',
|
||||||
|
success: (modalRes) => {
|
||||||
|
if (modalRes.confirm) {
|
||||||
|
uni.openSetting()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
uni.showToast({
|
||||||
|
title: errorMsg,
|
||||||
|
icon: 'none',
|
||||||
|
duration: 2000
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user