From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uni_modules/wu-ui-tools/libs/function/index.js | 738 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 738 insertions(+), 0 deletions(-) diff --git a/uni_modules/wu-ui-tools/libs/function/index.js b/uni_modules/wu-ui-tools/libs/function/index.js new file mode 100644 index 0000000..d9bad2d --- /dev/null +++ b/uni_modules/wu-ui-tools/libs/function/index.js @@ -0,0 +1,738 @@ +import { number, empty } from './test.js' +import { round } from './digit.js' +// 棰滆壊鎿嶄綔鏂规硶 +import Color from './color' + +/** + * @description 濡傛灉value灏忎簬min锛屽彇min锛涘鏋渧alue澶т簬max锛屽彇max + * @param {number} min + * @param {number} max + * @param {number} value + */ +function range(min = 0, max = 0, value = 0) { + return Math.max(min, Math.min(max, Number(value))) +} + +/** + * @description 鐢ㄤ簬鑾峰彇鐢ㄦ埛浼犻�掑�肩殑px鍊� 濡傛灉鐢ㄦ埛浼犻�掍簡"xxpx"鎴栬��"xxrpx"锛屽彇鍑哄叾鏁板�奸儴鍒嗭紝濡傛灉鏄�"xxxrpx"杩橀渶瑕佺敤杩噓ni.upx2px杩涜杞崲 + * @param {number|string} value 鐢ㄦ埛浼犻�掑�肩殑px鍊� + * @param {boolean} unit + * @returns {number|string} + */ +function getPx(value, unit = false) { + if (number(value)) { + return unit ? `${value}px` : Number(value) + } + // 濡傛灉甯︽湁rpx锛屽厛鍙栧嚭鍏舵暟鍊奸儴鍒嗭紝鍐嶈浆涓簆x鍊� + if (/(rpx|upx)$/.test(value)) { + return unit ? `${uni.upx2px(parseInt(value))}px` : Number(uni.upx2px(parseInt(value))) + } + return unit ? `${parseInt(value)}px` : parseInt(value) +} + +/** + * @description 杩涜寤舵椂锛屼互杈惧埌鍙互绠�鍐欎唬鐮佺殑鐩殑 姣斿: await uni.$w.sleep(20)灏嗕細闃诲20ms + * @param {number} value 鍫靛鏃堕棿 鍗曚綅ms 姣 + * @returns {Promise} 杩斿洖promise + */ +function sleep(value = 30) { + return new Promise((resolve) => { + setTimeout(() => { + resolve() + }, value) + }) +} +/** + * @description 杩愯鏈熷垽鏂钩鍙� + * @returns {string} 杩斿洖鎵�鍦ㄥ钩鍙�(灏忓啓) + * @link 杩愯鏈熷垽鏂钩鍙� https://uniapp.dcloud.io/frame?id=鍒ゆ柇骞冲彴 + */ +function os() { + return uni.getSystemInfoSync().platform.toLowerCase() +} +/** + * @description 鑾峰彇绯荤粺淇℃伅鍚屾鎺ュ彛 + * @link 鑾峰彇绯荤粺淇℃伅鍚屾鎺ュ彛 https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync + */ +function sys() { + return uni.getSystemInfoSync() +} + +/** + * @description 鍙栦竴涓尯闂存暟 + * @param {Number} min 鏈�灏忓�� + * @param {Number} max 鏈�澶у�� + */ +function random(min, max) { + if (min >= 0 && max > 0 && max >= min) { + const gab = max - min + 1 + return Math.floor(Math.random() * gab + min) + } + return 0 +} + +/** + * @param {Number} len uuid鐨勯暱搴� + * @param {Boolean} firstU 灏嗚繑鍥炵殑棣栧瓧姣嶇疆涓�"u" + * @param {Nubmer} radix 鐢熸垚uuid鐨勫熀鏁�(鎰忓懗鐫�杩斿洖鐨勫瓧绗︿覆閮芥槸杩欎釜鍩烘暟),2-浜岃繘鍒�,8-鍏繘鍒�,10-鍗佽繘鍒�,16-鍗佸叚杩涘埗 + */ +function guid(len = 32, firstU = true, radix = null) { + const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') + const uuid = [] + radix = radix || chars.length + + if (len) { + // 濡傛灉鎸囧畾uuid闀垮害,鍙槸鍙栭殢鏈虹殑瀛楃,0|x涓轰綅杩愮畻,鑳藉幓鎺墄鐨勫皬鏁颁綅,杩斿洖鏁存暟浣� + for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix] + } else { + let r + // rfc4122鏍囧噯瑕佹眰杩斿洖鐨剈uid涓�,鏌愪簺浣嶄负鍥哄畾鐨勫瓧绗� + uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-' + uuid[14] = '4' + + for (let i = 0; i < 36; i++) { + if (!uuid[i]) { + r = 0 | Math.random() * 16 + uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r] + } + } + } + // 绉婚櫎绗竴涓瓧绗�,骞剁敤u鏇夸唬,鍥犱负绗竴涓瓧绗︿负鏁板�兼椂,璇uuid涓嶈兘鐢ㄤ綔id鎴栬�卌lass + if (firstU) { + uuid.shift() + return `u${uuid.join('')}` + } + return uuid.join('') +} + +/** +* @description 鑾峰彇鐖剁粍浠剁殑鍙傛暟锛屽洜涓烘敮浠樺疂灏忕▼搴忎笉鏀寔provide/inject鐨勫啓娉� + this.$parent鍦ㄩ潪H5涓紝鍙互鍑嗙‘鑾峰彇鍒扮埗缁勪欢锛屼絾鏄湪H5涓紝闇�瑕佸娆his.$parent.$parent.xxx + 杩欓噷榛樿鍊肩瓑浜巙ndefined鏈夊畠鐨勫惈涔夛紝鍥犱负鏈�椤跺眰鍏冪礌(缁勪欢)鐨�$parent灏辨槸undefined锛屾剰鍛崇潃涓嶄紶name + 鍊�(榛樿涓簎ndefined)锛屽氨鏄煡鎵炬渶椤跺眰鐨�$parent +* @param {string|undefined} name 鐖剁粍浠剁殑鍙傛暟鍚� +*/ +function $parent(name = undefined) { + let parent = this.$parent + // 閫氳繃while鍘嗛亶锛岃繖閲屼富瑕佹槸涓轰簡H5闇�瑕佸灞傝В鏋愮殑闂 + while (parent) { + // 鐖剁粍浠� + if (parent.$options && parent.$options.name !== name) { + // 濡傛灉缁勪欢鐨刵ame涓嶇浉绛夛紝缁х画涓婁竴绾у鎵� + parent = parent.$parent + } else { + return parent + } + } + return false +} + +/** + * @description 鏍峰紡杞崲 + * 瀵硅薄杞瓧绗︿覆锛屾垨鑰呭瓧绗︿覆杞璞� + * @param {object | string} customStyle 闇�瑕佽浆鎹㈢殑鐩爣 + * @param {String} target 杞崲鐨勭洰鐨勶紝object-杞负瀵硅薄锛宻tring-杞负瀛楃涓� + * @returns {object|string} + */ +function addStyle(customStyle, target = 'object') { + // 瀛楃涓茶浆瀛楃涓诧紝瀵硅薄杞璞℃儏褰紝鐩存帴杩斿洖 + if (empty(customStyle) || typeof(customStyle) === 'object' && target === 'object' || target === 'string' && + typeof(customStyle) === 'string') { + return customStyle + } + // 瀛楃涓茶浆瀵硅薄 + if (target === 'object') { + // 鍘婚櫎瀛楃涓叉牱寮忎腑鐨勪袱绔┖鏍�(涓棿鐨勭┖鏍间笉鑳藉幓鎺夛紝姣斿padding: 20px 0濡傛灉鍘绘帀浜嗗氨閿欎簡)锛岀┖鏍兼槸鏃犵敤鐨� + customStyle = trim(customStyle) + // 鏍规嵁";"灏嗗瓧绗︿覆杞负鏁扮粍褰㈠紡 + const styleArray = customStyle.split(';') + const style = {} + // 鍘嗛亶鏁扮粍锛屾嫾鎺ユ垚瀵硅薄 + for (let i = 0; i < styleArray.length; i++) { + // 'font-size:20px;color:red;'锛屽姝ゆ渶鍚庡瓧绗︿覆鏈�";"鐨勮瘽锛屼細瀵艰嚧styleArray鏈�鍚庝竴涓厓绱犱负绌哄瓧绗︿覆锛岃繖閲岄渶瑕佽繃婊� + if (styleArray[i]) { + const item = styleArray[i].split(':') + style[trim(item[0])] = trim(item[1]) + } + } + return style + } + // 杩欓噷涓哄璞¤浆瀛楃涓插舰寮� + let string = '' + for (const i in customStyle) { + // 椹煎嘲杞负涓垝绾跨殑褰㈠紡锛屽惁鍒檆ss鍐呰仈鏍峰紡锛屾棤娉曡瘑鍒┘宄版牱寮忓睘鎬у悕 + const key = i.replace(/([A-Z])/g, '-$1').toLowerCase() + string += `${key}:${customStyle[i]};` + } + // 鍘婚櫎涓ょ绌烘牸 + return trim(string) +} + +/** + * @description 娣诲姞鍗曚綅锛屽鏋滄湁rpx锛寀px锛�%锛宲x绛夊崟浣嶇粨灏炬垨鑰呭�间负auto锛岀洿鎺ヨ繑鍥烇紝鍚﹀垯鍔犱笂px鍗曚綅缁撳熬 + * @param {string|number} value 闇�瑕佹坊鍔犲崟浣嶇殑鍊� + * @param {string} unit 娣诲姞鐨勫崟浣嶅悕 姣斿px + */ +function addUnit(value = 'auto', unit = uni?.$w?.config?.unit ? uni?.$w?.config?.unit : 'px') { + value = String(value) + // 鐢╳uui鍐呯疆楠岃瘉瑙勫垯涓殑number鍒ゆ柇鏄惁涓烘暟鍊� + return number(value) ? `${value}${unit}` : value +} + +/** + * @description 娣卞害鍏嬮殕 + * @param {object} obj 闇�瑕佹繁搴﹀厠闅嗙殑瀵硅薄 + * @param cache 缂撳瓨 + * @returns {*} 鍏嬮殕鍚庣殑瀵硅薄鎴栬�呭師鍊硷紙涓嶆槸瀵硅薄锛� + */ +function deepClone(obj, cache = new WeakMap()) { + if (obj === null || typeof obj !== 'object') return obj; + if (cache.has(obj)) return cache.get(obj); + let clone; + if (obj instanceof Date) { + clone = new Date(obj.getTime()); + } else if (obj instanceof RegExp) { + clone = new RegExp(obj); + } else if (obj instanceof Map) { + clone = new Map(Array.from(obj, ([key, value]) => [key, deepClone(value, cache)])); + } else if (obj instanceof Set) { + clone = new Set(Array.from(obj, value => deepClone(value, cache))); + } else if (Array.isArray(obj)) { + clone = obj.map(value => deepClone(value, cache)); + } else if (Object.prototype.toString.call(obj) === '[object Object]') { + clone = Object.create(Object.getPrototypeOf(obj)); + cache.set(obj, clone); + for (const [key, value] of Object.entries(obj)) { + clone[key] = deepClone(value, cache); + } + } else { + clone = Object.assign({}, obj); + } + cache.set(obj, clone); + return clone; +} + +/** + * @description JS瀵硅薄娣卞害鍚堝苟 + * @param {object} target 闇�瑕佹嫹璐濈殑瀵硅薄 + * @param {object} source 鎷疯礉鐨勬潵婧愬璞� + * @returns {object|boolean} 娣卞害鍚堝苟鍚庣殑瀵硅薄鎴栬�協alse锛堝叆鍙傛湁涓嶆槸瀵硅薄锛� + */ +function deepMerge(target = {}, source = {}) { + target = deepClone(target) + if (typeof target !== 'object' || target === null || typeof source !== 'object' || source === null) return target; + const merged = Array.isArray(target) ? target.slice() : Object.assign({}, target); + for (const prop in source) { + if (!source.hasOwnProperty(prop)) continue; + const sourceValue = source[prop]; + const targetValue = merged[prop]; + if (sourceValue instanceof Date) { + merged[prop] = new Date(sourceValue); + } else if (sourceValue instanceof RegExp) { + merged[prop] = new RegExp(sourceValue); + } else if (sourceValue instanceof Map) { + merged[prop] = new Map(sourceValue); + } else if (sourceValue instanceof Set) { + merged[prop] = new Set(sourceValue); + } else if (typeof sourceValue === 'object' && sourceValue !== null) { + merged[prop] = deepMerge(targetValue, sourceValue); + } else { + merged[prop] = sourceValue; + } + } + return merged; +} + +/** + * @description error鎻愮ず + * @param {*} err 閿欒鍐呭 + */ +function error(err) { + // 寮�鍙戠幆澧冩墠鎻愮ず锛岀敓浜х幆澧冧笉浼氭彁绀� + if (process.env.NODE_ENV === 'development') { + console.error(`wuui鎻愮ず锛�${err}`) + } +} + +/** + * @description 鎵撲贡鏁扮粍 + * @param {array} array 闇�瑕佹墦涔辩殑鏁扮粍 + * @returns {array} 鎵撲贡鍚庣殑鏁扮粍 + */ +function randomArray(array = []) { + // 鍘熺悊鏄痵ort鎺掑簭,Math.random()浜х敓0<= x < 1涔嬮棿鐨勬暟,浼氬鑷磝-0.05澶т簬鎴栬�呭皬浜�0 + return array.sort(() => Math.random() - 0.5) +} + +// padStart 鐨� polyfill锛屽洜涓烘煇浜涙満鍨嬫垨鎯呭喌锛岃繕鏃犳硶鏀寔es7鐨刾adStart锛屾瘮濡傜數鑴戠増鐨勫井淇″皬绋嬪簭 +// 鎵�浠ヨ繖閲屽仛涓�涓吋瀹筽olyfill鐨勫吋瀹瑰鐞� +if (!String.prototype.padStart) { + // 涓轰簡鏂逛究琛ㄧず杩欓噷 fillString 鐢ㄤ簡ES6 鐨勯粯璁ゅ弬鏁帮紝涓嶅奖鍝嶇悊瑙� + String.prototype.padStart = function(maxLength, fillString = ' ') { + if (Object.prototype.toString.call(fillString) !== '[object String]') { + throw new TypeError( + 'fillString must be String' + ) + } + const str = this + // 杩斿洖 String(str) 杩欓噷鏄负浜嗕娇杩斿洖鐨勫�兼槸瀛楃涓插瓧闈㈤噺锛屽湪鎺у埗鍙颁腑鏇寸鍚堢洿瑙� + if (str.length >= maxLength) return String(str) + + const fillLength = maxLength - str.length + let times = Math.ceil(fillLength / fillString.length) + while (times >>= 1) { + fillString += fillString + if (times === 1) { + fillString += fillString + } + } + return fillString.slice(0, fillLength) + str + } +} + +/** + * @description 鏍煎紡鍖栨椂闂� + * @param {String|Number} dateTime 闇�瑕佹牸寮忓寲鐨勬椂闂存埑 + * @param {String} fmt 鏍煎紡鍖栬鍒� yyyy:mm:dd|yyyy:mm|yyyy骞磎m鏈坉d鏃yyyy骞磎m鏈坉d鏃� hh鏃禡M鍒嗙瓑,鍙嚜瀹氫箟缁勫悎 榛樿yyyy-mm-dd + * @returns {string} 杩斿洖鏍煎紡鍖栧悗鐨勫瓧绗︿覆 + */ +function timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') { + let date + // 鑻ヤ紶鍏ユ椂闂翠负鍋囧�硷紝鍒欏彇褰撳墠鏃堕棿 + if (!dateTime) { + date = new Date() + } + // 鑻ヤ负unix绉掓椂闂存埑锛屽垯杞负姣鏃堕棿鎴筹紙閫昏緫鏈夌偣濂囨�紝浣嗕笉鏁㈡敼锛屼互淇濊瘉鍘嗗彶鍏煎锛� + else if (/^\d{10}$/.test(dateTime?.toString().trim())) { + date = new Date(dateTime * 1000) + } + // 鑻ョ敤鎴蜂紶鍏ュ瓧绗︿覆鏍煎紡鏃堕棿鎴筹紝new Date鏃犳硶瑙f瀽锛岄渶鍋氬吋瀹� + else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) { + date = new Date(Number(dateTime)) + } + // 澶勭悊骞冲彴鎬у樊寮傦紝鍦⊿afari/Webkit涓紝new Date浠呮敮鎸�/浣滀负鍒嗗壊绗︾殑瀛楃涓叉椂闂� + // 澶勭悊 '2022-07-10 01:02:03'锛岃烦杩� '2022-07-10T01:02:03' + else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) { + date = new Date(dateTime.replace(/-/g, '/')) + } + // 鍏朵粬閮借涓虹鍚� RFC 2822 瑙勮寖 + else { + date = new Date(dateTime) + } + + const timeSource = { + 'y': date.getFullYear().toString(), // 骞� + 'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 鏈� + 'd': date.getDate().toString().padStart(2, '0'), // 鏃� + 'h': date.getHours().toString().padStart(2, '0'), // 鏃� + 'M': date.getMinutes().toString().padStart(2, '0'), // 鍒� + 's': date.getSeconds().toString().padStart(2, '0') // 绉� + // 鏈夊叾浠栨牸寮忓寲瀛楃闇�姹傚彲浠ョ户缁坊鍔狅紝蹇呴』杞寲鎴愬瓧绗︿覆 + } + + for (const key in timeSource) { + const [ret] = new RegExp(`${key}+`).exec(formatStr) || [] + if (ret) { + // 骞村彲鑳藉彧闇�灞曠ず涓や綅 + const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0 + formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex)) + } + } + + return formatStr +} + +/** + * @description 鏃堕棿鎴宠浆涓哄涔呬箣鍓� + * @param {String|Number} timestamp 鏃堕棿鎴� + * @param {String|Boolean} format + * 鏍煎紡鍖栬鍒欏鏋滀负鏃堕棿鏍煎紡瀛楃涓诧紝瓒呭嚭涓�瀹氭椂闂磋寖鍥达紝杩斿洖鍥哄畾鐨勬椂闂存牸寮忥紱 + * 濡傛灉涓哄竷灏斿�糵alse锛屾棤璁轰粈涔堟椂闂达紝閮借繑鍥炲涔呬互鍓嶇殑鏍煎紡 + * @returns {string} 杞寲鍚庣殑鍐呭 + */ +function timeFrom(timestamp = null, format = 'yyyy-mm-dd') { + if (timestamp == null) timestamp = Number(new Date()) + timestamp = parseInt(timestamp) + // 鍒ゆ柇鐢ㄦ埛杈撳叆鐨勬椂闂存埑鏄杩樻槸姣,涓�鑸墠绔痡s鑾峰彇鐨勬椂闂存埑鏄绉�(13浣�),鍚庣浼犺繃鏉ョ殑涓虹(10浣�) + if (timestamp.toString().length == 10) timestamp *= 1000 + let timer = (new Date()).getTime() - timestamp + timer = parseInt(timer / 1000) + // 濡傛灉灏忎簬5鍒嗛挓,鍒欒繑鍥�"鍒氬垰",鍏朵粬浠ユ绫绘帹 + let tips = '' + switch (true) { + case timer < 300: + tips = '鍒氬垰' + break + case timer >= 300 && timer < 3600: + tips = `${parseInt(timer / 60)}鍒嗛挓鍓峘 + break + case timer >= 3600 && timer < 86400: + tips = `${parseInt(timer / 3600)}灏忔椂鍓峘 + break + case timer >= 86400 && timer < 2592000: + tips = `${parseInt(timer / 86400)}澶╁墠` + break + default: + // 濡傛灉format涓篺alse锛屽垯鏃犺浠�涔堟椂闂存埑锛岄兘鏄剧ずxx涔嬪墠 + if (format === false) { + if (timer >= 2592000 && timer < 365 * 86400) { + tips = `${parseInt(timer / (86400 * 30))}涓湀鍓峘 + } else { + tips = `${parseInt(timer / (86400 * 365))}骞村墠` + } + } else { + tips = timeFormat(timestamp, format) + } + } + return tips +} + +/** + * @description 鍘婚櫎绌烘牸 + * @param String str 闇�瑕佸幓闄ょ┖鏍肩殑瀛楃涓� + * @param String pos both(宸﹀彸)|left|right|all 榛樿both + */ +function trim(str, pos = 'both') { + str = String(str) + if (pos == 'both') { + return str.replace(/^\s+|\s+$/g, '') + } + if (pos == 'left') { + return str.replace(/^\s*/, '') + } + if (pos == 'right') { + return str.replace(/(\s*$)/g, '') + } + if (pos == 'all') { + return str.replace(/\s+/g, '') + } + return str +} + +/** + * @description 瀵硅薄杞瑄rl鍙傛暟 + * @param {object} data,瀵硅薄 + * @param {Boolean} isPrefix,鏄惁鑷姩鍔犱笂"?" + * @param {string} arrayFormat 瑙勫垯 indices|brackets|repeat|comma + */ +function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') { + const prefix = isPrefix ? '?' : '' + const _result = [] + if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets' + for (const key in data) { + const value = data[key] + // 鍘绘帀涓虹┖鐨勫弬鏁� + if (['', undefined, null].indexOf(value) >= 0) { + continue + } + // 濡傛灉鍊间负鏁扮粍锛屽彟琛屽鐞� + if (value.constructor === Array) { + // e.g. {ids: [1, 2, 3]} + switch (arrayFormat) { + case 'indices': + // 缁撴灉: ids[0]=1&ids[1]=2&ids[2]=3 + for (let i = 0; i < value.length; i++) { + _result.push(`${key}[${i}]=${value[i]}`) + } + break + case 'brackets': + // 缁撴灉: ids[]=1&ids[]=2&ids[]=3 + value.forEach((_value) => { + _result.push(`${key}[]=${_value}`) + }) + break + case 'repeat': + // 缁撴灉: ids=1&ids=2&ids=3 + value.forEach((_value) => { + _result.push(`${key}=${_value}`) + }) + break + case 'comma': + // 缁撴灉: ids=1,2,3 + let commaStr = '' + value.forEach((_value) => { + commaStr += (commaStr ? ',' : '') + _value + }) + _result.push(`${key}=${commaStr}`) + break + default: + value.forEach((_value) => { + _result.push(`${key}[]=${_value}`) + }) + } + } else { + _result.push(`${key}=${value}`) + } + } + return _result.length ? prefix + _result.join('&') : '' +} + +/** + * 鏄剧ず娑堟伅鎻愮ず妗� + * @param {String} title 鎻愮ず鐨勫唴瀹癸紝闀垮害涓� icon 鍙栧�兼湁鍏炽�� + * @param {Number} duration 鎻愮ず鐨勫欢杩熸椂闂达紝鍗曚綅姣锛岄粯璁わ細2000 + */ +function toast(title, duration = 2000) { + uni.showToast({ + title: String(title), + icon: 'none', + duration + }) +} + +/** + * @description 鏍规嵁涓婚type鍊�,鑾峰彇瀵瑰簲鐨勫浘鏍� + * @param {String} type 涓婚鍚嶇О,primary|info|error|warning|success + * @param {boolean} fill 鏄惁浣跨敤fill濉厖瀹炰綋鐨勫浘鏍� + */ +function type2icon(type = 'success', fill = false) { + // 濡傛灉闈為缃��,榛樿涓簊uccess + if (['primary', 'info', 'error', 'warning', 'success'].indexOf(type) == -1) type = 'success' + let iconName = '' + // 鐩墠(2019-12-12),info鍜宲rimary浣跨敤鍚屼竴涓浘鏍� + switch (type) { + case 'primary': + iconName = 'info-circle' + break + case 'info': + iconName = 'info-circle' + break + case 'error': + iconName = 'close-circle' + break + case 'warning': + iconName = 'error-circle' + break + case 'success': + iconName = 'checkmark-circle' + break + default: + iconName = 'checkmark-circle' + } + // 鏄惁鏄疄浣撶被鍨�,鍔犱笂-fill,鍦╥con缁勪欢搴撲腑,瀹炰綋鐨勭被鍚嶆槸鍚庨潰鍔�-fill鐨� + if (fill) iconName += '-fill' + return iconName +} + +/** + * @description 鏁板瓧鏍煎紡鍖� + * @param {number|string} number 瑕佹牸寮忓寲鐨勬暟瀛� + * @param {number} decimals 淇濈暀鍑犱綅灏忔暟 + * @param {string} decimalPoint 灏忔暟鐐圭鍙� + * @param {string} thousandsSeparator 鍗冨垎浣嶇鍙� + * @returns {string} 鏍煎紡鍖栧悗鐨勬暟瀛� + */ +function priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparator = ',') { + number = (`${number}`).replace(/[^0-9+-Ee.]/g, '') + const n = !isFinite(+number) ? 0 : +number + const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals) + const sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator + const dec = (typeof decimalPoint === 'undefined') ? '.' : decimalPoint + let s = '' + + s = (prec ? round(n, prec) + '' : `${Math.round(n)}`).split('.') + const re = /(-?\d+)(\d{3})/ + while (re.test(s[0])) { + s[0] = s[0].replace(re, `$1${sep}$2`) + } + + if ((s[1] || '').length < prec) { + s[1] = s[1] || '' + s[1] += new Array(prec - s[1].length + 1).join('0') + } + return s.join(dec) +} + +/** + * @description 鑾峰彇duration鍊� + * 濡傛灉甯︽湁ms鎴栬�卻鐩存帴杩斿洖锛屽鏋滃ぇ浜庝竴瀹氬�硷紝璁や负鏄痬s鍗曚綅锛屽皬浜庝竴瀹氬�硷紝璁や负鏄痵鍗曚綅 + * 姣斿浠�30浣嶉槇鍊硷紝閭d箞300澶т簬30锛屽彲浠ョ悊瑙d负鐢ㄦ埛鎯宠鐨勬槸300ms锛岃�屼笉鏄兂鑺�300s鍘绘墽琛屼竴涓姩鐢� + * @param {String|number} value 姣斿: "1s"|"100ms"|1|100 + * @param {boolean} unit 鎻愮ず: 濡傛灉鏄痜alse 榛樿杩斿洖number + * @return {string|number} + */ +function getDuration(value, unit = true) { + const valueNum = parseInt(value) + if (unit) { + if (/s$/.test(value)) return value + return value > 30 ? `${value}ms` : `${value}s` + } + if (/ms$/.test(value)) return valueNum + if (/s$/.test(value)) return valueNum > 30 ? valueNum : valueNum * 1000 + return valueNum +} + +/** + * @description 鏃ユ湡鐨勬湀鎴栨棩琛ラ浂鎿嶄綔 + * @param {String} value 闇�瑕佽ˉ闆剁殑鍊� + */ +function padZero(value) { + return `00${value}`.slice(-2) +} + +/** + * @description 鍦╳u-form鐨勫瓙缁勪欢鍐呭鍙戠敓鍙樺寲锛屾垨鑰呭け鍘荤劍鐐规椂锛屽皾璇曢�氱煡wu-form鎵ц鏍¢獙鏂规硶 + * @param {*} instance + * @param {*} event + */ +function formValidate(instance, event) { + const formItem = $parent.call(instance, 'wu-form-item') + const form = $parent.call(instance, 'wu-form') + // 濡傛灉鍙戠敓鍙樺寲鐨刬nput鎴栬�卼extarea绛夛紝鍏剁埗缁勪欢涓湁wu-form-item鎴栬�厀u-form绛夛紝灏辨墽琛宖orm鐨剉alidate鏂规硶 + // 鍚屾椂灏唂orm-item鐨刾ros浼犻�掔粰form锛岃鍏惰繘琛岀簿纭璞¢獙璇� + if (formItem && form) { + form.validateField(formItem.prop, () => {}, event) + } +} + +/** + * @description 鑾峰彇鏌愪釜瀵硅薄涓嬬殑灞炴�э紝鐢ㄤ簬閫氳繃绫讳技'a.b.c'鐨勫舰寮忓幓鑾峰彇涓�涓璞$殑鐨勫睘鎬х殑褰㈠紡 + * @param {object} obj 瀵硅薄 + * @param {string} key 闇�瑕佽幏鍙栫殑灞炴�у瓧娈� + * @returns {*} + */ +function getProperty(obj, key) { + if (!obj) { + return + } + if (typeof key !== 'string' || key === '') { + return '' + } + if (key.indexOf('.') !== -1) { + const keys = key.split('.') + let firstObj = obj[keys[0]] || {} + + for (let i = 1; i < keys.length; i++) { + if (firstObj) { + firstObj = firstObj[keys[i]] + } + } + return firstObj + } + return obj[key] +} + +/** + * @description 璁剧疆瀵硅薄鐨勫睘鎬у�硷紝濡傛灉'a.b.c'鐨勫舰寮忚繘琛岃缃� + * @param {object} obj 瀵硅薄 + * @param {string} key 闇�瑕佽缃殑灞炴�� + * @param {string} value 璁剧疆鐨勫�� + */ +function setProperty(obj, key, value) { + if (!obj) { + return + } + // 閫掑綊璧嬪�� + const inFn = function(_obj, keys, v) { + // 鏈�鍚庝竴涓睘鎬ey + if (keys.length === 1) { + _obj[keys[0]] = v + return + } + // 0~length-1涓猭ey + while (keys.length > 1) { + const k = keys[0] + if (!_obj[k] || (typeof _obj[k] !== 'object')) { + _obj[k] = {} + } + const key = keys.shift() + // 鑷皟鐢ㄥ垽鏂槸鍚﹀瓨鍦ㄥ睘鎬э紝涓嶅瓨鍦ㄥ垯鑷姩鍒涘缓瀵硅薄 + inFn(_obj[k], keys, v) + } + } + + if (typeof key !== 'string' || key === '') { + + } else if (key.indexOf('.') !== -1) { // 鏀寔澶氬眰绾ц祴鍊兼搷浣� + const keys = key.split('.') + inFn(obj, keys, value) + } else { + obj[key] = value + } +} + +/** + * @description 鑾峰彇褰撳墠椤甸潰璺緞 + */ +function page() { + const pages = getCurrentPages(); + const route = pages[pages.length - 1]?.route; + // 鏌愪簺鐗规畩鎯呭喌涓�(姣斿椤甸潰杩涜redirectTo鏃剁殑涓�浜涙椂鏈�)锛宲ages鍙兘涓虹┖鏁扮粍 + return `/${route ? route : ''}` +} + +/** + * @description 鑾峰彇褰撳墠璺敱鏍堝疄渚嬫暟缁� + */ +function pages() { + const pages = getCurrentPages() + return pages +} + +/** + * 鑾峰彇椤甸潰鍘嗗彶鏍堟寚瀹氬眰瀹炰緥 + * @param back {number} [0] - 0鎴栬�呰礋鏁帮紝琛ㄧず鑾峰彇鍘嗗彶鏍堢殑鍝竴灞傦紝0琛ㄧず鑾峰彇褰撳墠椤甸潰瀹炰緥锛�-1 琛ㄧず鑾峰彇涓婁竴涓〉闈㈠疄渚嬨�傞粯璁�0銆� + */ +function getHistoryPage(back = 0) { + const pages = getCurrentPages() + const len = pages.length + return pages[len - 1 + back] +} + + + +/** + * @description 淇敼wuui鍐呯疆灞炴�у�� + * @param {object} props 淇敼鍐呯疆props灞炴�� + * @param {object} config 淇敼鍐呯疆config灞炴�� + * @param {object} color 淇敼鍐呯疆color灞炴�� + * @param {object} zIndex 淇敼鍐呯疆zIndex灞炴�� + */ +function setConfig({ + props = {}, + config = {}, + color = {}, + zIndex = {} +}) { + const { + deepMerge, + } = uni.$w + uni.$w.config = deepMerge(uni.$w.config, config) + uni.$w.props = deepMerge(uni.$w.props, props) + uni.$w.color = deepMerge(uni.$w.color, color) + uni.$w.zIndex = deepMerge(uni.$w.zIndex, zIndex) +} + +export { + range, + getPx, + sleep, + os, + sys, + random, + guid, + $parent, + addStyle, + addUnit, + deepClone, + deepMerge, + error, + randomArray, + timeFormat, + timeFrom, + trim, + queryParams, + toast, + type2icon, + priceFormat, + getDuration, + padZero, + formValidate, + getProperty, + setProperty, + page, + pages, + getHistoryPage, + setConfig, + Color +} \ No newline at end of file -- Gitblit v1.9.3