index.vue
3.46 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<script lang="ts" setup>
  import { ComponentPropsConfigType, DataFetchUpdateFn } from '/@/views/visual/packages/index.type';
  import { option } from './config';
  import { useDataFetch } from '/@/views/visual/packages/hook/useSocket';
  import { Slider, Spin } from 'ant-design-vue';
  import { computed, ref, unref } from 'vue';
  import { useComponentScale } from '../../../hook/useComponentScale';
  import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
  import { useSendCommand } from '../../../hook/useSendCommand';
  import { useReceiveValue } from '../../../hook/useReceiveValue';
  const props = defineProps<{
    config: ComponentPropsConfigType<typeof option>;
  }>();
  const sliderValue = ref<number>(33);
  const oldSliderValue = ref<number>(33);
  const sMin = ref<number>(0);
  const sMax = ref<number>(100);
  const { loading, sendCommand } = useSendCommand();
  const getDesign = computed(() => {
    const { option, persetOption } = props.config;
    const { componentInfo, attribute, attributeRename, attributeName } = option;
    const { controlBarColor: persetControlBarColor, fonColor: persetFontColor } =
      persetOption || {};
    const { controlBarColor, fontColor } = componentInfo || {};
    return {
      attribute: attributeRename || attributeName || attribute,
      controlBarColor: controlBarColor ?? persetControlBarColor,
      fontColor: fontColor ?? persetFontColor,
    };
  });
  const index = ref<number>(0);
  const handleChange = async (e) => {
    index.value++;
    if (index.value == 1) {
      oldSliderValue.value = unref(sliderValue);
    }
    sliderValue.value = e;
  };
  const handleAfterChange = async (e) => {
    const flag = await sendCommand(props.config.option, unref(sliderValue));
    flag
      ? ((sliderValue.value = e), (oldSliderValue.value = sliderValue.value), (index.value = 0))
      : (sliderValue.value = unref(oldSliderValue));
  };
  const { getNumberValue } = useReceiveValue();
  const updateFn: DataFetchUpdateFn = (message, attribute) => {
    const { data = {} } = message;
    const [latest] = data[attribute] || [];
    const [_, value] = latest;
    sliderValue.value = getNumberValue(value);
  };
  useDataFetch(props, updateFn);
  const { getScale } = useComponentScale(props);
</script>
<template>
  <main class="w-full h-full flex flex-col justify-center">
    <DeviceName :config="config" class="text-center" />
    <main :style="getScale">
      <Spin :spinning="loading" class="w-full h-full">
        <div class="flex flex-col" style="width: 80%">
          <span
            :style="{ color: getDesign.fontColor }"
            class="font-bold text-xl mt-3 truncate text-center"
            >{{ sliderValue }}</span
          >
          <Slider
            :style="{ '--slider-color': getDesign.controlBarColor }"
            class="no-drag"
            :value="sliderValue"
            :min="sMin"
            :max="sMax"
            @change="handleChange"
            @afterChange="handleAfterChange"
          />
          <span
            :style="{ color: getDesign.fontColor }"
            class="mt-3 truncate font-bold text-xs text-center"
          >
            {{ getDesign.attribute || '属性' }}
          </span>
        </div>
      </Spin>
    </main>
  </main>
</template>
<style lang="less" scoped>
  :deep(.ant-slider-track) {
    background: var(--slider-color) !important;
  }
  :deep(.ant-spin-container) {
    display: flex !important;
    justify-content: center !important;
  }
</style>