utils.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 (cb) {
  70. return wx
  71. .createSelectorQuery()
  72. .selectViewport()
  73. .boundingClientRect()
  74. .exec(() => {
  75. cb();
  76. });
  77. };
  78. export const getRect = function (context, selector, needAll = false) {
  79. return new Promise((resolve, reject) => {
  80. wx.createSelectorQuery()
  81. .in(context)[needAll ? 'selectAll' : 'select'](selector)
  82. .boundingClientRect((rect) => {
  83. if (rect) {
  84. resolve(rect);
  85. }
  86. else {
  87. reject(rect);
  88. }
  89. })
  90. .exec();
  91. });
  92. };
  93. const isDef = function (value) {
  94. return value !== undefined && value !== null;
  95. };
  96. export const isNumber = function (value) {
  97. return /^\d+(\.\d+)?$/.test(value);
  98. };
  99. export const addUnit = function (value) {
  100. if (!isDef(value)) {
  101. return undefined;
  102. }
  103. value = String(value);
  104. return isNumber(value) ? `${value}px` : value;
  105. };
  106. export const getCharacterLength = (type, str, max) => {
  107. if (!str || str.length === 0) {
  108. return {
  109. length: 0,
  110. characters: '',
  111. };
  112. }
  113. if (type === 'maxcharacter') {
  114. let len = 0;
  115. for (let i = 0; i < str.length; i += 1) {
  116. let currentStringLength = 0;
  117. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  118. currentStringLength = 2;
  119. }
  120. else {
  121. currentStringLength = 1;
  122. }
  123. if (len + currentStringLength > max) {
  124. return {
  125. length: len,
  126. characters: str.slice(0, i),
  127. };
  128. }
  129. len += currentStringLength;
  130. }
  131. return {
  132. length: len,
  133. characters: str,
  134. };
  135. }
  136. else if (type === 'maxlength') {
  137. const length = str.length > max ? max : str.length;
  138. return {
  139. length,
  140. characters: str.slice(0, length),
  141. };
  142. }
  143. return {
  144. length: str.length,
  145. characters: str,
  146. };
  147. };
  148. export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
  149. export const getInstance = function (context, selector) {
  150. if (!context) {
  151. const pages = getCurrentPages();
  152. const page = pages[pages.length - 1];
  153. context = page.$$basePage || page;
  154. }
  155. const instance = context ? context.selectComponent(selector) : null;
  156. if (!instance) {
  157. console.warn('未找到组件,请检查selector是否正确');
  158. return null;
  159. }
  160. return instance;
  161. };
  162. export const unitConvert = (value) => {
  163. var _a;
  164. if (typeof value === 'string') {
  165. if (value.includes('rpx')) {
  166. return (parseInt(value, 10) * ((_a = systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.screenWidth) !== null && _a !== void 0 ? _a : 750)) / 750;
  167. }
  168. return parseInt(value, 10);
  169. }
  170. return value;
  171. };
  172. export const setIcon = (iconName, icon, defaultIcon) => {
  173. if (icon) {
  174. if (typeof icon === 'string') {
  175. return {
  176. [`${iconName}Name`]: icon,
  177. [`${iconName}Data`]: {},
  178. };
  179. }
  180. else if (typeof icon === 'object') {
  181. return {
  182. [`${iconName}Name`]: '',
  183. [`${iconName}Data`]: icon,
  184. };
  185. }
  186. else {
  187. return {
  188. [`${iconName}Name`]: defaultIcon,
  189. [`${iconName}Data`]: {},
  190. };
  191. }
  192. }
  193. return {
  194. [`${iconName}Name`]: '',
  195. [`${iconName}Data`]: {},
  196. };
  197. };
  198. export const isBool = (val) => typeof val === 'boolean';
  199. export const isObject = (val) => typeof val === 'object' && val != null;
  200. export const isString = (val) => typeof val === 'string';
  201. export const toCamel = (str) => str.replace(/-(\w)/g, (match, m1) => m1.toUpperCase());
  202. export const getCurrentPage = function () {
  203. const pages = getCurrentPages();
  204. return pages[pages.length - 1];
  205. };
  206. export const uniqueFactory = (compName) => {
  207. let number = 0;
  208. return () => `${prefix}_${compName}_${number++}`;
  209. };
  210. export const calcIcon = (icon, defaultIcon) => {
  211. if ((isBool(icon) && icon && defaultIcon) || isString(icon)) {
  212. return { name: isBool(icon) ? defaultIcon : icon };
  213. }
  214. if (isObject(icon)) {
  215. return icon;
  216. }
  217. return null;
  218. };