zhb 1 hete
szülő
commit
a5c4a2ec45
3 módosított fájl, 57 hozzáadás és 5 törlés
  1. 37 5
      components/cwg-complex-search.vue
  2. 9 0
      components/cwg-tabel.vue
  3. 11 0
      utils/pageActive.js

+ 37 - 5
components/cwg-complex-search.vue

@@ -117,6 +117,7 @@
 <script setup>
 import { ref, computed, watch, onMounted, nextTick, onUnmounted } from 'vue'
 import { useI18n } from 'vue-i18n'
+import { isRouteOnTop } from '@/utils/pageActive'
 
 const { t } = useI18n()
 
@@ -313,6 +314,27 @@ const nonDateField = computed(() => {
 const clonePlain = (val) => JSON.parse(JSON.stringify(val ?? {}))
 const isEqual = (a, b) => JSON.stringify(a ?? {}) === JSON.stringify(b ?? {})
 
+/** 所属页面 route,离开栈顶后不再 emit search */
+const ownerPageRoute = ref('')
+
+/** 仅字段结构变化时重新初始化(语言切换改 label 不触发) */
+const getFieldsSignature = (fields) => {
+    if (!fields?.length) return ''
+    return JSON.stringify(
+        fields.filter(Boolean).map((field) => ({
+            key: field.key,
+            type: field.type,
+            defaultValue: field.defaultValue,
+            optionsLength: Array.isArray(field.options) ? field.options.length : 0,
+            optionValues: Array.isArray(field.options)
+                ? field.options.map((o) => o?.value ?? o?.text ?? o)
+                : undefined,
+        }))
+    )
+}
+
+let fieldsSignature = ''
+
 // 监听外部 modelValue 变化
 watch(() => props.modelValue, (newVal) => {
     if (!newVal) return
@@ -374,6 +396,7 @@ const resetForm = () => {
 }
 // 触发查询
 const handleSearch = () => {
+    if (!isRouteOnTop(ownerPageRoute.value)) return
     emit('search', { ...formData.value })
 }
 
@@ -430,13 +453,22 @@ const applyFilter = () => {
     handleSearch()
 }
 
-// 监听fields变化,重新初始化
-watch(() => props.fields, () => {
-    initFormData()
-}, { deep: true, immediate: false })
+// 仅 fields 结构变化时重新初始化
+watch(
+    () => props.fields,
+    (fields) => {
+        const nextSig = getFieldsSignature(fields)
+        if (nextSig === fieldsSignature) return
+        fieldsSignature = nextSig
+        initFormData()
+    },
+    { deep: false }
+)
 
 onMounted(() => {
-    // 初始化表单数据
+    const pages = getCurrentPages()
+    ownerPageRoute.value = pages[pages.length - 1]?.route || ''
+    fieldsSignature = getFieldsSignature(props.fields)
     initFormData()
 })
 

+ 9 - 0
components/cwg-tabel.vue

@@ -168,6 +168,7 @@ import { ref, computed, watch, onMounted, onUnmounted, nextTick } from 'vue'
 import { getNoteText } from '@/utils/noteHelper';
 import useUserStore from "@/stores/use-user-store";
 import { useI18n } from "vue-i18n";
+import { isRouteOnTop } from '@/utils/pageActive'
 const userStore = useUserStore();
 const { locale } = useI18n();
 const props = defineProps({
@@ -563,8 +564,12 @@ const setDetailVisible = (visible) => {
     detailVisible.value = visible
 }
 
+// 表格挂载时所属页面(uni-app 页面栈未销毁时,离开页面后不再请求)
+const ownerPageRoute = ref('')
+
 // ========== 数据加载 ==========
 const loadData = async () => {
+    if (!isRouteOnTop(ownerPageRoute.value)) return
     if (props.data) {
         tableData.value = props.data
         if (props.summaryData) {
@@ -660,10 +665,12 @@ const loadData = async () => {
 }
 // 刷新表格
 const refreshTable = () => {
+    if (!isRouteOnTop(ownerPageRoute.value)) return
     pagination.value.current = 1
     loadData()
 }
 const reload = () => {
+    if (!isRouteOnTop(ownerPageRoute.value)) return
     loadData()
 }
 const pageLoading = ref(false)
@@ -741,6 +748,8 @@ watch(() => props.api, () => {
 }, { deep: true })
 // ========== 生命周期 ==========
 onMounted(() => {
+    const pages = getCurrentPages()
+    ownerPageRoute.value = pages[pages.length - 1]?.route || ''
     checkIsMobile()
     // #ifdef H5
     window.addEventListener('resize', handleResize)

+ 11 - 0
utils/pageActive.js

@@ -0,0 +1,11 @@
+/** 当前栈顶页面 route,如 pages/customer/trade-history */
+export function getTopPageRoute() {
+  const pages = getCurrentPages()
+  return pages[pages.length - 1]?.route || ''
+}
+
+/** 指定 route 是否为栈顶(页面离开栈顶后应停止请求) */
+export function isRouteOnTop(route) {
+  if (!route) return true
+  return getTopPageRoute() === route
+}