refactor: 拆分小程序 vue 结构
This commit is contained in:
52
src/pages/menu/composables/menu-page/data-actions.ts
Normal file
52
src/pages/menu/composables/menu-page/data-actions.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { Ref } from 'vue'
|
||||
import type {
|
||||
FulfillmentScene,
|
||||
MiniCategory,
|
||||
MiniMenuSection
|
||||
} from '@/shared'
|
||||
import { getCategories, getMenu } from '@/services'
|
||||
import type { useAppStore } from '@/stores'
|
||||
|
||||
type AppStoreInstance = ReturnType<typeof useAppStore>
|
||||
|
||||
export function createMenuDataActions (payload: {
|
||||
appStore: AppStoreInstance
|
||||
categories: Ref<MiniCategory[]>
|
||||
errorMessage: Ref<string>
|
||||
loading: Ref<boolean>
|
||||
sections: Ref<MiniMenuSection[]>
|
||||
}) {
|
||||
const { appStore, categories, errorMessage, loading, sections } = payload
|
||||
|
||||
async function loadMenu () {
|
||||
loading.value = true
|
||||
errorMessage.value = ''
|
||||
|
||||
try {
|
||||
await appStore.initBootstrap()
|
||||
await appStore.initStores()
|
||||
|
||||
const [nextCategories, nextSections] = await Promise.all([
|
||||
getCategories(appStore.currentStore.id, appStore.scene, appStore.channel),
|
||||
getMenu(appStore.currentStore.id, appStore.scene, appStore.channel)
|
||||
])
|
||||
|
||||
categories.value = nextCategories
|
||||
sections.value = nextSections
|
||||
} catch (error: unknown) {
|
||||
errorMessage.value = error instanceof Error ? error.message : '菜单加载失败,请检查接口是否可用'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSceneChange (value: string) {
|
||||
appStore.setScene(value as FulfillmentScene)
|
||||
await loadMenu()
|
||||
}
|
||||
|
||||
return {
|
||||
handleSceneChange,
|
||||
loadMenu
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user