utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isWxWork = exports.isPC = exports.getCurrentPage = exports.clamp = exports.addNumber = exports.toPromise = exports.groupSetData = exports.getAllRect = exports.getRect = exports.pickExclude = exports.requestAnimationFrame = exports.addUnit = exports.nextTick = exports.range = exports.getSystemInfoSync = exports.isDef = void 0;
  4. var validator_1 = require("./validator");
  5. var version_1 = require("./version");
  6. var validator_2 = require("./validator");
  7. Object.defineProperty(exports, "isDef", { enumerable: true, get: function () { return validator_2.isDef; } });
  8. var version_2 = require("./version");
  9. Object.defineProperty(exports, "getSystemInfoSync", { enumerable: true, get: function () { return version_2.getSystemInfoSync; } });
  10. function range(num, min, max) {
  11. return Math.min(Math.max(num, min), max);
  12. }
  13. exports.range = range;
  14. function nextTick(cb) {
  15. if ((0, version_1.canIUseNextTick)()) {
  16. wx.nextTick(cb);
  17. }
  18. else {
  19. setTimeout(function () {
  20. cb();
  21. }, 1000 / 30);
  22. }
  23. }
  24. exports.nextTick = nextTick;
  25. function addUnit(value) {
  26. if (!(0, validator_1.isDef)(value)) {
  27. return undefined;
  28. }
  29. value = String(value);
  30. return (0, validator_1.isNumber)(value) ? "".concat(value, "px") : value;
  31. }
  32. exports.addUnit = addUnit;
  33. function requestAnimationFrame(cb) {
  34. return setTimeout(function () {
  35. cb();
  36. }, 1000 / 30);
  37. }
  38. exports.requestAnimationFrame = requestAnimationFrame;
  39. function pickExclude(obj, keys) {
  40. if (!(0, validator_1.isPlainObject)(obj)) {
  41. return {};
  42. }
  43. return Object.keys(obj).reduce(function (prev, key) {
  44. if (!keys.includes(key)) {
  45. prev[key] = obj[key];
  46. }
  47. return prev;
  48. }, {});
  49. }
  50. exports.pickExclude = pickExclude;
  51. function getRect(context, selector) {
  52. return new Promise(function (resolve) {
  53. wx.createSelectorQuery()
  54. .in(context)
  55. .select(selector)
  56. .boundingClientRect()
  57. .exec(function (rect) {
  58. if (rect === void 0) { rect = []; }
  59. return resolve(rect[0]);
  60. });
  61. });
  62. }
  63. exports.getRect = getRect;
  64. function getAllRect(context, selector) {
  65. return new Promise(function (resolve) {
  66. wx.createSelectorQuery()
  67. .in(context)
  68. .selectAll(selector)
  69. .boundingClientRect()
  70. .exec(function (rect) {
  71. if (rect === void 0) { rect = []; }
  72. return resolve(rect[0]);
  73. });
  74. });
  75. }
  76. exports.getAllRect = getAllRect;
  77. function groupSetData(context, cb) {
  78. if ((0, version_1.canIUseGroupSetData)()) {
  79. context.groupSetData(cb);
  80. }
  81. else {
  82. cb();
  83. }
  84. }
  85. exports.groupSetData = groupSetData;
  86. function toPromise(promiseLike) {
  87. if ((0, validator_1.isPromise)(promiseLike)) {
  88. return promiseLike;
  89. }
  90. return Promise.resolve(promiseLike);
  91. }
  92. exports.toPromise = toPromise;
  93. // 浮点数精度处理
  94. function addNumber(num1, num2) {
  95. var cardinal = Math.pow(10, 10);
  96. return Math.round((num1 + num2) * cardinal) / cardinal;
  97. }
  98. exports.addNumber = addNumber;
  99. // 限制value在[min, max]之间
  100. var clamp = function (num, min, max) { return Math.min(Math.max(num, min), max); };
  101. exports.clamp = clamp;
  102. function getCurrentPage() {
  103. var pages = getCurrentPages();
  104. return pages[pages.length - 1];
  105. }
  106. exports.getCurrentPage = getCurrentPage;
  107. exports.isPC = ['mac', 'windows'].includes((0, version_1.getSystemInfoSync)().platform);
  108. // 是否企业微信
  109. exports.isWxWork = (0, version_1.getSystemInfoSync)().environment === 'wxwork';