| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import {getPaperBatches} from "@/api/dz/papers.js";
- import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";
- const key = Symbol('PaperBatchCondition')
- export const useProvidePaperBatchCondition = function (type, autoSelectFirst = false) {
- // 批次
- const buildType = type
- const batchId = ref('')
- const batchList = ref([])
- const batchEvent = createEventHook()
- let isInitialized = false
- onMounted(async () => {
- const res = await getPaperBatches()
- batchList.value = res.data
-
- // 如果启用自动选择且列表不为空,且是首次初始化,默认选中最新的第一个
- if (autoSelectFirst && batchList.value.length > 0 && !isInitialized && !batchId.value) {
- isInitialized = true
- // 按创建时间降序排序,选择最新的第一个
- const sorted = [...batchList.value].sort((a, b) => {
- const timeA = a.createTime || a.create_time || 0
- const timeB = b.createTime || b.create_time || 0
- return timeB - timeA
- })
- batchId.value = sorted[0].batchId
- }
- })
- watch(batchId, async (batchId) => {
- if (batchId) {
- const payload = {buildType, batchId}
- await batchEvent.trigger(payload)
- }
- })
- const payload = {buildType, batchId, batchList, onBatchReady: batchEvent.on}
- provideLocal(key, payload)
- return payload
- }
- export const useInjectPaperBatchCondition = function () {
- return injectLocal(key)
- }
|