83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import { ref } from 'vue'
|
|||
|
|
import { getProvince, tableAddress } from '@/api/address'
|
||
|
|
import type { Address } from '@/models/address'
|
||
|
|
import { loading, loadingMore } from '@/utils'
|
||
|
|
import { runOnceAsync, resetOnceAsyncByPrefix } from '@/composables/useOnceAsync'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Shared address page datasets — mobile and desktop views read this module state only.
|
||
|
|
*/
|
||
|
|
let initPromise: Promise<void> | null = null
|
||
|
|
const initialized = ref(false)
|
||
|
|
|
||
|
|
export const addressItems = ref<Address[]>([])
|
||
|
|
export const addressProvinceList = ref<any[]>([])
|
||
|
|
|
||
|
|
export function isAddressPageDataInitialized() {
|
||
|
|
return initialized.value
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function refreshAddressItems() {
|
||
|
|
try {
|
||
|
|
loading.value = true
|
||
|
|
const data = await tableAddress({ page: 0, limit: 150 })
|
||
|
|
addressItems.value = data.rows
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Address list refresh error:', error)
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
loadingMore.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function loadAddressItems() {
|
||
|
|
const data = await tableAddress({ page: 0, limit: 150 })
|
||
|
|
addressItems.value = data.rows
|
||
|
|
}
|
||
|
|
|
||
|
|
async function loadProvinces() {
|
||
|
|
addressProvinceList.value = await getProvince()
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function initAddressPageData(force = false) {
|
||
|
|
if (!force && initialized.value) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!force && initPromise) {
|
||
|
|
return initPromise
|
||
|
|
}
|
||
|
|
|
||
|
|
initPromise = (async () => {
|
||
|
|
try {
|
||
|
|
if (force) {
|
||
|
|
resetAddressPageData()
|
||
|
|
}
|
||
|
|
|
||
|
|
loading.value = true
|
||
|
|
|
||
|
|
await Promise.all([
|
||
|
|
runOnceAsync('address:items', () => loadAddressItems(), force),
|
||
|
|
runOnceAsync('address:provinces', () => loadProvinces(), force),
|
||
|
|
])
|
||
|
|
|
||
|
|
initialized.value = true
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Address page initialization error:', error)
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
initPromise = null
|
||
|
|
}
|
||
|
|
})()
|
||
|
|
|
||
|
|
return initPromise
|
||
|
|
}
|
||
|
|
|
||
|
|
export function resetAddressPageData() {
|
||
|
|
initialized.value = false
|
||
|
|
initPromise = null
|
||
|
|
resetOnceAsyncByPrefix('address:')
|
||
|
|
addressItems.value = []
|
||
|
|
addressProvinceList.value = []
|
||
|
|
}
|