mirror of
https://gitee.com/beijing_hongye_huicheng/lilishop-uniapp.git
synced 2026-08-06 10:57:25 +08:00
- 将多个组件转换为 `<script setup>` 语法,提升可读性和性能 - 优化状态管理和事件处理逻辑,简化代码结构 - 更新样式和布局以适应新组件结构 - 添加新功能和修复已知问题,提升用户体验
100 lines
1.8 KiB
Vue
100 lines
1.8 KiB
Vue
<template></template>
|
|
|
|
<script setup lang="ts">
|
|
import { onMounted } from 'vue'
|
|
import { getAddressCode } from '@/api/address'
|
|
import config from '@/config/config'
|
|
|
|
const emit = defineEmits<{
|
|
callback: [payload: Record<string, any>]
|
|
close: []
|
|
}>()
|
|
|
|
onMounted(() => {
|
|
init()
|
|
})
|
|
|
|
function initMap() {
|
|
uni.chooseLocation({
|
|
success(res) {
|
|
posToCity(res.latitude, res.longitude).then((val) => {
|
|
getAddressCode(
|
|
val.regeocode.addressComponent.citycode,
|
|
val.regeocode.addressComponent.township,
|
|
).then((code) => {
|
|
emit('callback', { ...val, ...res, ...code })
|
|
emit('close')
|
|
})
|
|
})
|
|
},
|
|
fail(e) {
|
|
console.log(e)
|
|
emit('close')
|
|
},
|
|
})
|
|
}
|
|
|
|
function init() {
|
|
// #ifdef MP-WEIXIN
|
|
wechatMap()
|
|
// #endif
|
|
// #ifndef MP-WEIXIN
|
|
initMap()
|
|
// #endif
|
|
}
|
|
|
|
function wechatMap() {
|
|
uni.authorize({
|
|
scope: 'scope.userLocation',
|
|
success() {
|
|
initMap()
|
|
},
|
|
fail() {
|
|
uni.showModal({
|
|
content: '检测到您没打开获取地址功能权限,是否去设置打开?',
|
|
confirmText: '确认',
|
|
cancelText: '取消',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
uni.openSetting({
|
|
success: () => {
|
|
initMap()
|
|
},
|
|
})
|
|
} else {
|
|
emit('close')
|
|
}
|
|
},
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
function posToCity(latitude: number, longitude: number) {
|
|
return new Promise<any>((resolve, reject) => {
|
|
uni.request({
|
|
url: 'https://restapi.amap.com/v3/geocode/regeo',
|
|
method: 'GET',
|
|
data: {
|
|
key: config.aMapKey,
|
|
location: `${longitude},${latitude}`,
|
|
},
|
|
success: ({ data }: any) => {
|
|
const { status, info } = data
|
|
if (status === '1') {
|
|
resolve(data)
|
|
} else {
|
|
reject(info)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err)
|
|
},
|
|
})
|
|
})
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
</style>
|