合伙人运营小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 context
  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. context
  81. .createSelectorQuery()[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. export const isNumber = function (value) {
  94. return /^\d+(\.\d+)?$/.test(value);
  95. };
  96. export const isNull = function (value) {
  97. return value === null;
  98. };
  99. export const isUndefined = (value) => typeof value === 'undefined';
  100. export const isDef = function (value) {
  101. return !isUndefined(value) && !isNull(value);
  102. };
  103. export const addUnit = function (value) {
  104. if (!isDef(value)) {
  105. return undefined;
  106. }
  107. value = String(value);
  108. return isNumber(value) ? `${value}px` : value;
  109. };
  110. export const getCharacterLength = (type, char, max) => {
  111. const str = String(char !== null && char !== void 0 ? char : '');
  112. if (str.length === 0) {
  113. return {
  114. length: 0,
  115. characters: '',
  116. };
  117. }
  118. if (type === 'maxcharacter') {
  119. let len = 0;
  120. for (let i = 0; i < str.length; i += 1) {
  121. let currentStringLength = 0;
  122. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  123. currentStringLength = 2;
  124. }
  125. else {
  126. currentStringLength = 1;
  127. }
  128. if (len + currentStringLength > max) {
  129. return {
  130. length: len,
  131. characters: str.slice(0, i),
  132. };
  133. }
  134. len += currentStringLength;
  135. }
  136. return {
  137. length: len,
  138. characters: str,
  139. };
  140. }
  141. else if (type === 'maxlength') {
  142. const length = str.length > max ? max : str.length;
  143. return {
  144. length,
  145. characters: str.slice(0, length),
  146. };
  147. }
  148. return {
  149. length: str.length,
  150. characters: str,
  151. };
  152. };
  153. export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
  154. export const getInstance = function (context, selector) {
  155. if (!context) {
  156. const pages = getCurrentPages();
  157. const page = pages[pages.length - 1];
  158. context = page.$$basePage || page;
  159. }
  160. const instance = context ? context.selectComponent(selector) : null;
  161. if (!instance) {
  162. console.warn('未找到组件,请检查selector是否正确');
  163. return null;
  164. }
  165. return instance;
  166. };
  167. export const unitConvert = (value) => {
  168. var _a;
  169. if (typeof value === 'string') {
  170. if (value.includes('rpx')) {
  171. return (parseInt(value, 10) * ((_a = systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.screenWidth) !== null && _a !== void 0 ? _a : 750)) / 750;
  172. }
  173. return parseInt(value, 10);
  174. }
  175. return value !== null && value !== void 0 ? value : 0;
  176. };
  177. export const setIcon = (iconName, icon, defaultIcon) => {
  178. if (icon) {
  179. if (typeof icon === 'string') {
  180. return {
  181. [`${iconName}Name`]: icon,
  182. [`${iconName}Data`]: {},
  183. };
  184. }
  185. else if (typeof icon === 'object') {
  186. return {
  187. [`${iconName}Name`]: '',
  188. [`${iconName}Data`]: icon,
  189. };
  190. }
  191. else {
  192. return {
  193. [`${iconName}Name`]: defaultIcon,
  194. [`${iconName}Data`]: {},
  195. };
  196. }
  197. }
  198. return {
  199. [`${iconName}Name`]: '',
  200. [`${iconName}Data`]: {},
  201. };
  202. };
  203. export const isBool = (val) => typeof val === 'boolean';
  204. export const isObject = (val) => typeof val === 'object' && val != null;
  205. export const isString = (val) => typeof val === 'string';
  206. export const toCamel = (str) => str.replace(/-(\w)/g, (match, m1) => m1.toUpperCase());
  207. export const getCurrentPage = function () {
  208. const pages = getCurrentPages();
  209. return pages[pages.length - 1];
  210. };
  211. export const uniqueFactory = (compName) => {
  212. let number = 0;
  213. return () => `${prefix}_${compName}_${number++}`;
  214. };
  215. export const calcIcon = (icon, defaultIcon) => {
  216. if ((isBool(icon) && icon && defaultIcon) || isString(icon)) {
  217. return { name: isBool(icon) ? defaultIcon : icon };
  218. }
  219. if (isObject(icon)) {
  220. return icon;
  221. }
  222. return null;
  223. };
  224. export const isOverSize = (size, sizeLimit) => {
  225. var _a;
  226. if (!sizeLimit)
  227. return false;
  228. const base = 1000;
  229. const unitMap = {
  230. B: 1,
  231. KB: base,
  232. MB: base * base,
  233. GB: base * base * base,
  234. };
  235. 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'];
  236. return size > computedSize;
  237. };
  238. export const rpx2px = (rpx) => Math.floor((systemInfo.windowWidth * rpx) / 750);