| | |
| | | import { prefix } from './config'; |
| | | const systemInfo = wx.getSystemInfoSync(); |
| | | export const debounce = function (func, wait = 500) { |
| | | let timerId; |
| | | return function (...rest) { |
| | | if (timerId) { |
| | | clearTimeout(timerId); |
| | | } |
| | | timerId = setTimeout(() => { |
| | | func.apply(this, rest); |
| | | }, wait); |
| | | }; |
| | | }; |
| | | export const throttle = (func, wait = 100, options = null) => { |
| | | let previous = 0; |
| | | let timerid = null; |
| | | if (!options) { |
| | | options = { |
| | | leading: true, |
| | | }; |
| | | } |
| | | return function (...args) { |
| | | const now = Date.now(); |
| | | if (!previous && !options.leading) |
| | | previous = now; |
| | | const remaining = wait - (now - previous); |
| | | const context = this; |
| | | if (remaining <= 0) { |
| | | if (timerid) { |
| | | clearTimeout(timerid); |
| | | timerid = null; |
| | | } |
| | | previous = now; |
| | | func.apply(context, args); |
| | | } |
| | | }; |
| | | }; |
| | | export const classNames = function (...args) { |
| | | const hasOwn = {}.hasOwnProperty; |
| | | const classes = []; |
| | | args.forEach((arg) => { |
| | | if (!arg) |
| | | return; |
| | | const argType = typeof arg; |
| | | if (argType === 'string' || argType === 'number') { |
| | | classes.push(arg); |
| | | } |
| | | else if (Array.isArray(arg) && arg.length) { |
| | | const inner = classNames(...arg); |
| | | if (inner) { |
| | | classes.push(inner); |
| | | } |
| | | } |
| | | else if (argType === 'object') { |
| | | for (const key in arg) { |
| | | if (hasOwn.call(arg, key) && arg[key]) { |
| | | classes.push(key); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | return classes.join(' '); |
| | | }; |
| | | export const styles = function (styleObj) { |
| | | return Object.keys(styleObj) |
| | | .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`) |
| | | .join('; '); |
| | | }; |
| | | export const getAnimationFrame = function (context, cb) { |
| | | return wx |
| | | .createSelectorQuery() |
| | | .in(context) |
| | | .selectViewport() |
| | | .boundingClientRect() |
| | | .exec(() => { |
| | | cb(); |
| | | }); |
| | | }; |
| | | export const getRect = function (context, selector, needAll = false) { |
| | | return new Promise((resolve, reject) => { |
| | | wx.createSelectorQuery() |
| | | .in(context)[needAll ? 'selectAll' : 'select'](selector) |
| | | .boundingClientRect((rect) => { |
| | | if (rect) { |
| | | resolve(rect); |
| | | } |
| | | else { |
| | | reject(rect); |
| | | } |
| | | }) |
| | | .exec(); |
| | | }); |
| | | }; |
| | | const isDef = function (value) { |
| | | return value !== undefined && value !== null; |
| | | }; |
| | | export const isNumber = function (value) { |
| | | return /^\d+(\.\d+)?$/.test(value); |
| | | }; |
| | | export const addUnit = function (value) { |
| | | if (!isDef(value)) { |
| | | return undefined; |
| | | } |
| | | value = String(value); |
| | | return isNumber(value) ? `${value}px` : value; |
| | | }; |
| | | export const getCharacterLength = (type, str, max) => { |
| | | if (!str || str.length === 0) { |
| | | return { |
| | | length: 0, |
| | | characters: '', |
| | | }; |
| | | } |
| | | if (type === 'maxcharacter') { |
| | | let len = 0; |
| | | for (let i = 0; i < str.length; i += 1) { |
| | | let currentStringLength = 0; |
| | | if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) { |
| | | currentStringLength = 2; |
| | | } |
| | | else { |
| | | currentStringLength = 1; |
| | | } |
| | | if (len + currentStringLength > max) { |
| | | return { |
| | | length: len, |
| | | characters: str.slice(0, i), |
| | | }; |
| | | } |
| | | len += currentStringLength; |
| | | } |
| | | return { |
| | | length: len, |
| | | characters: str, |
| | | }; |
| | | } |
| | | else if (type === 'maxlength') { |
| | | const length = str.length > max ? max : str.length; |
| | | return { |
| | | length, |
| | | characters: str.slice(0, length), |
| | | }; |
| | | } |
| | | return { |
| | | length: str.length, |
| | | characters: str, |
| | | }; |
| | | }; |
| | | export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size)); |
| | | export const getInstance = function (context, selector) { |
| | | if (!context) { |
| | | const pages = getCurrentPages(); |
| | | const page = pages[pages.length - 1]; |
| | | context = page.$$basePage || page; |
| | | } |
| | | const instance = context ? context.selectComponent(selector) : null; |
| | | if (!instance) { |
| | | console.warn('未找到组件,请检查selector是否正确'); |
| | | return null; |
| | | } |
| | | return instance; |
| | | }; |
| | | export const unitConvert = (value) => { |
| | | var _a; |
| | | if (typeof value === 'string') { |
| | | if (value.includes('rpx')) { |
| | | return (parseInt(value, 10) * ((_a = systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.screenWidth) !== null && _a !== void 0 ? _a : 750)) / 750; |
| | | } |
| | | return parseInt(value, 10); |
| | | } |
| | | return value; |
| | | }; |
| | | export const setIcon = (iconName, icon, defaultIcon) => { |
| | | if (icon) { |
| | | if (typeof icon === 'string') { |
| | | return { |
| | | [`${iconName}Name`]: icon, |
| | | [`${iconName}Data`]: {}, |
| | | }; |
| | | } |
| | | else if (typeof icon === 'object') { |
| | | return { |
| | | [`${iconName}Name`]: '', |
| | | [`${iconName}Data`]: icon, |
| | | }; |
| | | } |
| | | else { |
| | | return { |
| | | [`${iconName}Name`]: defaultIcon, |
| | | [`${iconName}Data`]: {}, |
| | | }; |
| | | } |
| | | } |
| | | return { |
| | | [`${iconName}Name`]: '', |
| | | [`${iconName}Data`]: {}, |
| | | }; |
| | | }; |
| | | export const isBool = (val) => typeof val === 'boolean'; |
| | | export const isObject = (val) => typeof val === 'object' && val != null; |
| | | export const isString = (val) => typeof val === 'string'; |
| | | export const toCamel = (str) => str.replace(/-(\w)/g, (match, m1) => m1.toUpperCase()); |
| | | export const getCurrentPage = function () { |
| | | const pages = getCurrentPages(); |
| | | return pages[pages.length - 1]; |
| | | }; |
| | | export const uniqueFactory = (compName) => { |
| | | let number = 0; |
| | | return () => `${prefix}_${compName}_${number++}`; |
| | | }; |
| | | export const calcIcon = (icon, defaultIcon) => { |
| | | if ((isBool(icon) && icon && defaultIcon) || isString(icon)) { |
| | | return { name: isBool(icon) ? defaultIcon : icon }; |
| | | } |
| | | if (isObject(icon)) { |
| | | return icon; |
| | | } |
| | | return null; |
| | | }; |
| | | export const isOverSize = (size, sizeLimit) => { |
| | | var _a; |
| | | if (!sizeLimit) |
| | | return false; |
| | | const base = 1000; |
| | | const unitMap = { |
| | | B: 1, |
| | | KB: base, |
| | | MB: base * base, |
| | | GB: base * base * base, |
| | | }; |
| | | const computedSize = typeof sizeLimit === 'number' ? sizeLimit * base : (sizeLimit === null || sizeLimit === void 0 ? void 0 : sizeLimit.size) * unitMap[(_a = sizeLimit === null || sizeLimit === void 0 ? void 0 : sizeLimit.unit) !== null && _a !== void 0 ? _a : 'KB']; |
| | | return size > computedSize; |
| | | }; |
| | | import{prefix}from"./config";import{isString,isNumber,isDef,isBoolean,isObject}from"./validator";import{getWindowInfo,getAppBaseInfo,getDeviceInfo}from"./wechat";export const systemInfo=getWindowInfo();export const appBaseInfo=getAppBaseInfo();export const deviceInfo=getDeviceInfo();export const debounce=function(e,t=500){let n;return function(...o){n&&clearTimeout(n),n=setTimeout(()=>{e.apply(this,o)},t)}};export const throttle=(e,t=100,n=null)=>{let o=0,r=null;return n||(n={leading:!0}),function(...c){const s=Date.now();o||n.leading||(o=s);const i=this;t-(s-o)<=0&&(r&&(clearTimeout(r),r=null),o=s,e.apply(i,c))}};export const classNames=function(...e){const t={}.hasOwnProperty,n=[];return e.forEach(e=>{if(!e)return;const o=typeof e;if("string"===o||"number"===o)n.push(e);else if(Array.isArray(e)&&e.length){const t=classNames(...e);t&&n.push(t)}else if("object"===o)for(const o in e)t.call(e,o)&&e[o]&&n.push(o)}),n.join(" ")};export const styles=function(e){return Object.keys(e).map(t=>`${t}: ${e[t]}`).join("; ")};export const getAnimationFrame=function(e,t){return e.createSelectorQuery().selectViewport().boundingClientRect().exec(()=>{t()})};export const getRect=function(e,t,n=!1){return new Promise((o,r)=>{e.createSelectorQuery()[n?"selectAll":"select"](t).boundingClientRect(e=>{e?o(e):r(e)}).exec()})};export const getTreeDepth=(e,t)=>e.reduce((e,n)=>n[null!=t?t:"children"]&&n[null!=t?t:"children"].length>0?Math.max(e,getTreeDepth(n[null!=t?t:"children"],t)+1):Math.max(e,1),0);export const isIOS=function(){var e;return!!((null===(e=null==deviceInfo?void 0:deviceInfo.system)||void 0===e?void 0:e.toLowerCase().search("ios"))+1)};export const addUnit=function(e){if(isDef(e))return e=String(e),isNumber(e)?`${e}px`:e};export const getCharacterLength=(e,t,n)=>{const o=String(null!=t?t:"");if(0===o.length)return{length:0,characters:""};if("maxcharacter"===e){let e=0;for(let t=0;t<o.length;t+=1){let r=0;if(r=o.charCodeAt(t)>127||94===o.charCodeAt(t)?2:1,e+r>n)return{length:e,characters:o.slice(0,t)};e+=r}return{length:e,characters:o}}if("maxlength"===e){const e=o.length>n?n:o.length;return{length:e,characters:o.slice(0,e)}}return{length:o.length,characters:o}};export const chunk=(e,t)=>Array.from({length:Math.ceil(e.length/t)},(n,o)=>e.slice(o*t,o*t+t));export const getInstance=function(e,t){if(!e){const t=getCurrentPages(),n=t[t.length-1];e=n.$$basePage||n}const n=e?e.selectComponent(t):null;return n||(console.warn("未找到组件,请检查selector是否正确"),null)};export const unitConvert=e=>{var t;return"string"==typeof e?e.includes("rpx")?parseInt(e,10)*(null!==(t=null==systemInfo?void 0:systemInfo.screenWidth)&&void 0!==t?t:750)/750:parseInt(e,10):null!=e?e:0};export const setIcon=(e,t,n)=>t?"string"==typeof t?{[`${e}Name`]:t,[`${e}Data`]:{}}:"object"==typeof t?{[`${e}Name`]:"",[`${e}Data`]:t}:{[`${e}Name`]:n,[`${e}Data`]:{}}:{[`${e}Name`]:"",[`${e}Data`]:{}};export const toCamel=e=>e.replace(/-(\w)/g,(e,t)=>t.toUpperCase());export const getCurrentPage=function(){const e=getCurrentPages();return e[e.length-1]};export const uniqueFactory=e=>{let t=0;return()=>{const n=`${prefix}_${e}_${t}`;return t+=1,n}};export const calcIcon=(e,t)=>e&&(isBoolean(e)&&t||isString(e))?{name:isBoolean(e)?t:e}:isObject(e)?e:null;export const isOverSize=(e,t)=>{var n;if(!t)return!1;const o=1e3,r={B:1,KB:o,MB:1e6,GB:1e9};return e>("number"==typeof t?t*o:(null==t?void 0:t.size)*r[null!==(n=null==t?void 0:t.unit)&&void 0!==n?n:"KB"])};export const rpx2px=e=>Math.floor(systemInfo.windowWidth*e/750);export const nextTick=()=>new Promise(e=>{wx.nextTick(()=>{e()})}); |