From 9860e221460a0a4ac1903dad2c97160d0eed0e63 Mon Sep 17 00:00:00 2001 From: long <515897141@qq.com> Date: 星期五, 03 三月 2023 10:59:42 +0800 Subject: [PATCH] 初始化 --- src/utils/fn.js | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 333 insertions(+), 0 deletions(-) diff --git a/src/utils/fn.js b/src/utils/fn.js new file mode 100644 index 0000000..57a8364 --- /dev/null +++ b/src/utils/fn.js @@ -0,0 +1,333 @@ +/* + * @Author: 涓橀獜_jun + * @Date: 2019-11-21 10:59:17 + * @Last Modified by: 涓橀獜_jun + * @Last Modified time: 2019-12-30 16:29:45 + */ +// 閫氱敤function锛岄�氳繃鍏ㄥ眬瀹夎锛屾垨import寮曠敤璋冪敤 + +let scrollBarWidth // 婊氬姩鏉″搴� +// 鑾峰彇娴忚鍣ㄦ粴鍔ㄦ潯瀹藉害 +function getScrollBarWidth () { + if (scrollBarWidth !== undefined) return scrollBarWidth; + + const outer = document.createElement('div'); + outer.className = 'el-scrollbar__wrap'; + outer.style.visibility = 'hidden'; + outer.style.width = '100px'; + outer.style.position = 'absolute'; + outer.style.top = '-9999px'; + document.body.appendChild(outer); + + const widthNoScroll = outer.offsetWidth; + outer.style.overflow = 'scroll'; + + const inner = document.createElement('div'); + inner.style.width = '100%'; + outer.appendChild(inner); + + const widthWithScroll = inner.offsetWidth; + outer.parentNode.removeChild(outer); + scrollBarWidth = widthNoScroll - widthWithScroll; + + return scrollBarWidth; +} +// 鑾峰彇鏈�杩戞牱寮� +// function getCurrentStyle (obj, prop) { +// if (obj.currentStyle) { +// return obj.currentStyle[prop]; +// } else if (window.getComputedStyle) { +// prop = prop.replace(/([A-Z])/g, "-$1"); +// prop = prop.toLowerCase(); +// return document.defaultView.getComputedStyle(obj, null)[prop]; +// } +// return null; +// } +// // 鍒ゆ柇鏄惁鏈夋粴鍔ㄦ潯 +// function hasScrollBar (obj) { +// return getCurrentStyle(obj, 'overflow') == 'hidden' +// ? 0 +// : document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight); +// } + +// 鏃ユ湡鏍煎紡鍖� +Date.prototype.format = function(format){ + var o = { + "M+" : this.getMonth()+1, //month + "d+" : this.getDate(), //day + "H+" : this.getHours(), //hour + "m+" : this.getMinutes(), //minute + "s+" : this.getSeconds(), //second + "q+" : Math.floor((this.getMonth()+3)/3), //quarter + "S" : this.getMilliseconds() //millisecond + }; + + if(/(y+)/.test(format)) { + format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); + } + + for(var k in o) { + if(new RegExp("("+ k +")").test(format)) { + format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); + } + } + return format; +}; + +// 娑堥櫎瀛楃涓插浣欑┖鏍� +if (!String.prototype.trim) { + String.prototype.trim = function () { + return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); + }; +} + +/** + * 鑾峰彇鏈湴缂撳瓨 localStorage + * @param {string} key storage 鏍囪 + * @return {*} 鍊� + */ +function getLocalStorage(key){ + if (!key) { + return + } + var result = localStorage.getItem(key) || '' + try { + result = JSON.parse(result) + } catch(e) { + + } + return result +} + +/** + * 鑾峰彇鏈湴缂撳瓨 sessionStorage + * @param {string} key storage 鏍囪 + * @return {*} 鍊� + */ +function getSessionStorage(key){ + var result = sessionStorage.getItem(key) || '' + try { + result = JSON.parse(result) + } catch(e) { + + } + return result +} + +/** + * 淇濆瓨鏈湴缂撳瓨 localStorage + * @param {string} key storage 鏍囪 + * @param {*} value 鍊� + */ +function setLocalStorage(key, value){ + if (typeof value === 'object') { + value = JSON.stringify(value) + } + localStorage.setItem(key, value) +} + +/** + * 淇濆瓨鏈湴缂撳瓨 sessionStorage + * @param {string} key storage 鏍囪 + * @param {*} value 鍊� + */ +function setSessionStorage(key, value){ + if (typeof value === 'object') { + value = JSON.stringify(value) + } + sessionStorage.setItem(key, value) +} + +/** + * 绉婚櫎鏈湴缂撳瓨 localStorage + * @param {string} key storage 鏍囪 + */ +function removeLocalStorage(key){ + localStorage.removeItem(key) +} + +/** + * 绉婚櫎鏈湴缂撳瓨 sessionStorage + * @param {string} key storage 鏍囪 + */ +function removeSessionStorage(key){ + sessionStorage.removeItem(key) +} + +/** + * 杈撳叆鐨勪环鏍奸檺鍒跺湪涓や綅灏忔暟 + * @param {string} number 浠锋牸 + * @return {string} 杩囨护鍚庣殑浠锋牸 + */ +function toFixed2 (number) { + number += '' + if (number == '') { + return '' + } + if (isNaN(number)) { + return '' + } + + if (number.indexOf('.')>-1) { + var arr = number.split('.') + if (arr[1].length>2) { + arr[1] = arr[1].substring(0, 2) + } + number = arr[0] + '.' + arr[1] + return number + } + + return number +} +/** + * 浠锋牸杞紝鍒嗭紝涓哄崟浣� + * @param {string|number} price + */ +function amountFen (price) { + return parseInt(price*100) +} + +/** + * 鑾峰彇鏌ヨ瀛楃涓� + * @param {string} name + * @param {string} url 榛樿 location.href + */ +function getQueryString(name, url){ + url = url || location.href + + url = url.replace(/\#\S*/g, '') + + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); + var r = /\?/.test(url) && url.split('?')[1].match(reg); + if(r != null) { + return r[2]; + } + return ""; +} + +// 鍏ㄥ眬缂撳瓨 +var g_el = null +/** + * 缁戝畾div婊氬姩鍒板簳閮� + * @param {dom} el + * @param {function} callback + */ +function onReachBottom (el, callback) { + let offsetHeight = el.offsetHeight || window.outerHeight + let isWindow = el === window + g_el = el + + el.onscroll = ()=>{ + let scrollTop = isWindow ? document.documentElement.scrollTop : el.scrollTop + let scrollHeight = isWindow ? document.body.scrollHeight : el.scrollHeight + // console.log(scrollTop, scrollHeight) + // console.log(offsetHeight, scrollTop, scrollHeight) + if ((offsetHeight + scrollTop) - scrollHeight >= -1) { + typeof callback === 'function' && callback() + } + } +} +/** + * 鍙栨秷缁戝畾 + * @param {dom} el + */ +function offReachBottom (el) { + el = el || g_el + el.onscroll = null + g_el = null +} +/** + * 鐢熸垚鍞竴id + */ +function uuid () { + var s = [] + var hexDigits = '0123456789abcdef' + for (var i = 0; i < 36; i++) { + s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1) + } + s[14] = '4'; + s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); + + s[8] = s[13] = s[18] = s[23] = '-' + var uuid = s.join('') + return uuid +} + +/** + * 鏇挎崲褰撳墠閾炬帴锛屾棤杩斿洖 + * @param {string} href + */ +function urlReplace (href) { + if (!href) { + return; + } + if (href && /^#|javasc/.test(href) === false) { + if (history.replaceState) { + history.replaceState(null, document.title, href.split('#')[0] + '#'); + location.replace(href); + } else { + location.replace(href); + } + } +}; + +var fn = { + getLocalStorage, + getSessionStorage, + setLocalStorage, + setSessionStorage, + removeLocalStorage, + removeSessionStorage, + + // 鍒ゆ柇鏄惁鏈夋粴鍔ㄦ潯锛屽苟鑾峰彇婊氬姩瀹藉害 + // getScrollBarWidth () { + // return hasScrollBar(document.body) ? getScrollBarWidth() : 0 + // }, + getScrollBarWidth, + + toFixed2, + amountFen, + + getQueryString, + onReachBottom, + offReachBottom, + + uuid, + urlReplace, + + /** + * 娣辨嫹璐� + * @param {object} obj 琚鍒剁殑瀵硅薄 + * @return {object} 澶嶅埗瀹屾垚鐨勫璞� + */ + deepCopyFN (obj) { + if (typeof obj !== 'object') { + return obj + } + + let cloneObj = {} + switch (obj.constructor) { + case Array: + cloneObj = [] + case Object: + for (var property in obj) { + cloneObj[property] = typeof obj[property] === 'object' ? this.deepCopyFN(obj[property]) : obj[property] + } + break + case Map: + cloneObj = new Map() + obj.forEach((value, key) => { + cloneObj.set(key, typeof value === 'object' ? this.deepCopyFN(value) : value) + }) + break + case Set: + cloneObj = new Set() + obj.forEach(value => { + cloneObj.add(typeof value === 'object' ? this.deepCopyFN(value) : value) + }) + break + } + return cloneObj + }, +} +export default fn \ No newline at end of file -- Gitblit v1.8.0