|
|
@@ -24,7 +24,7 @@
|
|
|
</fs-page>
|
|
|
</template>
|
|
|
<script lang="ts" setup name="borrow">
|
|
|
-import { ref, onMounted,onActivated, onBeforeUnmount,nextTick ,provide } from 'vue';
|
|
|
+import { ref, onMounted, onActivated, onBeforeUnmount, nextTick, provide } from 'vue';
|
|
|
import { useFs } from '@fast-crud/fast-crud';
|
|
|
import { createCrudOptions } from './crud';
|
|
|
// import { GetPermission } from './api';
|
|
|
@@ -37,9 +37,65 @@ import CollectEquipmentDialog from './component/CollectEquipment/index.vue';
|
|
|
import SettlementDialog from './component/CollectEquipment/SettlementDialog.vue';
|
|
|
import * as api from './api';
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
-import { useRoute } from 'vue-router';
|
|
|
+import { useRoute, onBeforeRouteUpdate } from 'vue-router';
|
|
|
+import type { LocationQuery } from 'vue-router';
|
|
|
+
|
|
|
const route = useRoute();
|
|
|
|
|
|
+function firstQueryString(v: unknown): string {
|
|
|
+ if (v == null) return '';
|
|
|
+ if (Array.isArray(v)) {
|
|
|
+ const x = v[0];
|
|
|
+ return x != null ? String(x) : '';
|
|
|
+ }
|
|
|
+ return String(v);
|
|
|
+}
|
|
|
+
|
|
|
+const BORROW_LIST_DEBUG = true;
|
|
|
+
|
|
|
+/** 路由 query → 借用列表搜索表单(与 crud 列 status / status__in 一致;禁止重复 status 键) */
|
|
|
+function buildBorrowListSearchFormFromRoute(q: LocationQuery) {
|
|
|
+ const raw = firstQueryString(
|
|
|
+ (q as Record<string, unknown>).status__in ?? q.status_in ?? q.status
|
|
|
+ ).trim();
|
|
|
+ if (BORROW_LIST_DEBUG) {
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log('[borrow/list] 路由 query', { ...q }, '→ 原始 status 链:', raw);
|
|
|
+ }
|
|
|
+ if (raw === '') {
|
|
|
+ const empty = { status: undefined, status__in: undefined };
|
|
|
+ if (BORROW_LIST_DEBUG) {
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log('[borrow/list] 无 status 条件 → doSearch form', empty);
|
|
|
+ }
|
|
|
+ return empty;
|
|
|
+ }
|
|
|
+ /* 多状态:逗号分隔,对应表单列 status__in */
|
|
|
+ if (raw.includes(',')) {
|
|
|
+ const f = { status: undefined, status__in: raw };
|
|
|
+ if (BORROW_LIST_DEBUG) {
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log('[borrow/list] 多状态(含逗号)→ form.status__in =', f.status__in);
|
|
|
+ }
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ const num = Number(raw);
|
|
|
+ if (Number.isFinite(num) && num > 0) {
|
|
|
+ const f = { status: num, status__in: undefined };
|
|
|
+ if (BORROW_LIST_DEBUG) {
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log('[borrow/list] 单状态(数字)→ form.status =', f.status);
|
|
|
+ }
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ const f = { status: raw, status__in: undefined };
|
|
|
+ if (BORROW_LIST_DEBUG) {
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log('[borrow/list] 单状态(非纯数字)→ form.status =', f.status);
|
|
|
+ }
|
|
|
+ return f;
|
|
|
+}
|
|
|
+
|
|
|
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions });
|
|
|
const showBorrowTypeDialog = ref(false);
|
|
|
const showCommonBorrowDialog = ref(false);
|
|
|
@@ -47,9 +103,18 @@ const showClassroomBorrowDialog = ref(false);
|
|
|
const showSpecialBorrowDialog = ref(false);
|
|
|
const showCollectDialog = ref(false);
|
|
|
const borrowForm = ref({});
|
|
|
-const showSettlementDialog = ref(false);
|
|
|
+const showSettlementDialog = ref(false);
|
|
|
|
|
|
+/** keep-alive 下首次进入会先后触发 onMounted 与 onActivated,避免重复 doSearch */
|
|
|
+const skipNextActivatedBorrowSearch = ref(false);
|
|
|
|
|
|
+/** 与 onActivated 协调:避免 keep-alive 首次 onMounted + onActivated 连续请求两次 */
|
|
|
+function applyRouteSearchToCrud() {
|
|
|
+ skipNextActivatedBorrowSearch.value = true;
|
|
|
+ crudExpose.doSearch({
|
|
|
+ form: buildBorrowListSearchFormFromRoute(route.query),
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
const lastFormSnapshot = ref({});
|
|
|
// provide('lastFormSnapshot', lastFormSnapshot);
|
|
|
@@ -367,28 +432,10 @@ async function handleRenewDevice(e: CustomEvent) {
|
|
|
|
|
|
let observer: MutationObserver | null = null;
|
|
|
|
|
|
-onMounted(() => {
|
|
|
- // 设置列权限
|
|
|
- // const newOptions = await handleColumnPermission(GetPermission, crudOptions);
|
|
|
- //重置crudBinding
|
|
|
- // resetCrudOptions(newOptions);
|
|
|
- // 刷新
|
|
|
- /* crudExpose.doRefresh(); */
|
|
|
- // window.addEventListener('borrow-add', () => {
|
|
|
- // showBorrowTypeDialog.value = true
|
|
|
- // });
|
|
|
- window.addEventListener('borrow-view', handleView);
|
|
|
- window.addEventListener('borrow-edit', handleEdit);
|
|
|
- window.addEventListener('borrow-remove', handleRemove);
|
|
|
- window.addEventListener('borrow-collect', handleCollect);
|
|
|
- window.addEventListener('borrow-return', handleReturn);
|
|
|
- window.addEventListener('borrow-settlement', handleSettlement);
|
|
|
- window.addEventListener('renew-device', handleRenewDevice);
|
|
|
-
|
|
|
-
|
|
|
+function bindBorrowActionbarButton(): boolean {
|
|
|
const fsActionbar = document.querySelector('.borrow-page .fs-actionbar');
|
|
|
- if (!fsActionbar) return;
|
|
|
- // 创建借用单按钮
|
|
|
+ if (!fsActionbar) return false;
|
|
|
+
|
|
|
function insertButton() {
|
|
|
if (fsActionbar && !fsActionbar.querySelector('.my-create-btn')) {
|
|
|
const button = document.createElement('button');
|
|
|
@@ -401,27 +448,59 @@ onMounted(() => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 先尝试插入一次
|
|
|
insertButton();
|
|
|
-
|
|
|
- // 监听 DOM 变化,确保 actionbar 出现时插入按钮
|
|
|
- // observer = new MutationObserver(() => {
|
|
|
- // insertButton();
|
|
|
- // });
|
|
|
- // observer.observe(document.body, { childList: true, subtree: true });
|
|
|
-
|
|
|
+ if (observer) {
|
|
|
+ observer.disconnect();
|
|
|
+ observer = null;
|
|
|
+ }
|
|
|
observer = new MutationObserver((_m, obs) => {
|
|
|
insertButton();
|
|
|
- // 插入完毕后停掉 observer
|
|
|
obs.disconnect();
|
|
|
});
|
|
|
observer.observe(fsActionbar, { childList: true });
|
|
|
- crudExpose.doSearch({
|
|
|
- form: {
|
|
|
- status: Number(route.query.status) || ''
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+onMounted(() => {
|
|
|
+ window.addEventListener('borrow-view', handleView);
|
|
|
+ window.addEventListener('borrow-edit', handleEdit);
|
|
|
+ window.addEventListener('borrow-remove', handleRemove);
|
|
|
+ window.addEventListener('borrow-collect', handleCollect);
|
|
|
+ window.addEventListener('borrow-return', handleReturn);
|
|
|
+ window.addEventListener('borrow-settlement', handleSettlement);
|
|
|
+ window.addEventListener('renew-device', handleRenewDevice);
|
|
|
+
|
|
|
+ /* 必须执行:此前若未找到 .fs-actionbar 会提前 return,导致从未 doSearch,URL 带参也不生效 */
|
|
|
+ nextTick(() => {
|
|
|
+ applyRouteSearchToCrud();
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!bindBorrowActionbarButton()) {
|
|
|
+ nextTick(() => {
|
|
|
+ if (!bindBorrowActionbarButton()) {
|
|
|
+ window.setTimeout(() => bindBorrowActionbarButton(), 100);
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+/** 同页仅 query 变化时(如 ?status=);勿 watch 全局 fullPath,keep-alive 下会误触发 */
|
|
|
+onBeforeRouteUpdate((to) => {
|
|
|
+ nextTick(() => {
|
|
|
+ crudExpose.doSearch({
|
|
|
+ form: buildBorrowListSearchFormFromRoute(to.query),
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|
|
|
|
|
|
+onActivated(() => {
|
|
|
+ if (skipNextActivatedBorrowSearch.value) {
|
|
|
+ skipNextActivatedBorrowSearch.value = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ crudExpose.doSearch({
|
|
|
+ form: buildBorrowListSearchFormFromRoute(route.query),
|
|
|
+ });
|
|
|
});
|
|
|
onBeforeUnmount(() => {
|
|
|
if (observer) {
|
|
|
@@ -440,11 +519,5 @@ onBeforeUnmount(() => {
|
|
|
window.removeEventListener('renew-device', handleRenewDevice);
|
|
|
});
|
|
|
|
|
|
-// onActivated(() => {
|
|
|
-// crudExpose.doRefresh();
|
|
|
-// });
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
</script>
|
|
|
<style lang="scss" scoped></style>
|