index.vue 4.1 KB
<script setup lang="ts">
import { ref, computed, onBeforeUnmount, watchEffect, toRaw, unref } from 'vue'
import { icon } from '@/plugins'
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { SettingItemBox } from '@/components/Pages/ChartItemSetting'
import { RequestDataTypeEnum } from '@/enums/external/httpEnum'
import { ChartDataMatchingAndShow } from '../../../external/components/ChartDataMatchingAndShow'
import { newFunctionHandle } from '@/utils'
import { useTargetData } from '../../../../hooks/useTargetData.hook'
import { NButton, NSelect, NTooltip, NIcon, SelectOption } from 'naive-ui'
import { RequestInfoPanel } from '../RequestInfoPanel'
import { RequestModal } from '../RequestModal'
import { customRequest } from '@/api/external/customRequest'

const { HelpOutlineIcon, FlashIcon } = icon.ionicons5
const { targetData } = useTargetData()

const requestModal = ref<InstanceType<typeof RequestModal>>()

const designStore = useDesignStore()


const selectedRequestType = ref(targetData.value.request.requestDataType || RequestDataTypeEnum.AJAX)

const getApiRequestType: SelectOption[] = [
  { label: '自定义请求', value: RequestDataTypeEnum.AJAX },
  { label: '公共接口', value: RequestDataTypeEnum.Pond }
]

// 是否展示数据分析
const loading = ref(false)
const showMatching = ref(false)

let firstFocus = 0
let lastFilter: any = undefined

// 发送请求
const sendHandle = async () => {
  if (!targetData.value?.request) return

  loading.value = true
  try {
    console.log('enter')
    const res = await customRequest(toRaw(targetData.value.request))
    loading.value = false
    if (res) {
      targetData.value.option.dataset = newFunctionHandle(res, res, targetData.value.filter)
      showMatching.value = true
      return
    }
    window['$message'].warning('数据异常,请检查参数!')
  } catch (error) {
    loading.value = false
    window['$message'].warning('数据异常,请检查参数!')
  }
}

// 颜色
const themeColor = computed(() => {
  return designStore.getAppTheme
})

const handleClickPanel = () => {
  unref(requestModal)?.openModal(true, unref(selectedRequestType))
}

watchEffect(() => {
  const filter = targetData.value?.filter
  if (lastFilter !== filter && firstFocus) {
    lastFilter = filter
    sendHandle()
  }
  firstFocus++
})

onBeforeUnmount(() => {
  lastFilter = null
})


</script>

<template>
  <div class="things-kit-chart-configurations-data-ajax">
    <SettingItemBox name="接口方式" :alone="true">
      <NSelect v-model:value="selectedRequestType" :options="getApiRequestType" />
    </SettingItemBox>

    <RequestInfoPanel @clickPanel="handleClickPanel" />

    <SettingItemBox :alone="true">
      <template #name>
        测试
        <NTooltip trigger="hover">
          <template #trigger>
            <NIcon size="21" :depth="3">
              <HelpOutlineIcon />
            </NIcon>
          </template>
          默认赋值给 dataset 字段
        </NTooltip>
      </template>
      <NButton type="primary" ghost @click="sendHandle">
        <template #icon>
          <NIcon>
            <FlashIcon />
          </NIcon>
        </template>
        发送请求
      </NButton>
    </SettingItemBox>

    <!-- 底部数据展示 -->
    <ChartDataMatchingAndShow :show="showMatching && !loading" :ajax="true"></ChartDataMatchingAndShow>

    <!-- 骨架图 -->
    <go-skeleton :load="loading" :repeat="3"></go-skeleton>

    <RequestModal ref="requestModal" />
  </div>
</template>



<style lang="scss" scoped>
@include thingsKit('chart-configurations-data-ajax') {
  .n-card-shallow {
    &.n-card {
      @extend .go-background-filter;

      @include deep() {
        .n-card__content {
          padding: 10px;
        }
      }
    }

    .edit-text {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 325px;
      height: 270px;
      cursor: pointer;
      opacity: 0;
      transition: all 0.3s;
      @extend .go-background-filter;
      backdrop-filter: blur(2px) !important;
    }

    &:hover {
      border-color: v-bind('themeColor');

      .edit-text {
        opacity: 1;
      }
    }
  }
}
</style>