index.vue 3.46 KB
<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>