utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { prefix } from './config';
  2. const systemInfo = wx.getSystemInfoSync();
  3. export const debounce = function (func, wait = 500) {
  4. let timerId;
  5. return function (...rest) {
  6. if (timerId) {
  7. clearTimeout(timerId);
  8. }
  9. timerId = setTimeout(() => {
  10. func.apply(this, rest);
  11. }, wait);
  12. };
  13. };
  14. export const throttle = (func, wait = 100, options = null) => {
  15. let previous = 0;
  16. let timerid = null;
  17. if (!options) {
  18. options = {
  19. leading: true,
  20. };
  21. }
  22. return function (...args) {
  23. const now = Date.now();
  24. if (!previous && !options.leading)
  25. previous = now;
  26. const remaining = wait - (now - previous);
  27. const context = this;
  28. if (remaining <= 0) {
  29. if (timerid) {
  30. clearTimeout(timerid);
  31. timerid = null;
  32. }
  33. previous = now;
  34. func.apply(context, args);
  35. }
  36. };
  37. };
  38. export const classNames = function (...args) {
  39. const hasOwn = {}.hasOwnProperty;
  40. const classes = [];
  41. args.forEach((arg) => {
  42. if (!arg)
  43. return;
  44. const argType = typeof arg;
  45. if (argType === 'string' || argType === 'number') {
  46. classes.push(arg);
  47. }
  48. else if (Array.isArray(arg) && arg.length) {
  49. const inner = classNames(...arg);
  50. if (inner) {
  51. classes.push(inner);
  52. }
  53. }
  54. else if (argType === 'object') {
  55. for (const key in arg) {
  56. if (hasOwn.call(arg, key) && arg[key]) {
  57. classes.push(key);
  58. }
  59. }
  60. }
  61. });
  62. return classes.join(' ');
  63. };
  64. export const styles = function (styleObj) {
  65. return Object.keys(styleObj)
  66. .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`)
  67. .join('; ');
  68. };
  69. export const getAnimationFrame = function (context, cb) {
  70. return wx
  71. .createSelectorQuery()
  72. .in(context)
  73. .selectViewport()
  74. .boundingClientRect()
  75. .exec(() => {
  76. cb();
  77. });
  78. };
  79. export const getRect = function (context, selector, needAll = false) {
  80. return new Promise((resolve, reject) => {
  81. wx.createSelectorQuery()
  82. .in(context)[needAll ? 'selectAll' : 'select'](selector)
  83. .boundingClientRect((rect) => {
  84. if (rect) {
  85. resolve(rect);
  86. }
  87. else {
  88. reject(rect);
  89. }
  90. })
  91. .exec();
  92. });
  93. };
  94. const isDef = function (value) {
  95. return value !== undefined && value !== null;
  96. };
  97. export const isNumber = function (value) {
  98. return /^\d+(\.\d+)?$/.test(value);
  99. };
  100. export const addUnit = function (value) {
  101. if (!isDef(value)) {
  102. return undefined;
  103. }
  104. value = String(value);
  105. return isNumber(value) ? `${value}px` : value;
  106. };
  107. export const getCharacterLength = (type, str, max) => {
  108. if (!str || str.length === 0) {
  109. return {
  110. length: 0,
  111. characters: '',
  112. };
  113. }
  114. if (type === 'maxcharacter') {
  115. let len = 0;
  116. for (let i = 0; i < str.length; i += 1) {
  117. let currentStringLength = 0;
  118. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  119. currentStringLength = 2;
  120. }
  121. else {
  122. currentStringLength = 1;
  123. }
  124. if (len + currentStringLength > max) {
  125. return {
  126. length: len,
  127. characters: str.slice(0, i),
  128. };
  129. }
  130. len += currentStringLength;
  131. }
  132. return {
  133. length: len,
  134. characters: str,
  135. };
  136. }
  137. else if (type === 'maxlength') {
  138. const length = str.length > max ? max : str.length;
  139. return {
  140. length,
  141. characters: str.slice(0, length),
  142. };
  143. }
  144. return {
  145. length: str.length,
  146. characters: str,
  147. };
  148. };
  149. export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
  150. export const getInstance = function (context, selector) {
  151. if (!context) {
  152. const pages = getCurrentPages();
  153. const page = pages[pages.length - 1];
  154. context = page.$$basePage || page;
  155. }
  156. const instance = context ? context.selectComponent(selector) : null;
  157. if (!instance) {
  158. console.warn('未找到组件,请检查selector是否正确');
  159. return null;
  160. }
  161. return instance;
  162. };
  163. export const unitConvert = (value) => {
  164. var _a;
  165. if (typeof value === 'string') {
  166. if (value.includes('rpx')) {
  167. return (parseInt(value, 10) * ((_a = systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.screenWidth) !== null && _a !== void 0 ? _a : 750)) / 750;
  168. }
  169. return parseInt(value, 10);
  170. }
  171. return value;
  172. };
  173. export const setIcon = (iconName, icon, defaultIcon) => {
  174. if (icon) {
  175. if (typeof icon === 'string') {
  176. return {
  177. [`${iconName}Name`]: icon,
  178. [`${iconName}Data`]: {},
  179. };
  180. }
  181. else if (typeof icon === 'object') {
  182. return {
  183. [`${iconName}Name`]: '',
  184. [`${iconName}Data`]: icon,
  185. };
  186. }
  187. else {
  188. return {
  189. [`${iconName}Name`]: defaultIcon,
  190. [`${iconName}Data`]: {},
  191. };
  192. }
  193. }
  194. return {
  195. [`${iconName}Name`]: '',
  196. [`${iconName}Data`]: {},
  197. };
  198. };
  199. export const isBool = (val) => typeof val === 'boolean';
  200. export const isObject = (val) => typeof val === 'object' && val != null;
  201. export const isString = (val) => typeof val === 'string';
  202. export const toCamel = (str) => str.replace(/-(\w)/g, (match, m1) => m1.toUpperCase());
  203. export const getCurrentPage = function () {
  204. const pages = getCurrentPages();
  205. return pages[pages.length - 1];
  206. };
  207. export const uniqueFactory = (compName) => {
  208. let number = 0;
  209. return () => `${prefix}_${compName}_${number++}`;
  210. };
  211. export const calcIcon = (icon, defaultIcon) => {
  212. if ((isBool(icon) && icon && defaultIcon) || isString(icon)) {
  213. return { name: isBool(icon) ? defaultIcon : icon };
  214. }
  215. if (isObject(icon)) {
  216. return icon;
  217. }
  218. return null;
  219. };
  220. export const isOverSize = (size, sizeLimit) => {
  221. var _a;
  222. if (!sizeLimit)
  223. return false;
  224. const base = 1000;
  225. const unitMap = {
  226. B: 1,
  227. KB: base,
  228. MB: base * base,
  229. GB: base * base * base,
  230. };
  231. 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'];
  232. return size > computedSize;
  233. };