-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
291 lines (269 loc) · 6.94 KB
/
Copy pathutils.js
File metadata and controls
291 lines (269 loc) · 6.94 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
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
/**
* Created by yaer on 2018/7/6;
* @Email 740905172@qq.com
**/
/**
* 获取系统
* @returns {*}
*/
export function getSys() {
let sys;
let u = navigator.userAgent;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isiOS) {
sys = "ios";
} else {
sys = "android";
}
console.log("当前手机是" + sys + "操作系统");
return sys;
}
/**
* 获取url的params
* @param url url地址
* @returns {Object}
*/
export function GetRequest(url) {
let theRequest = {};
let index = url.indexOf("?");
if (index !== -1) {
let str = url.substr(index+1);
let strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
const [key,val] = strs[i].split("=");
theRequest[key] = val;
}
}
return theRequest;
}
/**
* 获取url的参数(用于key有特殊字符的情况,参数传递特殊字符怎么想的??)
* @param url 链接
* @returns List<Map<String,String>>
*/
export function getParams(url) {
let paramsArr = url.split('?')[1].split('&');
let arr = [];
paramsArr.forEach((item) => {
let itemArr = item.split('=');
arr.push({
[decodeURI(itemArr[0])]: itemArr[1]
}
);
})
;
return arr;
}
/**
* fixed定位解决遮盖元素问题
* @param isAppendChild 是否需要在当前节点父级下 不判断是否最后一个元素跟当前元素相同 就直接创建节点
* @param cloneDom fixed定位的dom节点
* @param company css单位 px rem
* @returns {*}
*/
export function fixedClone(isAppendChild, cloneDom, company = 'rem') {
// 获取需要克隆节点的宽高
let height = cloneDom.offsetHeight;
let width = cloneDom.offsetWidth;
// 生成dom并且获取html的fontSize
let dom = document.createElement("div");
let htmlFontSize = Number(window.document.documentElement.style.fontSize.split('px')[0]);
//dom赋值
if (company === 'rem') {
dom.style.height = height / htmlFontSize + 'rem';
dom.style.width = width / htmlFontSize + 'rem';
} else {
dom.style.height = height + 'px';
dom.style.width = width + 'px';
}
// 插入dom 如果cloneDom的最后一个节点是cloneDom 那么就直接在最后插入dom 否则就在clone下一个节点前插入dom
let parent = cloneDom.parentNode;
if (parent.lastChild === cloneDom && isAppendChild) {
parent.appendChild(dom);
} else {
parent.insertBefore(dom, cloneDom.nextSibling)
}
}
/**
* 时间转换
* @param date 毫秒值的时间
* @param format 转换格式
* @returns {string}
*/
export function setDate(date, format) {
let time = new Date(date);
let val = '';
switch (format) {
case 'yy-mm':
val = `${time.getFullYear()}年${time.getMonth() + 1}月`;
break;
case 'yy-mm-dd':
val = `${time.getFullYear()}年${time.getMonth() + 1}月${zero(time.getDate())}日`;
break;
case 'yy-mm-dd-hh-ii':
val = `${time.getFullYear()}-${time.getMonth() + 1}-${zero(time.getDate())} ${zero(time.getHours())}:${zero(time.getMinutes())}`;
break;
case 'hh-ii':
val = `${zero(time.getHours())}:${zero(time.getMinutes())}`;
break;
case 'hh-ii-ss':
val = `${zero(time.getHours())}:${zero(time.getMinutes())}:${zero(time.getSeconds())}`;
break;
}
return val
}
/**
* 时间倒计时
* @param mss 毫秒时间
* @returns {string}
*/
export function formatDuring(mss) {
let hours = zero(parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)));
let minutes = zero(parseInt((mss % (1000 * 60 * 60)) / (1000 * 60)));
let seconds = ((zero((mss % (1000 * 60)) / 1000)) + '').split('.')[0];
return `${hours}:${minutes}:${seconds}`
}
/**
* 补0
* @param num Number数字
* @returns {*}
*/
function zero(num) {
if (num < 10) {
return '0' + num
} else {
return num
}
}
/**
* 唤起APP
* @param jumpLink 唤起APP的链接
* @param download 唤起不成功的下载链接
* @returns {*}
*/
export function AppJump(jumpLink, download) {
Indicator.open({
text: '跳转中...',
spinnerType: 'fading-circle'
});
//判断浏览器
let u = navigator.userAgent;
if (/MicroMessenger/gi.test(u)) {
// 引导用户在浏览器中打开
alert('请在浏览器中打开');
return false;
}
let d = new Date();
let t0 = d.getTime();
openApp(jumpLink);
let delay = setInterval(() => {
let d = new Date();
let t1 = d.getTime();
if (t1 - t0 < 3000 && t1 - t0 > 2000) {
// 这里跳转下载页
Indicator.close();
window.location.href = download;
}
if (t1 - t0 >= 3000) {
clearInterval(delay);
}
},
1000
)
;
function openApp(src) {
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
if (getSys() === "ios") {
window.location.href = src;
} else {
let ifr = document.createElement('iframe');
ifr.src = src;
ifr.style.display = 'none';
document.body.appendChild(ifr);
window.setTimeout(() => {
document.body.removeChild(ifr);
},
2000
)
;
}
}
}
/**
* 当路由变化的时候需要处理的事件
* @param callback 路由变化执行的回调事件
*/
export function goBackConfirm(callback) {
let href = window.location.href;
location.hash = 'last';
history.pushState({back: 1}, null, href);
// 监听路由变化
onhashchange = () =
>
{
if (location.hash.indexOf('#last') < 0) return;
callback && callback()
}
;
}
/**
* 获取cookie
* @returns {{}}
*/
export function getCookie() {
let obj = {};
let cookieArr = document.cookie.split(';');
cookieArr.forEach((item) => {
let itemArr = item.replace(/\s+/g, "").split('=');
obj[itemArr[0]] = itemArr[1];
})
;
return obj;
}
/**
* 把字符串改为金额的格式
* @param num 金额 (Number)
* @param decimal
* @returns {string}xx,xxx,xxx,xxx.xx
*/
export function moneyFormat(num, decimal) {
if (typeof num === "number") {
num = String(num.toFixed(decimal || 2));
let re = /(-?\d+)(\d{3})/;
while (re.test(num)) num = num.replace(re, "$1,$2");
return num;
}
return num;
}
/**
* 时间转换
* @param x new Date()对象
* @param y 格式 yy-M-d h:m:s (可拆开写)
* @returns {string | void} 时间字符串
*/
export function dateFormat(x, y) {
let z = {
y: x.getFullYear(),
M: x.getMonth() + 1,
d: x.getDate(),
h: x.getHours(),
m: x.getMinutes(),
s: x.getSeconds()
};
return y.replace(/(y+|M+|d+|h+|m+|s+)/g, function (v) {
return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-(v.length > 2 ? v.length : 2))
}
);
}
/**
* 手机号格式化 3 4 4
* @param phone {String} 手机号
* @returns {string | void | *}
*/
export function formatPhone(phone) {
if(typeof phone === 'number'){
phone = phone.toString();
}
return phone.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3')
}