111
jihongshun
8 天以前 0c741cdda7ef9935a20d3090dfea97e1ce8ae754
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
import httpUtil from "@/utils/HttpUtil";
import {Message} from "element-ui";
import {executeSelectApi} from "@/api/sqlExecuteApi";
 
export async function getDataJson(cptDataForm){
    let resStr = '{}';//防止JSON解析报错
    let iptStr = cptDataForm.dataText;
    if (cptDataForm.dataSource === 1){
        resStr = JSON.parse(iptStr);
    }else if(cptDataForm.dataSource === 2){//调接口
        iptStr = cptDataForm.apiUrl
        if (iptStr){
            await httpUtil.doRequest(iptStr,'get').then(res => {
                resStr = res.data?res.data:res;
            })
        }else{
            Message.error("接口地址不能为空");
        }
    }else if(cptDataForm.dataSource === 3){
        iptStr = cptDataForm.sql
        if (iptStr){
            let staticData = JSON.parse(cptDataForm.dataText);
            const isArray = Array.isArray(staticData);
            await executeSelectApi({sql:iptStr, isArray}).then(res => {
                resStr = res.data;
            })
        }else{
            Message.error("SQL不能为空");
        }
    }
    return resStr;
}
 
let cptTimer = {}
 
export function pollingRefresh(uuid, cptDataForm, loadData){
    if(uuid) {
        clearInterval(cptTimer[uuid]);//清除旧的定时器
    }
    if (!cptDataForm){
        Message.warning("cptDataForm==>null");
        return;
    }
    if (!loadData){
        Message.warning("子组件未实现数据解析方法");
        return;
    }
    loadData();
    if (cptDataForm.pollTime && cptDataForm.pollTime !== 0) {//轮询
        cptTimer[uuid] = setInterval(function () {
            loadData();
        }, cptDataForm.pollTime * 1000)
    }
}
 
export function clearCptInterval(uuid, clearAll){
    if(uuid) {
        clearInterval(cptTimer[uuid]);//清除旧的定时器
    }
    if (clearAll){
        for (const key in cptTimer) {
            clearInterval(cptTimer[key]);
        }
    }
}