fix:修复bug
This commit is contained in:
@@ -4,8 +4,6 @@ import {
|
||||
getCurrentInstance
|
||||
} from 'vue';
|
||||
import Search from "@/pages/home/components/tabbar-home/components/search.vue";
|
||||
import useEventEmit from "@/hooks/useEventEmit";
|
||||
import {EventEnum} from "@/constant/enums";
|
||||
const { t } = useI18n();
|
||||
import {useAddressStore} from "@/pages/address/store/address";
|
||||
import {appUserAddressListPost} from "@/service";
|
||||
@@ -15,6 +13,8 @@ const addressStore = useAddressStore()
|
||||
|
||||
const addressesList = ref([])
|
||||
const currentAddressId = ref('')
|
||||
// 从 checkout 等页面通过 URL 传入的地址 id,用于进入页面时保持正确选中项
|
||||
const initialAddressIdFromQuery = ref('')
|
||||
function getAddressList() {
|
||||
appUserAddressListPost({
|
||||
params: {
|
||||
@@ -25,37 +25,61 @@ function getAddressList() {
|
||||
console.log('获取用户地址列表', res)
|
||||
addressesList.value = res.rows
|
||||
if(res.rows.length > 0) {
|
||||
currentAddressId.value = res.rows[0].id
|
||||
// 若有从上一页传入的 id 且该 id 在列表中,则保持选中该地址;否则选中第一个
|
||||
const fromQuery = initialAddressIdFromQuery.value
|
||||
const existsInList = res.rows.some((row: any) => String(row.id) === String(fromQuery))
|
||||
if (fromQuery && existsInList) {
|
||||
currentAddressId.value = fromQuery
|
||||
} else {
|
||||
currentAddressId.value = res.rows[0].id
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
function handleClickSearch() {
|
||||
uni.navigateTo({
|
||||
url: '/pages-user/pages/search-address/index',
|
||||
events: {
|
||||
chooseAddress: (data: any) => {
|
||||
console.log('搜索的地址信息', data)
|
||||
if (!data) return
|
||||
|
||||
// 先判断是否已有相同地址
|
||||
const exist = addressesList.value.find((item: any) => {
|
||||
return (
|
||||
item.formattedAddress === data.formattedAddress &&
|
||||
(item.displayName || '') === (data.displayName || '')
|
||||
)
|
||||
})
|
||||
|
||||
if (exist) {
|
||||
// 已存在:直接选择这个地址,走原有 chooseAddress 流程
|
||||
chooseAddress(exist)
|
||||
} else {
|
||||
// 不存在:走新增地址流程
|
||||
addressStore.setAddressLocation({
|
||||
displayName: data.displayName,
|
||||
formattedAddress: data.formattedAddress,
|
||||
longitude: data.location.lng,
|
||||
latitude: data.location.lat,
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/address/choose-type'
|
||||
})
|
||||
}, 300)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
useEventEmit(EventEnum.CHOOSE_ADDRESS, (data) => {
|
||||
console.log('搜索的地址信息', data)
|
||||
if(data) {
|
||||
addressStore.setAddressLocation({
|
||||
displayName: data.displayName,
|
||||
formattedAddress: data.formattedAddress,
|
||||
longitude: data.location.lng,
|
||||
latitude: data.location.lat,
|
||||
})
|
||||
setTimeout(()=> {
|
||||
uni.navigateTo({
|
||||
url: '/pages/address/choose-type'
|
||||
})
|
||||
}, 300)
|
||||
}
|
||||
})
|
||||
onShow(()=> {
|
||||
getAddressList()
|
||||
})
|
||||
onLoad((options: any)=> {
|
||||
if(options.id) {
|
||||
initialAddressIdFromQuery.value = options.id
|
||||
currentAddressId.value = options.id
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user