riku
2025-07-16 c23ac06446a9a1edc41cc13723e5d0b8eabdfd63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { ref } from 'vue';
import { defineStore } from 'pinia';
import deviceApi from '@/api/deviceApi';
import { useFetchData } from '@/composables/fetchData';
 
// 走航设备
export const useDeviceStore = defineStore('device', () => {
  const deviceList = ref([]);
  const { loading, fetchData } = useFetchData();
 
  function getDevice(deviceType) {
    if (deviceType) {
      return deviceList.value.filter((v) => v.deviceType == deviceType);
    } else {
      return deviceList.value;
    }
  }
 
  function fetchDevice(type) {
    return fetchData((page, pageSize) => {
      return deviceApi
        .fethchDevice({ type: type, page, pageSize })
        .then((res) => {
          deviceList.value = res.data.sort((a, b) => {
            return a.deviceCode < b.deviceCode ? -1 : 1;
          });
          return res;
        });
    });
  }
 
  function deleteDevice(deviceCode) {
    return fetchData(() => {
      return deviceApi.deleteDevice(deviceCode).then((res) => {
        let index = -1;
        for (let i = 0; i < deviceList.value.length; i++) {
          const e = deviceList.value[i];
          if (e.deviceCode == deviceCode) {
            index = i;
            break;
          }
        }
        if (index != -1) {
          deviceList.value.splice(index, 1);
        }
        return res;
      });
    });
  }
 
  return { deviceList, loading, getDevice, fetchDevice, deleteDevice };
});