whychdw
2019-07-02 d2f48914142e8e40f432aebb987529f08a22c407
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import $ from 'jquery';
const CORS= 'http://localhost:8080/sensor/';        // 开启跨域请求(调试时开启)
//const CORS='';           // 关闭跨域请求(发布时开启)
//定义计时器
function Interval() {
    this.timer = null;
    this.time = '';
    this.callback = '';
}
// 开启计时器并添加
Interval.prototype.start = function(callback, time) {
    // 先关闭计时器
    this.stop();
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        callback();
        this.timer = setInterval(callback, time);
    }else {
        console.warn('未完整配置参数!');
    }
};
// 开启计时器
Interval.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time);
};
 
// 关闭计时器
Interval.prototype.stop = function() {
    clearInterval(this.timer);
};
 
 
// 延时计时器
function Timeout() {
    this.timer = null;
    this.time = '';
    this.callback = '';
}
// 开启计时器并添加
Timeout.prototype.start = function(callback, time, exe) {
    // 先关闭计时器
    this.stop();
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        if(exe != 'exe') {
            callback();
        }
        this.timer = setTimeout(callback, time);
    }else {
        console.warn('未完整配置参数!');
    }
};
// 开启计时器
Timeout.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time, 'exe');
};
 
// 关闭计时器
Timeout.prototype.stop = function() {
    clearTimeout(this.timer);
};
 
/*从多维数组中获取最大值*/
function getMaxFromArr(arr) {
    var newArray = arr.join(",").split(",");
    return Math.max.apply({},newArray);
}
 
/*从多维数组中获取最小值*/
function getMinFromArr (arr) {
    var newArray = arr.join(",").split(",");
    return Math.min.apply({},newArray);
}
 
function ajax(option) {
    $.ajax({
        type: 'post',
        async: true,
        url: CORS+option.url,
        data: option.data,
        dataType: 'json',
        success: option.success,
        error: option.error,
        complete: option.complete
    });
}
 
// 格式化时间
Date.prototype.format =function(format)
{
    var o = {
    "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
 
// 根据对象获取url
function getUrlStr(props) {
    var str = "";
    Object.keys(props).forEach(function(key) {
        str+=props[key]+"/";
    });
    return str;
}
 
// 根据验证规则校验对象的属性值
function checkObjIsGood(obj, verify, callback) {
    var result = {
        code: 1,
        name: '',
        msg: '',
        data: obj
    };
    Object.keys(verify).forEach(function(key) {
        // 上一笔数据是否有效
        if(result.code && obj[key] != undefined) {
            var valueStatus = checkValueIsGood(obj[key], verify[key]);
            result.code = valueStatus.code;
            result.name = valueStatus.name;
            result.msg = valueStatus.msg;
        }
    });
    
    // 如果code=1设置msg的值
    if(result.code) {
        result.name = '';
        result.msg = '数据匹配成功';
    }
    
    // 执行callback函数
    if(typeof callback == 'function') {
        callback(result);
    }
}
 
// 检测当前数据是否符合要求
function checkValueIsGood(val, option) {
    var result = {
        code: 0,
        name: option.name,
        msg: option.msg
    };
    // 校验数据是否符合匹配规则
    if(option.pattern.test(val)) {
        // 数据存在取值范围的校验
        if(option.regVal) {
            if(val>=option.min && val<=option.max) {
                result.code =1;
            }
        }else {
            result.code =1;
        }
    }
    // 返回结果集
    return result;
}
 
export{
    CORS,           //  跨域请求内容
    Interval,       // 计时器
    Timeout,        // 延时计时器
    getMaxFromArr,  // 获取数组中的最大值
    getMinFromArr,   // 获取数组中的最小值
    ajax,            // 请求
    checkObjIsGood, // 验证对象数据
    getUrlStr       // 根据对象获取url
};