common.js
4.8 KB
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
/**
* 公共方法
* author:jack
* date:2018-07-03
* */
import axios from 'axios';
const common = {
/**
* 导出excel
* */
exportExcel: function(options) {
if (!options) {
return false;
}
let oParams = {
method: options.method || 'post',
url: options.url,
responseType: 'blob'
}
if (options.method == 'GET' || options.method == 'get') {
oParams.params = options.data
} else {
oParams.data = options.data;
}
console.log(options.data);
axios(oParams).then(response => {
if (!response) {
return
};
console.log(response)
let a = document.createElement('a')
let blob = new Blob([response.data], { type: "application/vnd.ms-excel" })
let objectUrl = URL.createObjectURL(blob)
a.setAttribute("href", objectUrl)
a.setAttribute("download", '订单信息.xls')
a.click()
// let url = window.URL.createObjectURL(new Blob([response.data], {
// type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
// }));
// let link = document.createElement('a');
// link.style.display = 'none';
// link.href = url;
// link.setAttribute('download', options.fileName || 'excel.xlsx');
// document.body.appendChild(link);
// link.click();
if (options.success) {
(options.success)();
}
}).catch((error) => {
if (options.errorFunc) {
(options.errorFunc)(error);
}
})
},
/**
* 下载zip
*/
downPackFile: function(options) {
if (!options) {
return false;
};
/* let iframe = document.createElement('a')
iframe.style.display = 'none'
iframe.src = options.url
document.body.appendChild(link);
link.click(); */
window.location.href = options.url;
//document.body.appendChild(iframe)
},
/**
* 获取时间对象
*/
getDateInfo: function() {
var o = new Date();
var y = o.getFullYear();
var m = o.getMonth() + 1;
var d = o.getDate();
var M = m;
var D = d;
if (m < 10) {
M = '0' + m;
};
if (d < 10) {
D = '0' + d;
};
return {
y: y,
m: m,
M: M,
d: d,
D: D,
today: y + '-' + M + '-' + D
};
},
/**
* 判断按钮是否有权限
*/
hasPermission: function(id) {
if (!id) {
return false
};
let res = sessionStorage.getItem('MENURESOUCE');
let mres = '';
let resObj = {};
if (res) {
mres = JSON.parse(res);
};
if (mres && mres.length > 0) {
for (let i = 0, len = mres.length; i < len; i++) {
let child = mres[i].children;
resObj[mres[i].menuId] = mres[i];
let abc = Common.processData(child);
Object.assign(resObj, abc);
}
};
if (resObj[id]) {
return true;
} else {
return false;
}
},
/**
* 递归处理数据
*/
processData: function(o) {
let list = {};
if (o && o.length > 0) {
for (let i = 0, len = o.length; i < len; i++) {
let child = o[i].children;
list[o[i].menuId] = o[i];
if (child && child.length > 0) {
let mn = Common.processData(child);
Object.assign(list, mn);
}
}
}
return list;
},
/**
* 递归获取子节点数据
*/
processChildData: function(oInfo) {
let resObj = {};
if (!oInfo) {
return false;
}
if (oInfo && oInfo.length > 0) {
for (let i = 0, len = oInfo.length; i < len; i++) {
let child = oInfo[i].children;
resObj[oInfo[i].menuId] = oInfo[i];
let abc = Common.processData(child);
Object.assign(resObj, abc);
}
};
return resObj;
},
/**
* 数字转换成货币格式
*/
formatMoney: function(number, places, symbol, thousand, decimal) {
number = number || 0;
places = !isNaN(places = Math.abs(places)) ? places : 2;
symbol = symbol !== undefined ? symbol : "$";
thousand = thousand || ",";
decimal = decimal || ".";
var negative = number < 0 ? "-" : "",
i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" +
thousand) + (places ? '' : "");
},
/**
* 字符串截取
*/
/**
* 截取字符串
*/
subStrByLength: function(str, length) {
var retStr = str;
var len = 3;
if (retStr) {
if (length) {
len = length;
};
if (str.length > len) {
retStr = str.substring(0, len) + '...';
}
};
return retStr;
}
};
export const exportExcel = common.exportExcel;
export const subStrByLength = common.subStrByLength;
export const formatMoney = common.formatMoney;