- 组织级 store 统一提供 resetForOrganization,清空缓存、查询参数、分页与 loading - 加载方法提交状态前校验捕获的公司 ID 与上下文版本,旧公司延迟响应不再覆盖新公司 - 页面重挂载清理局部状态(物料选择、单据 ID、申请详情、审批弹窗) - 新增切换守卫,脏表单时取消切换不清空数据、保存期间禁止切换 - 接上库存事件消费与写请求的保存互斥标记 - 仓库配置页在已有仓库/库位时隐藏新增入口,仅保留编辑与启停
82 lines
3.1 KiB
JavaScript
82 lines
3.1 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { applicationApi } from '../api/application'
|
|
import { useOrganizationStore } from './organization'
|
|
import {
|
|
captureOrganizationContext,
|
|
isCurrentOrganizationContext,
|
|
shouldDiscardResponse
|
|
} from '../utils/organizationContext'
|
|
|
|
export const useApplicationStore = defineStore('application', () => {
|
|
const myApplications = ref([])
|
|
const loading = ref(false)
|
|
const myPage = ref({ currentPage: 1, pageSize: 20, total: 0, totalPages: 0 })
|
|
|
|
// 列表当前生效的筛选条件。取消申请后会在原地重新拉取列表,那次拉取必须沿用
|
|
// 用户选中的状态,否则列表会悄悄退回全量,而下拉框里还显示着选中的筛选值。
|
|
const filters = ref({ status: '' })
|
|
|
|
async function fetchMyApplications(status = '', pageNum = 1) {
|
|
filters.value = { status }
|
|
// 捕获发起时的公司上下文快照:响应回来时用它判断是否已被切换作废。
|
|
const snapshot = captureOrganizationContext()
|
|
loading.value = true
|
|
try {
|
|
const res = await applicationApi.getMy(status, pageNum, myPage.value.pageSize)
|
|
if (!isCurrentOrganizationContext(snapshot)) return
|
|
myApplications.value = res.data?.items || res.data || []
|
|
myPage.value.total = res.data?.total || myApplications.value.length
|
|
myPage.value.currentPage = pageNum
|
|
myPage.value.totalPages = Math.ceil(myPage.value.total / myPage.value.pageSize) || 1
|
|
} catch (error) {
|
|
if (shouldDiscardResponse(error)) return
|
|
throw error
|
|
} finally {
|
|
if (isCurrentOrganizationContext(snapshot)) loading.value = false
|
|
}
|
|
}
|
|
|
|
async function createApplication(data) {
|
|
const organization = useOrganizationStore()
|
|
organization.beginSaving()
|
|
try {
|
|
const res = await applicationApi.create(data)
|
|
// 这处刻意清空筛选并回到第 1 页,和上面「沿用筛选」的规矩相反:刚提交的是
|
|
// 待审批单,若沿用「已通过」之类的筛选就看不见它,用户会以为没提交成功。
|
|
// 清空的是 store 这份状态,页面那份(下拉框)由 Application.vue 同步重置。
|
|
await fetchMyApplications('', 1)
|
|
return res
|
|
} finally {
|
|
organization.endSaving()
|
|
}
|
|
}
|
|
|
|
async function cancelApplication(id) {
|
|
const organization = useOrganizationStore()
|
|
organization.beginSaving()
|
|
try {
|
|
const res = await applicationApi.cancel(id)
|
|
await fetchMyApplications(filters.value.status, myPage.value.currentPage)
|
|
return res
|
|
} finally {
|
|
organization.endSaving()
|
|
}
|
|
}
|
|
|
|
// 公司范围变更时清空共享缓存:我的申请、筛选、分页与加载态。
|
|
function resetForOrganization() {
|
|
myApplications.value = []
|
|
loading.value = false
|
|
myPage.value = { currentPage: 1, pageSize: 20, total: 0, totalPages: 0 }
|
|
filters.value = { status: '' }
|
|
}
|
|
|
|
useOrganizationStore().registerOrganizationReset(resetForOrganization)
|
|
|
|
return {
|
|
myApplications, loading, myPage,
|
|
fetchMyApplications, createApplication, cancelApplication, resetForOrganization
|
|
}
|
|
})
|