-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompanyCommonTask.js
329 lines (292 loc) · 9.99 KB
/
CompanyCommonTask.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Author:
* CreateAt: 2017-10-23
* LastModify: 2017-12-11
* Goal: 公司常用功能封装
* 1. eventHelper, 事件辅助方法
* 2. ajaxHelper, Ajax辅助方法
* 3. dateHelper, 时间辅助方法
* 4. ligerHelper, ligerUI辅助方法
* 5. financialHelper, 与财务相关的辅助方法
* 6. strategies, 验证相关的方法
* 7. testHelper, 测试相关
* 8. mathHelper, 数学运算
* 9. ioHelper, 导入和导出
* 10. image, 与image相关的常用操作
* 11. pattern, 与pattern有相关的操作
* 12. aHelper, 与数组相关的辅助方法
*/
var eventHelper = (function () {
// 对指定的元素绑定回车事件触发另一指定元素的click事件
// 通常对页面绑定回车触发搜索按钮
var pageBindEnter = function (dom, bindElement) {
$(dom).keydown(function () {
if (event.keyCode == "13") {//keyCode=13是回车键
$(bindElement).click();
}
});
};
return {
pageBindEnter: pageBindEnter
}
})();
var ajaxHelper = (function () {
// 根据传入的url和key使用Ajax获取单个值并返回
var getSingleValue = function (url, key, info) {
var result = '';
$.ajax({
type: "post",
url: url,
async: false, //是否异步
data: info === undefined ? null : info,
success: function (data) {
var resultObj = $.parseJSON(data);
result = resultObj.Rows[0][key];
}
});
return result;
};
// 根据传入的url和key使用Ajax获取值并填充到知道id
var ajaxInsertSingleValue = function (url, key, tag, info, callback) {
$.ajax({
type: "post",
url: url,
data: info === undefined ? null : info,
success: function (data) {
var resultObj = $.parseJSON(data);
$("#" + tag).text(resultObj.Rows[0][key]);
if (callback) {
callback(resultObj.Rows[0][key]);
}
}
});
};
return {
getSingleValue: getSingleValue,
ajaxInsertSingleValue: ajaxInsertSingleValue
};
})();
var dateHelper = (function () {
var getDateObj, getDateFromString, getDateTime, getCurrentDate;
// 根据传入的dateTime String(例如: 2017/08/31 11:59:59)返回date(例如: 2017-08-31)
getDateObj = function (dateStr) {
if (null == dateStr || undefined == dateStr) {
return null;
}
// dateStr format, see more: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date
// Todo: add support like '/Date(1328423451489)/, see one way of implement: line 7296 of ligerui.all.js
return new Date(dateStr);
};
getDateFromString = function (dateStr) {
var date = getDateObj(dateStr);
return date ? null: date.getFullYear() + '-' + (date.getMonth() + 1)
+ '-' + date.getDate();
};
// 根据传入的dateTime String(例如: 2017/08/31 11:59:59)返回date(例如: 2017-08-31 11:59:59)
getDateTime = function (dateStr) {
var date = getDateObj(dateStr);
return date ? null: date.getFullYear() + '-'
+ (date.getMonth() + 1) + '-'
+ date.getDate() + ' '
+ date.getHours() + ':'
+ date.getMinutes() + ':'
+ date.getSeconds();
};
// 返回当前时间
getCurrentDate = function () {
var date1 = new Date();
return date1.getFullYear() + '-' + (date1.getMonth()+1) + '-' + date1.getDate() + ' '
+ date1.getHours() + ':' + date1.getMinutes() + ':' + date1.getSeconds();
};
return {
getDateFromString: getDateFromString,
getDateTime: getDateTime,
getCurrentDate: getCurrentDate
};
})();
// 与ligerUI相关的辅助方法
var ligerHelper = (function () {
var getSingleFilter, getMultiFilter;
// 返回ligerUI的单个条件限制
getSingleFilter = function (key, value, op) {
return {
op: 'AND',
rules: [
{ field: key, value: value, op: op }
]
};
};
// 返回ligerUI的多个条件限制
// getMultiFilter([{key:"", value:"", op:""}...])
getMultiFilter = function(info) {
var rule = [];
for (var i=0; i<info.length; i++) {
rule.push({
field: info[i].key,
value: info[i].value,
op: info[0].op });
}
return {
op: 'AND',
rules: rule
};
};
return {
getSingleFilter: getSingleFilter,
getMultiFilter: getMultiFilter
};
})();
// 与财务相关的辅助方法
var financialHelper = (function () {
var convertCurrency;
convertCurrency = function (money) {
var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'), //汉字的数字
cnIntRadice = new Array('', '拾', '佰', '仟'), //基本单位
cnIntUnits = new Array('', '万', '亿', '兆'), //对应整数部分扩展单位
cnDecUnits = new Array('角', '分', '毫', '厘'), //对应小数部分单位
cnInteger = '整', //整数金额时后面跟的字符
cnIntLast = '元', //整型完以后的单位
maxNum = 999999999999999.9999, //最大处理的数字
integerNum, //金额整数部分
decimalNum, //金额小数部分
chineseStr = '', //输出的中文金额字符串
parts; //分离金额后用的数组,预定义
if (money == '') { return ''; }
money = parseFloat(money);
if (money >= maxNum) {
//超出最大处理数字
return '';
}
if (money == 0) {
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
//转换为字符串
money = money.toString();
if (money.indexOf('.') == -1) {
integerNum = money;
decimalNum = '';
} else {
parts = money.split('.');
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
//获取整型部分转换
if (parseInt(integerNum, 10) > 0) {
var zeroCount = 0;
var IntLen = integerNum.length;
for (var i = 0; i < IntLen; i++) {
var n = integerNum.substr(i, 1);
var p = IntLen - i - 1;
var q = p / 4;
var m = p % 4;
if (n == '0') {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
//归零
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m == 0 && zeroCount < 4) {
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
//小数部分
if (decimalNum != '') {
var decLen = decimalNum.length;
for (var i = 0; i < decLen; i++) {
var n = decimalNum.substr(i, 1);
if (n != '0') {
chineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if (chineseStr == '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimalNum == '') {
chineseStr += cnInteger;
}
return chineseStr;
}
return {
convertCurrency: convertCurrency
}
})();
// 验证相关的方法
var strategies = {
isNumber: function (value) {
return /^[0-9\.]+$/.test(value);
}
};
// assert
var testHelper = {
assert: function (value, msg) {
if (!value) {
throw(msg || (value + " does not equal true"));
}
},
assertEqual: function (val1, val2, msg) {
if (val1 !== val2) {
throw(msg || (val1 + " does not equal " + val2));
}
}
};
var mathHelper = {
guid : function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
}).toUpperCase();
}
};
// 处理涉及IO的操作
var ioHelper = (function() {
var excelExport;
// 导出Excel
excelExport = function(rule, url, spName) {
var form = $("<form>"); //定义一个form表单
form.attr("style", "display:none");
form.attr("id", "form1");
form.attr("target", "");
form.attr("method", "post");
form.attr("action", url);
var input1 = $("<input>");
input1.attr("type", "hidden");
input1.attr("name", "view");
input1.attr("value", spName);
var input2 = $("<input>");
input2.attr("type", "hidden");
input2.attr("name", "where");
input2.attr("value", JSON2.stringify(rule));
$("body").append(form); //将表单放置在web中
form.append(input1);
form.append(input2);
form.submit(); //表单提交
}
return {
excelExport: excelExport
};
}) ();
var image = (function() {
var imageView;
// 弹出图片浏览框
imageView = function(obj) {
var url = $(obj).attr("url");
$.ligerDialog.open({
title: "查看单据图片",
target: "<div><img src='" + url + "' style='width:450px;height:450px;'/></div>",
height: 500,
width: 500
});
}
return {
imageView: imageView
}
})();
var aHelper = (function() {
})();