spring
2 天以前 6e763136fdf4469143ebbae0b717eb8e9b0ca954
src/pages/routingInspection/index.vue
@@ -2,7 +2,14 @@
  <view>
    <wd-row>
      <wd-col :span="21">
        <wd-search placeholder-left hide-cancel></wd-search>
        <wd-search
          v-model="searchKeyword"
          placeholder="请输入班组名称"
          placeholder-left
          hide-cancel
          @search="handleSearch"
          @clear="handleClear"
        ></wd-search>
      </wd-col>
      <wd-col :span="3">
        <view class="scan_box" @click="openScan">
@@ -17,64 +24,64 @@
        :title="`${item.deviceModel}(待检查${item.pendingNum}条)`"
        class="tab_bg"
      >
        <ProductList :api="RoutingInspectionApi.getInspectListByPatrol" :ProList="item" />
        <ProductList
          :key="searchKey"
          :api="RoutingInspectionApi.getInspectListByPatrol"
          :ProList="{ ...item, teamName: searchKeyword }"
        />
      </wd-tab>
    </wd-tabs>
    <Scan ref="scanRef" emitName="scan" />
    <wd-toast />
  </view>
</template>
<script lang="ts" setup>
import { ref, reactive, computed, onMounted, onUnmounted } from "vue";
import { onShow, onHide } from "@dcloudio/uni-app";
import ProductList from "./list/index.vue";
import Scan from "@/components/scan/index.vue";
import { useUserStore } from "@/store/modules/user";
import reportApi from "@/api/work/report";
import { useToast } from "wot-design-uni";
import RoutingInspectionApi from "@/api/routingInspection/routingInspection";
import { useScanCode } from "@/composables/useScanCode";
const scanRef = ref();
const userStore = useUserStore();
const userInfo: any = computed(() => userStore.userInfo);
const toast = useToast();
const tab = ref<number>(0);
const patrolList = ref<any[]>([]); // 巡检设备列表数据
const searchKeyword = ref<string>(""); // 搜索关键词(班组名称)
const searchKey = ref<number>(0); // 用于强制刷新列表
// 使用扫码管理 composable(全局监听器,不随页面切换关闭)
const { deviceUid, deviceModel, hasScanned, displayText, loadFromCache, enableListener } =
  useScanCode("scanIndex");
const handlePatrolData = (index: number, count: number) => {
  // 可以在这里更新特定巡检设备的待检查数量
  // 例如:patrolList.value[index].pendingNum = count;
};
// 处理搜索
const handleSearch = (value: string) => {
  console.log("搜索班组:", value);
  searchKey.value++; // 更新 key 强制刷新列表
};
// 处理清空搜索
const handleClear = () => {
  console.log("清空搜索");
  searchKeyword.value = "";
  searchKey.value++; // 更新 key 强制刷新列表
};
const openScan = () => {
  scanRef.value.triggerScan();
};
const getScanCode = async () => {
  const { code } = await reportApi.sendWorkTime({
    userName: userInfo.value.userName,
  console.log("index.vue - 点击扫码按钮(全局扫码模式,无需手动触发)");
  // 全局扫码模式下,硬件扫码会自动触发,无需手动调用
  uni.showToast({
    title: "请使用扫码枪扫描",
    icon: "none",
  });
  if (code == 200) {
    toast.success("扫码成功");
  }
};
// 获取特定巡检设备的数据
const getPatrolData = (item: any) => {
  return async (params: any) => {
    // 这里可以根据item中的信息调用相应的接口获取详情
    // 返回的数据格式需要与ProductList组件期望的格式一致
    return {
      code: 200,
      data: {
        type: "巡检",
        data: {
          total: 0,
          records: [],
        },
      },
    };
  };
};
// 获取巡检设备列表
@@ -89,24 +96,32 @@
  }
};
// 确保先移除再添加监听
const setupScanListener = () => {
  uni.$off("scan", getScanCode); // 先移除旧的
  uni.$on("scan", getScanCode); // 再添加新的
};
onMounted(() => {
  // 开启广播监听事件
  setupScanListener();
  console.log("显示1");
  // 页面加载时获取巡检设备列表
  loadPatrolList();
  // 启用全局监听器
  enableListener();
  console.log("index.vue - onMounted");
});
onUnmounted(() => {
  // 开启广播监听事件
  uni.$off("scan", getScanCode);
  console.log("离开1");
onShow(() => {
  console.log("========== index.vue - onShow 触发 ==========");
  // 页面显示时重新启用监听器(确保监听器有效)
  enableListener();
  // 加载缓存(更新UI显示)
  loadFromCache();
  // 检查是否需要刷新列表(只有提交成功后才刷新)
  const needRefresh = uni.getStorageSync("needRefreshInspectionList");
  if (needRefresh) {
    console.log("检测到需要刷新列表,开始刷新...");
    // 重新加载巡检设备列表(刷新待检查数量)
    loadPatrolList();
    // 强制刷新 ProductList 组件
    searchKey.value++;
    // 清除刷新标记
    uni.removeStorageSync("needRefreshInspectionList");
  }
});
</script>
@@ -146,4 +161,4 @@
.statistics_box {
  margin: 15px;
}
</style>
</style>