提交 | 用户 | age
|
3ac5f2
|
1 |
/* |
J |
2 |
* @Author: 丘骏_jun |
|
3 |
* @Date: 2019-11-21 10:59:17 |
|
4 |
* @Last Modified by: 丘骏_jun |
|
5 |
* @Last Modified time: 2019-12-30 16:29:45 |
|
6 |
*/ |
|
7 |
// 通用function,通过全局安装,或import引用调用 |
|
8 |
|
bb2b08
|
9 |
var fn_obj = {}// fn.js对象 |
J |
10 |
|
3ac5f2
|
11 |
let scrollBarWidth // 滚动条宽度 |
J |
12 |
// 获取浏览器滚动条宽度 |
|
13 |
function getScrollBarWidth() { |
|
14 |
if (scrollBarWidth !== undefined) return scrollBarWidth |
|
15 |
|
|
16 |
const outer = document.createElement('div') |
|
17 |
outer.className = 'el-scrollbar__wrap' |
|
18 |
outer.style.visibility = 'hidden' |
|
19 |
outer.style.width = '100px' |
|
20 |
outer.style.position = 'absolute' |
|
21 |
outer.style.top = '-9999px' |
|
22 |
document.body.appendChild(outer) |
|
23 |
|
|
24 |
const widthNoScroll = outer.offsetWidth |
|
25 |
outer.style.overflow = 'scroll' |
|
26 |
|
|
27 |
const inner = document.createElement('div') |
|
28 |
inner.style.width = '100%' |
|
29 |
outer.appendChild(inner) |
|
30 |
|
|
31 |
const widthWithScroll = inner.offsetWidth |
|
32 |
outer.parentNode.removeChild(outer) |
|
33 |
scrollBarWidth = widthNoScroll - widthWithScroll |
|
34 |
|
|
35 |
return scrollBarWidth |
|
36 |
} |
|
37 |
// 获取最近样式 |
|
38 |
// function getCurrentStyle (obj, prop) { |
|
39 |
// if (obj.currentStyle) { |
|
40 |
// return obj.currentStyle[prop]; |
|
41 |
// } else if (window.getComputedStyle) { |
|
42 |
// prop = prop.replace(/([A-Z])/g, "-$1"); |
|
43 |
// prop = prop.toLowerCase(); |
|
44 |
// return document.defaultView.getComputedStyle(obj, null)[prop]; |
|
45 |
// } |
|
46 |
// return null; |
|
47 |
// } |
|
48 |
// // 判断是否有滚动条 |
|
49 |
// function hasScrollBar (obj) { |
|
50 |
// return getCurrentStyle(obj, 'overflow') == 'hidden' |
|
51 |
// ? 0 |
|
52 |
// : document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight); |
|
53 |
// } |
|
54 |
|
|
55 |
// 日期格式化 |
|
56 |
// eslint-disable-next-line no-extend-native |
|
57 |
Date.prototype.format = function(format) { |
|
58 |
var o = { |
|
59 |
'M+': this.getMonth() + 1, // month |
|
60 |
'd+': this.getDate(), // day |
|
61 |
'H+': this.getHours(), // hour |
|
62 |
'm+': this.getMinutes(), // minute |
|
63 |
's+': this.getSeconds(), // second |
|
64 |
'q+': Math.floor((this.getMonth() + 3) / 3), // quarter |
|
65 |
'S': this.getMilliseconds() // millisecond |
|
66 |
} |
|
67 |
|
|
68 |
if (/(y+)/.test(format)) { |
|
69 |
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)) |
|
70 |
} |
|
71 |
|
|
72 |
for (var k in o) { |
|
73 |
if (new RegExp('(' + k + ')').test(format)) { |
|
74 |
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)) |
|
75 |
} |
|
76 |
} |
|
77 |
return format |
|
78 |
} |
|
79 |
|
|
80 |
// 消除字符串多余空格 |
|
81 |
if (!String.prototype.trim) { |
|
82 |
// eslint-disable-next-line no-extend-native |
|
83 |
String.prototype.trim = function() { |
|
84 |
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '') |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* 获取本地缓存 localStorage |
|
90 |
* @param {string} key storage 标记 |
|
91 |
* @return {*} 值 |
|
92 |
*/ |
|
93 |
function getLocalStorage(key) { |
|
94 |
if (!key) { |
|
95 |
return |
|
96 |
} |
|
97 |
var result = localStorage.getItem(key) || '' |
|
98 |
try { |
|
99 |
result = JSON.parse(result) |
|
100 |
// eslint-disable-next-line no-empty |
|
101 |
} catch (e) {} |
|
102 |
return result |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* 获取本地缓存 sessionStorage |
|
107 |
* @param {string} key storage 标记 |
|
108 |
* @return {*} 值 |
|
109 |
*/ |
|
110 |
function getSessionStorage(key) { |
|
111 |
var result = sessionStorage.getItem(key) || '' |
|
112 |
try { |
|
113 |
result = JSON.parse(result) |
|
114 |
// eslint-disable-next-line no-empty |
|
115 |
} catch (e) {} |
|
116 |
return result |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* 保存本地缓存 localStorage |
|
121 |
* @param {string} key storage 标记 |
|
122 |
* @param {*} value 值 |
|
123 |
*/ |
|
124 |
function setLocalStorage(key, value) { |
|
125 |
if (typeof value === 'object') { |
|
126 |
value = JSON.stringify(value) |
|
127 |
} |
|
128 |
localStorage.setItem(key, value) |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* 保存本地缓存 sessionStorage |
|
133 |
* @param {string} key storage 标记 |
|
134 |
* @param {*} value 值 |
|
135 |
*/ |
|
136 |
function setSessionStorage(key, value) { |
|
137 |
if (typeof value === 'object') { |
|
138 |
value = JSON.stringify(value) |
|
139 |
} |
|
140 |
sessionStorage.setItem(key, value) |
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* 移除本地缓存 localStorage |
|
145 |
* @param {string} key storage 标记 |
|
146 |
*/ |
|
147 |
function removeLocalStorage(key) { |
|
148 |
localStorage.removeItem(key) |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* 移除本地缓存 sessionStorage |
|
153 |
* @param {string} key storage 标记 |
|
154 |
*/ |
|
155 |
function removeSessionStorage(key) { |
|
156 |
sessionStorage.removeItem(key) |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* 输入的价格限制在两位小数 |
|
161 |
* @param {string} number 价格 |
|
162 |
* @return {string} 过滤后的价格 |
|
163 |
*/ |
|
164 |
function toFixed2(number) { |
|
165 |
number += '' |
|
166 |
// eslint-disable-next-line eqeqeq |
|
167 |
if (number == '') { |
|
168 |
return '' |
|
169 |
} |
|
170 |
if (isNaN(number)) { |
|
171 |
return '' |
|
172 |
} |
|
173 |
|
|
174 |
if (number.indexOf('.') > -1) { |
|
175 |
var arr = number.split('.') |
|
176 |
if (arr[1].length > 2) { |
|
177 |
arr[1] = arr[1].substring(0, 2) |
|
178 |
} |
|
179 |
number = arr[0] + '.' + arr[1] |
|
180 |
return number |
|
181 |
} |
|
182 |
|
|
183 |
return number |
|
184 |
} |
|
185 |
/** |
|
186 |
* 价格转,分,为单位 |
|
187 |
* @param {string|number} price |
|
188 |
*/ |
|
189 |
function amountFen(price) { |
|
190 |
return parseInt(price * 100) |
|
191 |
} |
|
192 |
|
|
193 |
/** |
|
194 |
* 获取查询字符串 |
|
195 |
* @param {string} name |
|
196 |
* @param {string} url 默认 location.href |
|
197 |
*/ |
|
198 |
function getQueryString(name, url) { |
|
199 |
url = url || location.href |
|
200 |
|
|
201 |
url = url.replace(/\#\S*/g, '') |
|
202 |
|
|
203 |
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)') |
|
204 |
var r = /\?/.test(url) && url.split('?')[1].match(reg) |
|
205 |
if (r != null) { |
|
206 |
return r[2] |
|
207 |
} |
|
208 |
return '' |
|
209 |
} |
|
210 |
|
|
211 |
// 全局缓存 |
|
212 |
var g_el = null |
|
213 |
/** |
|
214 |
* 绑定div滚动到底部 |
|
215 |
* @param {dom} el |
|
216 |
* @param {function} callback |
|
217 |
*/ |
|
218 |
function onReachBottom(el, callback) { |
|
219 |
const offsetHeight = el.offsetHeight || window.outerHeight |
|
220 |
const isWindow = el === window |
|
221 |
g_el = el |
|
222 |
|
|
223 |
el.onscroll = () => { |
|
224 |
const scrollTop = isWindow ? document.documentElement.scrollTop : el.scrollTop |
|
225 |
const scrollHeight = isWindow ? document.body.scrollHeight : el.scrollHeight |
|
226 |
// console.log(scrollTop, scrollHeight) |
|
227 |
// console.log(offsetHeight, scrollTop, scrollHeight) |
|
228 |
if ((offsetHeight + scrollTop) - scrollHeight >= -1) { |
|
229 |
typeof callback === 'function' && callback() |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
/** |
|
234 |
* 取消绑定 |
|
235 |
* @param {dom} el |
|
236 |
*/ |
|
237 |
function offReachBottom(el) { |
|
238 |
el = el || g_el |
|
239 |
el.onscroll = null |
|
240 |
g_el = null |
|
241 |
} |
|
242 |
/** |
|
243 |
* 生成唯一id |
|
244 |
*/ |
|
245 |
function uuid() { |
|
246 |
var s = [] |
|
247 |
var hexDigits = '0123456789abcdef' |
|
248 |
for (var i = 0; i < 36; i++) { |
|
249 |
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1) |
|
250 |
} |
|
251 |
s[14] = '4' |
|
252 |
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) |
|
253 |
|
|
254 |
s[8] = s[13] = s[18] = s[23] = '-' |
|
255 |
var uuid = s.join('') |
|
256 |
return uuid |
|
257 |
} |
|
258 |
|
|
259 |
/** |
|
260 |
* 替换当前链接,无返回 |
|
261 |
* @param {string} href |
|
262 |
*/ |
|
263 |
function urlReplace(href) { |
|
264 |
if (!href) { |
|
265 |
return |
|
266 |
} |
|
267 |
if (href && /^#|javasc/.test(href) === false) { |
|
268 |
if (history.replaceState) { |
|
269 |
history.replaceState(null, document.title, href.split('#')[0] + '#') |
|
270 |
location.replace(href) |
|
271 |
} else { |
|
272 |
location.replace(href) |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
/** |
|
278 |
* 获取当前时间的对应的时间对象(如年月日,时分秒,星期几,时间戳) |
|
279 |
* @param {Object} page 页面对象 |
|
280 |
* @param {String} key 时间对象的字段名 |
bb2b08
|
281 |
* @param {String} pageKey 页面关键词 |
3ac5f2
|
282 |
*/ |
bb2b08
|
283 |
function countGetTime(page, key, pageKey) { |
3ac5f2
|
284 |
var dayStamp = new Date().getTime() |
bb2b08
|
285 |
fn_obj[`timer_${pageKey}`] = null |
3ac5f2
|
286 |
var time = new Date(dayStamp) |
J |
287 |
var year = time.getFullYear() |
|
288 |
let month = time.getMonth() + 1 |
|
289 |
let date = time.getDate() |
|
290 |
let hours = time.getHours() |
|
291 |
let minute = time.getMinutes() |
|
292 |
let second = time.getSeconds() |
|
293 |
|
|
294 |
if (month < 10) { month = '0' + month } |
|
295 |
if (date < 10) { date = '0' + date } |
|
296 |
if (hours < 10) { hours = '0' + hours } |
|
297 |
if (minute < 10) { minute = '0' + minute } |
|
298 |
if (second < 10) { second = '0' + second } |
|
299 |
const weekNum = new Date().getDay() |
|
300 |
const week = { |
|
301 |
0: '星期天', |
|
302 |
1: '星期一', |
|
303 |
2: '星期二', |
|
304 |
3: '星期三', |
|
305 |
4: '星期四', |
|
306 |
5: '星期五', |
|
307 |
6: '星期六' |
|
308 |
} |
|
309 |
var res = { |
|
310 |
week: week[weekNum ] ? week[weekNum ] : '日期错误', |
|
311 |
day: `${year}年${month}月${date}日 ${hours}:${minute}:${second}`, |
|
312 |
date: `${year}年${month}月${date}日`, |
|
313 |
timeMin: `${hours}:${minute}`, |
|
314 |
time: `${hours}:${minute}:${second}`, |
|
315 |
timeStamp: dayStamp |
|
316 |
} |
|
317 |
page[key] = res |
bb2b08
|
318 |
fn_obj[`timer_${pageKey}`] = setTimeout(() => { |
J |
319 |
countGetTime(page, key, pageKey) |
3ac5f2
|
320 |
}, 1000) |
bb2b08
|
321 |
// console.log('1111111111111111111111', fn_obj) |
J |
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* 停止当前页面的计时功能 |
|
326 |
* @param {String} pageKey 页面关键词 |
|
327 |
*/ |
|
328 |
function stopCountGetTime(pageKey) { |
|
329 |
clearTimeout(fn_obj[`timer_${pageKey}`]) |
|
330 |
fn_obj[`timer_${pageKey}`] = null |
3ac5f2
|
331 |
} |
J |
332 |
|
aeb317
|
333 |
/** |
J |
334 |
* 轮询异步调用接口 |
|
335 |
* @param {Object} page 页面对象this |
|
336 |
* @param {String} fnName 要轮询的方法名称 |
|
337 |
* @param {String} pageKey 页面关键字 |
|
338 |
* @param {Number} timeStamp 多少秒轮询 |
|
339 |
*/ |
|
340 |
function pollingAjaxFn(page, fnName, pageKey, timeStamp = 1000) { |
|
341 |
if (!page || !fnName || !pageKey) { |
|
342 |
return |
|
343 |
} |
|
344 |
page[fnName] && page[fnName]() |
|
345 |
fn_obj[`pollingTimer_${pageKey}`] = setTimeout(() => { |
|
346 |
pollingAjaxFn(page, fnName, pageKey, timeStamp) |
|
347 |
}, timeStamp) |
|
348 |
} |
|
349 |
|
|
350 |
/** |
|
351 |
* 停止轮询 |
|
352 |
* @param {String} pageKey 页面关键词 |
|
353 |
*/ |
|
354 |
function stopPollingAjaxFn(pageKey) { |
|
355 |
clearTimeout(fn_obj[`pollingTimer_${pageKey}`]) |
|
356 |
fn_obj[`pollingTimer_${pageKey}`] = null |
|
357 |
} |
|
358 |
|
|
359 |
// 随机12位数 |
|
360 |
function getCheckedId() { |
|
361 |
var s = [] |
|
362 |
var hexDigits = '0123456789abcdef' |
|
363 |
for (var i = 0; i < 12; i++) { |
|
364 |
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1) |
|
365 |
} |
|
366 |
// s[14] = '4'; |
|
367 |
// s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); |
|
368 |
|
|
369 |
// s[8] = s[13] = s[18] = s[23] = '-' |
|
370 |
var uuid = s.join('') |
|
371 |
return uuid |
|
372 |
} |
|
373 |
|
3ac5f2
|
374 |
var fn = { |
J |
375 |
getLocalStorage, |
|
376 |
getSessionStorage, |
|
377 |
setLocalStorage, |
|
378 |
setSessionStorage, |
|
379 |
removeLocalStorage, |
|
380 |
removeSessionStorage, |
|
381 |
|
|
382 |
// eslint-disable-next-line indent |
|
383 |
// eslint-disable-next-line indent |
|
384 |
// 判断是否有滚动条,并获取滚动宽度 |
|
385 |
// getScrollBarWidth () { |
|
386 |
// return hasScrollBar(document.body) ? getScrollBarWidth() : 0 |
|
387 |
// }, |
|
388 |
getScrollBarWidth, |
|
389 |
|
|
390 |
toFixed2, |
|
391 |
amountFen, |
|
392 |
|
|
393 |
getQueryString, |
|
394 |
onReachBottom, |
|
395 |
offReachBottom, |
|
396 |
|
|
397 |
uuid, |
|
398 |
urlReplace, |
|
399 |
|
|
400 |
countGetTime, |
bb2b08
|
401 |
stopCountGetTime, |
aeb317
|
402 |
pollingAjaxFn, |
J |
403 |
stopPollingAjaxFn, |
|
404 |
getCheckedId, |
3ac5f2
|
405 |
|
J |
406 |
/** |
|
407 |
* 深拷贝 |
|
408 |
* @param {object} obj 被复制的对象 |
|
409 |
* @return {object} 复制完成的对象 |
|
410 |
*/ |
|
411 |
deepCopyFN(obj) { |
|
412 |
if (typeof obj !== 'object') { |
|
413 |
return obj |
|
414 |
} |
|
415 |
|
|
416 |
let cloneObj = {} |
|
417 |
switch (obj.constructor) { |
|
418 |
case Array: |
|
419 |
cloneObj = [] |
|
420 |
// eslint-disable-next-line no-fallthrough |
|
421 |
case Object: |
|
422 |
for (var property in obj) { |
|
423 |
cloneObj[property] = typeof obj[property] === 'object' ? this.deepCopyFN(obj[property]) : obj[property] |
|
424 |
} |
|
425 |
break |
|
426 |
case Map: |
|
427 |
cloneObj = new Map() |
|
428 |
obj.forEach((value, key) => { |
|
429 |
cloneObj.set(key, typeof value === 'object' ? this.deepCopyFN(value) : value) |
|
430 |
}) |
|
431 |
break |
|
432 |
case Set: |
|
433 |
cloneObj = new Set() |
|
434 |
obj.forEach(value => { |
|
435 |
cloneObj.add(typeof value === 'object' ? this.deepCopyFN(value) : value) |
|
436 |
}) |
|
437 |
break |
|
438 |
} |
|
439 |
return cloneObj |
|
440 |
} |
|
441 |
} |
|
442 |
export default fn |