fix:修复无限发送请求,修复不能独立配置,修复不能动态更新

This commit is contained in:
Free-sss 2025-09-04 15:31:16 +08:00
parent 5bf82ab58b
commit 30d47df4bd
7 changed files with 114 additions and 37 deletions

View File

@ -27,7 +27,7 @@
{ {
"rank": "TOP1", "rank": "TOP1",
"name": "株机公司", "name": "株机公司",
"value": 932 "value": 101
}, },
{ {
"rank": "TOP2", "rank": "TOP2",
@ -330,7 +330,7 @@
{ {
"title": "调漆作业室", "title": "调漆作业室",
"label": "智控场景", "label": "智控场景",
"value": null, "value": 0,
"color": "#FFD700", "color": "#FFD700",
"image": "scene-control.png" "image": "scene-control.png"
} }

View File

@ -2,6 +2,7 @@ import { PublicConfigClass } from '@/packages/public'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { TopAlarmsConfig } from './index' import { TopAlarmsConfig } from './index'
import dataJson from './data.json' import dataJson from './data.json'
import { cloneDeep } from 'lodash'
export const option = { export const option = {
sceneCode: 'T04', sceneCode: 'T04',
componentIndexKey: "a", componentIndexKey: "a",
@ -51,6 +52,6 @@ export const option = {
export default class Config extends PublicConfigClass implements CreateComponentType { export default class Config extends PublicConfigClass implements CreateComponentType {
public key = TopAlarmsConfig.key public key = TopAlarmsConfig.key
public chartConfig = TopAlarmsConfig public chartConfig = TopAlarmsConfig
public option = option public option = cloneDeep(option)
} }

View File

@ -30,15 +30,15 @@
<span class="rank-icon"></span> <span class="rank-icon"></span>
{{ index + 1 }} {{ index + 1 }}
</span> </span>
<span class="name" :style="{ color: option.nameColor }">{{ item.enterpriseName }}</span> <span class="name" :style="{ color: option.nameColor }">{{ item.name }}</span>
</div> </div>
<div class="item-value"> <div class="item-value">
<span class="value" :style="{ color: option.valueColor }">{{ item.alarmCount }}</span> <span class="value" :style="{ color: option.valueColor }">{{ item.value }}</span>
</div> </div>
<div class="progress-bar-wrapper"> <div class="progress-bar-wrapper">
<div class="progress-bar"> <div class="progress-bar">
<div class="progress" :style="{ <div class="progress" :style="{
width: calculateWidth(item.alarmCount), width: calculateWidth(item.value),
background: `linear-gradient(to right, ${option.barColorStart}, ${option.barColorEnd})` background: `linear-gradient(to right, ${option.barColorStart}, ${option.barColorEnd})`
}"></div> }"></div>
</div> </div>

View File

@ -2,6 +2,7 @@ import { PublicConfigClass } from '@/packages/public'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { ParkingSceneConfig } from './index' import { ParkingSceneConfig } from './index'
import dataJson from './data.json' import dataJson from './data.json'
import { cloneDeep } from 'lodash'
export const option = { export const option = {
sceneCode: "", sceneCode: "",
@ -25,5 +26,5 @@ export const option = {
export default class Config extends PublicConfigClass implements CreateComponentType { export default class Config extends PublicConfigClass implements CreateComponentType {
public key = ParkingSceneConfig.key public key = ParkingSceneConfig.key
public chartConfig = ParkingSceneConfig public chartConfig = ParkingSceneConfig
public option = option public option = cloneDeep(option)
} }

View File

@ -225,52 +225,84 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, computed, onMounted, watch } from 'vue' import { PropType, computed, onMounted, watch, ref } from 'vue'
import { option as configOption } from './config' import { option as configOption } from './config'
import { ParkingSceneConfig } from './index' import { ParkingSceneConfig } from './index'
import axios from 'axios'; import axios from 'axios';
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
type: Object as PropType<{ option: typeof configOption }>, type: Object as PropType<{ option: typeof configOption }>,
required: true required: true
} }
}) })
const key = ParkingSceneConfig.key; const key = ParkingSceneConfig.key;
const option = computed(() => props.chartConfig.option) const option = computed(() => props.chartConfig.option)
const isFetching = ref(false) //
//
const getStaticData = async (key: string, componentIndexKey: string, sceneCode: string) => { const getStaticData = async (key: string, componentIndexKey: string, sceneCode: string) => {
let dataTemp = option.value.dataset
try { try {
const response = await axios.get('/staticData/static.json'); const response = await axios.get('/staticData/static.json');
if (response.data) { if (response.data) {
console.log('静态数据:', response.data); console.log('静态数据:', response.data);
dataTemp = response.data[sceneCode]?.[key]?.[componentIndexKey]?.['source']; const dataTemp = response.data[sceneCode]?.[key]?.[componentIndexKey]?.['source'];
if (!dataTemp) { if (!dataTemp) {
console.warn(`Data not found for sceneCode: ${sceneCode}, key: ${key}, componentIndexKey: ${componentIndexKey}`); console.warn(`Data not found for sceneCode: ${sceneCode}, key: ${key}, componentIndexKey: ${componentIndexKey}`);
return props.chartConfig.option.dataset; //
} }
return dataTemp;
} }
console.log("datatemp:", dataTemp)
} catch (err) { } catch (err) {
// console.error('static.json:', err); console.error('获取static.json失败:', err);
} }
return dataTemp return props.chartConfig.option.dataset; //
} }
//
const fetchData = async () => {
//
if (isFetching.value) return;
isFetching.value = true;
try {
let data;
//
data = await getStaticData(key, option.value.componentIndexKey, option.value.sceneCode);
props.chartConfig.option.dataset = data;
if (JSON.stringify(data) !== JSON.stringify(option.value.dataset)) {
console.log('更新数据:', data);
}
} finally {
isFetching.value = false;
}
}
watch( watch(
() => props.chartConfig.option.dataset, () => [
async (newData: any) => { props.chartConfig.option.sceneCode,
option.value.dataset = await getStaticData(key, option.value.componentIndexKey, option.value.sceneCode); props.chartConfig.option.componentIndexKey,
props.chartConfig.option.dataset
],
async () => {
await fetchData();
}, },
{ {
immediate: true, immediate: true,
deep: false deep: true
} }
) )
//
onMounted(async () => { onMounted(async () => {
option.value.dataset = await getStaticData(key, option.value.componentIndexKey, option.value.sceneCode); await fetchData();
}) })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -2,6 +2,7 @@ import { PublicConfigClass } from '@/packages/public'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { SceneDistributionConfig } from './index' import { SceneDistributionConfig } from './index'
import dataJson from './data.json' import dataJson from './data.json'
import { cloneDeep } from 'lodash'
export const option = { export const option = {
sceneCode: 'T06', sceneCode: 'T06',
@ -22,5 +23,5 @@ export const option = {
export default class Config extends PublicConfigClass implements CreateComponentType { export default class Config extends PublicConfigClass implements CreateComponentType {
public key = SceneDistributionConfig.key public key = SceneDistributionConfig.key
public chartConfig = SceneDistributionConfig public chartConfig = SceneDistributionConfig
public option = option public option = cloneDeep(option)
} }

View File

@ -221,7 +221,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, computed, onMounted } from 'vue' import { PropType, computed, onMounted, ref, watch } from 'vue'
import { option as configOption } from './config' import { option as configOption } from './config'
import axios from 'axios' import axios from 'axios'
import { SceneDistributionConfig } from './index' import { SceneDistributionConfig } from './index'
@ -241,26 +241,68 @@ const getImageUrl = (name: string) => {
const key = SceneDistributionConfig.key; const key = SceneDistributionConfig.key;
//
const getStaticData = async (key: string, componentIndexKey: string, sceneCode: string) => { const getStaticData = async (key: string, componentIndexKey: string, sceneCode: string) => {
let dataTemp = option.value.dataset
try { try {
const response = await axios.get('/staticData/static.json'); const response = await axios.get('/staticData/static.json');
if (response.data) { if (response.data) {
console.log('静态数据:', response.data); console.log('静态数据:', response.data);
dataTemp = response.data[sceneCode]?.[key]?.[componentIndexKey]?.['source']; const dataTemp = response.data[sceneCode]?.[key]?.[componentIndexKey]?.['source'];
if (!dataTemp) { if (!dataTemp) {
console.warn(`Data not found for sceneCode: ${sceneCode}, key: ${key}, componentIndexKey: ${componentIndexKey}`); console.warn(`Data not found for sceneCode: ${sceneCode}, key: ${key}, componentIndexKey: ${componentIndexKey}`);
return props.chartConfig.option.dataset; //
} }
return dataTemp;
} }
console.log("datatemp:", dataTemp)
} catch (err) { } catch (err) {
console.error('获取static.json失败:', err); console.error('获取static.json失败:', err);
} }
return dataTemp return props.chartConfig.option.dataset; //
} }
const isFetching = ref(false) //
//
const fetchData = async () => {
//
if (isFetching.value) return;
isFetching.value = true;
try {
let data;
//
data = await getStaticData(key, option.value.componentIndexKey, option.value.sceneCode);
props.chartConfig.option.dataset = data;
if (JSON.stringify(data) !== JSON.stringify(option.value.dataset)) {
console.log('更新数据:', data);
}
} finally {
isFetching.value = false;
}
}
//
watch(
() => [
props.chartConfig.option.sceneCode,
props.chartConfig.option.componentIndexKey,
],
async () => {
await fetchData();
},
{
immediate: true,
deep: true
}
)
//
onMounted(async () => { onMounted(async () => {
option.value.dataset = await getStaticData(key, option.value.componentIndexKey, option.value.sceneCode); await fetchData();
}) })
</script> </script>