Files
TakeoutSaaS.C-Side-Mini-Pro…/src/pages/menu/composables/menu-page/data-actions.ts

53 lines
1.4 KiB
TypeScript

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
}
}