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
import moment from 'moment';
import storeInfo from '../../store/index'
import router from "../../router";
import jsonp from 'jsonp';
import Veu from 'vue'
//对象、一维数组去重(返回一个新数组)
export const singleArr = (arr, val) => {
let newArr = arr.concat()
let len = newArr.length;
if (val) {
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (newArr[i][val] == newArr[j][val]) {
newArr.splice(j, 1);
len--;
j--;
}
}
}
} else {
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (newArr[i] == newArr[j]) {
newArr.splice(j, 1);
len--;
j--;
}
}
}
}
return newArr;
}
//删除关闭详情的页面
export const closedDetail = (url, goToUrl) => {
if (goToUrl) {
window.sessionStorage.setItem('activeItem', goToUrl)
window.sessionStorage.setItem('activeKey', goToUrl)
storeInfo.commit('changeActKey', goToUrl)
router.push(goToUrl)
}
}
//判断对象values是否为空 并返回不为空对象
export const isEmptyParams = (obj) => {
let obje = {}
Object.assign(obje, obj)
for (let key in obje) {
if (obje[key] === '' || typeof obje[key] === 'undefined' || obje[key] === null || obje[key].length == 0) {
delete obje[key]
}
}
return obje
}
//数组转为树形结构
// toTree(数组,父id,父id属性key, id当前节点id key)
export const toTree = (list, parId, pId, id) => {
let len = list.length
function loop(parId) {
let res = [];
for (let i = 0; i < len; i++) {
let item = list[i]
if (item[pId] == parId) {
item.children = loop(item[id])
res.push(item)
}
}
return res
}
return loop(parId)
}
//树形数据转数组
export const treeToArray = (source) => {
let res = []
const fn = (source) => {
source.forEach(el => {
res.push(el)
el.children && el.children.length > 0 ? fn(el.children) : ""
})
}
return res
}
//删除树状结构数据中空children
export const removeEmptyChildren = (data) => {
data.forEach(item => {
if (item.children === null || item.children.length === 0) {
delete item.children
} else {
removeEmptyChildren(item.children)
}
})
return data
}
//修改时间格式
export const timeFormat = (time, format = 'YYYY-MM-DD') => {
let atime = moment(time).format(format)
return atime
}
/**
* 非空判断 已挂载到原型上$isNot
* 解决Vue Template模板中无法使用可选链的问题
* eg:
* let ces = {
data: {
data1: {
name: '测试'
}
}
}
let b = this.$isNot(ces, 'data', 'data1', 'name') //测试
{{$isNot(ces, 'data', 'data1', 'name')}} //测试
*/
// export const optionalChaining = (obj, ...rest) => {
// let tmp = obj;
// for (let key in rest) {
// let name = rest[key];
// tmp = tmp?.[name]; //es11可选链
// }
// return tmp ?? "未知";
// };
// 校验手机号
export const checkPhone = (rule, value, callback) => {
if (value == '' || value == undefined) {
callback();
}
let ckPhone = /^1(?:3[0-9]|4[5-9]|5[0-9]|6[12456]|7[0-8]|8[0-9]|9[0-9])[0-9]{8}$/;
let tell = /^0\d{2,3}-?\d{7,8}$/;
if (ckPhone.test(value) || tell.test(value)) {
callback()
} else {
callback(new Error('请输入正确的手机号或座机'));
}
}
//根据枚举标识获取下拉数据
export const getEnumByFlag = (enumFlag) => {
let result = []
let enumSessionStorange = JSON.parse(window.sessionStorage.getItem('allEnum'))
if (enumSessionStorange && enumFlag) {
for (let obj in enumSessionStorange) {
if (obj == enumFlag) {
result = enumSessionStorange[obj]
}
}
}
return result
}
//读取cookie
export const getCookie = name => {
let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
};
//判断一对象是否是空白
export const isBlank = obj => {
if (obj == null || obj == undefined || obj == '' || obj == ' ') {
return true;
}
if (obj === null || obj === undefined || obj === '' || obj === ' ') {
return true;
}
if (obj == {} || obj == []) {
return true
}
return false;
};
export const isNotBlank = obj => {
return !isBlank(obj)
}
/**
* 根据身份证读卡器读取基本信息
*/
export const GetUserInfoByCardDevice = () => {
let {protocol} = window.location;
let url = protocol === "https:" ? "https://localhost:9199/api/ReadMsg" : "http://localhost:8989/api/ReadMsg";
return new Promise((resolve) => {
// res {code: "-1", retcode: "0x41", retmsg: "读居民身份证操作失败", errmsg: ""}
jsonp(url, {timeout: 30000}, (err, data) => {
if (!err) {
if (data.cardno) {
resolve(data);
} else {
let reg = /\d+/g;
let messageInfo = data.message;
let datault = reg.exec(messageInfo);
let status = datault && datault[0];
if (/^[45]\d+/.test(status)) {
window.parent.parentLibInfo.Antd.message.error('设备连接失败');
} else {
window.parent.parentLibInfo.Antd.message.error(data.retmsg);
}
}
} else if (err && err.message === 'Timeout') {
window.parent.parentLibInfo.Antd.message.error('设备连接失败');
}
});
})
};
/**
* 区域编码转换为控件需要的格式
* @param areaCode
* @returns {string[]}
*/
export function areCodeTrans(areaCode) {
let areaCodeList = []
if (!areaCode) {
return areaCodeList;
}
let fullZero = process.env.VUE_APP_AREA_CODE_FULL_ZERO;
if (fullZero == 'true') {
//末尾填充0实现
return areCodeTrans2(areaCode);
}
//末尾不填充0实现
if (areaCode.length >= 2) {
areaCodeList.push(areaCode.substring(0, 2))
}
if (areaCode.length >= 4) {
areaCodeList.push(areaCode.substring(0, 4))
}
if (areaCode.length >= 6) {
areaCodeList.push(areaCode.substring(0, 6))
}
if (areaCode.length >= 9) {
areaCodeList.push(areaCode.substring(0, 9))
}
if (areaCode.length >= 12) {
areaCodeList.push(areaCode)
}
return areaCodeList
}
//
function areCodeTrans2(areaCode) {
//let areaCode = "140311103204";
let areaCodeList = []
if (!areaCode) {
return areaCodeList;
}
areaCodeList.push(areaCode.substring(0, 2))
if (areaCode.length === 2) {
return areaCodeList;
}
areaCodeList.push(areaCode.substring(0, 4) + "00000000")
if (areaCode.substring(4, 6) == "00") {
return areaCodeList
}
areaCodeList.push(areaCode.substring(0, 6) + "000000")
if (areaCode.substring(6, 9) == "000") {
return areaCodeList
}
areaCodeList.push(areaCode.substring(0, 9) + "000")
if (areaCode.substring(9, 12) == "000") {
return areaCodeList
}
areaCodeList.push(areaCode)
return areaCodeList
}