From 659d09ec24dab6c451220c8f3bb3943b0fdb3ba1 Mon Sep 17 00:00:00 2001 From: quanyawei <401863037@qq.com> Date: Mon, 08 Jan 2024 16:16:12 +0800 Subject: [PATCH] fix:地图导航 --- uni_modules/uview-ui/libs/function/index.js | 731 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 731 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/libs/function/index.js b/uni_modules/uview-ui/libs/function/index.js new file mode 100644 index 0000000..bd80ee7 --- /dev/null +++ b/uni_modules/uview-ui/libs/function/index.js @@ -0,0 +1,731 @@ +import test from './test.js' +import { round } from './digit.js' +/** + * @description ������value������min������min���������value������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"���������������uni.upx2px������������ + * @param {number|string} value ������������������px��� + * @param {boolean} unit + * @returns {number|string} + */ +function getPx(value, unit = false) { + if (test.number(value)) { + return unit ? `${value}px` : Number(value) + } + // ������������rpx���������������������������������������px��� + 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.$u.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������������,���������x������������,��������������� + for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix] + } else { + let r + // rfc4122���������������������uuid���,��������������������������� + 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������,���������������������������������,���guuid������������id������class + if (firstU) { + uuid.shift() + return `u${uuid.join('')}` + } + return uuid.join('') +} + +/** +* @description ������������������������������������������������������������provide/inject��������� + this.$parent������H5������������������������������������������������H5������������������this.$parent.$parent.xxx + ���������������������undefined���������������������������������������(������)���$parent������undefined������������������name + ���(���������undefined)���������������������������$parent +* @param {string|undefined} name ��������������������� +*/ +function $parent(name = undefined) { + let parent = this.$parent + // ������while������������������������������H5��������������������������� + while (parent) { + // ��������� + if (parent.$options && parent.$options.name !== name) { + // ���������������name��������������������������������� + parent = parent.$parent + } else { + return parent + } + } + return false +} + +/** + * @description ������������ + * ��������������������������������������������� + * @param {object | string} customStyle ��������������������� + * @param {String} target ������������������object-���������������string-��������������� + * @returns {object|string} + */ +function addStyle(customStyle, target = 'object') { + // ������������������������������������������������������������ + if (test.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) { + // ���������������������������������������css������������������������������������������������ + const key = i.replace(/([A-Z])/g, '-$1').toLowerCase() + string += `${key}:${customStyle[i]};` + } + // ������������������ + return trim(string) +} + +/** + * @description ������������������������rpx���upx���%���px���������������������������auto������������������������������px������������ + * @param {string|number} value ������������������������ + * @param {string} unit ������������������ ������px + */ +function addUnit(value = 'auto', unit = uni?.$u?.config?.unit ?? 'px') { + value = String(value) + // ���uView������������������������number��������������������� + return test.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} ������������������������������false��������������������������� + */ +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(`uView���������${err}`) + } +} + +/** + * @description ������������ + * @param {array} array ��������������������� + * @returns {array} ������������������ + */ +function randomArray(array = []) { + // ���������sort������,Math.random()������0<= x < 1������������,���������x-0.05������������������0 + return array.sort(() => Math.random() - 0.5) +} + +// padStart ��� polyfill������������������������������������������������es7���padStart������������������������������������ +// ���������������������������polyfill��������������� +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���mm���dd���|yyyy���mm���dd��� hh���MM������,������������������ ������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��������������������������� + else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) { + date = new Date(Number(dateTime)) + } + // ���������������������������Safari/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 + * ��������������������������������������������������������������������������������������������������������� + * ������������������false������������������������������������������������������ + * @returns {string} ������������������ + */ +function timeFrom(timestamp = null, format = 'yyyy-mm-dd') { + if (timestamp == null) timestamp = Number(new Date()) + timestamp = parseInt(timestamp) + // ������������������������������������������������,������������js���������������������������(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���false���������������������������������������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 ���������url������ + * @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) { + // ������������������,���������success + if (['primary', 'info', 'error', 'warning', 'success'].indexOf(type) == -1) type = 'success' + let iconName = '' + // ������(2019-12-12),info���primary��������������������� + 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,���icon������������,���������������������������-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������������������������������������������������ms������������������������������������s������ + * ���������30������������������300������30������������������������������������300ms������������������300s��������������������� + * @param {String|number} value ������: "1s"|"100ms"|1|100 + * @param {boolean} unit ������: ���������false ������������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���������������������������������������������������������������������u-form������������������ + * @param {*} instance + * @param {*} event + */ +function formValidate(instance, event) { + const formItem = uni.$u.$parent.call(instance, 'u-form-item') + const form = uni.$u.$parent.call(instance, 'u-form') + // ���������������������input������textarea������������������������u-form-item������u-form���������������form���validate������ + // ���������form-item���pros���������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) { + // ������������������key + if (keys.length === 1) { + _obj[keys[0]] = v + return + } + // 0~length-1���key + 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() + // ���������������������(������������������redirectTo������������������)���pages������������������ + return `/${pages[pages.length - 1]?.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 ������uView��������������� + * @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.$u + uni.$u.config = deepMerge(uni.$u.config, config) + uni.$u.props = deepMerge(uni.$u.props, props) + uni.$u.color = deepMerge(uni.$u.color, color) + uni.$u.zIndex = deepMerge(uni.$u.zIndex, zIndex) +} + +export default { + 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 +} -- Gitblit v1.8.0