utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. export function bindInput(event) {
  2. var obj = {},
  3. key = event.target.dataset['key'];
  4. obj[key] = event.detail.value;
  5. console.log(obj)
  6. this.setData(obj);
  7. }
  8. export function randomWord(randomFlag, min, max) {
  9. let str = "";
  10. let range = min;
  11. let arr = [
  12. "0",
  13. "1",
  14. "2",
  15. "3",
  16. "4",
  17. "5",
  18. "6",
  19. "7",
  20. "8",
  21. "9",
  22. "a",
  23. "b",
  24. "c",
  25. "d",
  26. "e",
  27. "f",
  28. "g",
  29. "h",
  30. "i",
  31. "j",
  32. "k",
  33. "l",
  34. "m",
  35. "n",
  36. "o",
  37. "p",
  38. "q",
  39. "r",
  40. "s",
  41. "t",
  42. "u",
  43. "v",
  44. "w",
  45. "x",
  46. "y",
  47. "z",
  48. "A",
  49. "B",
  50. "C",
  51. "D",
  52. "E",
  53. "F",
  54. "G",
  55. "H",
  56. "I",
  57. "J",
  58. "K",
  59. "L",
  60. "M",
  61. "N",
  62. "O",
  63. "P",
  64. "Q",
  65. "R",
  66. "S",
  67. "T",
  68. "U",
  69. "V",
  70. "W",
  71. "X",
  72. "Y",
  73. "Z",
  74. ];
  75. // 随机产生
  76. if (randomFlag) {
  77. range = Math.round(Math.random() * (max - min)) + min;
  78. }
  79. for (var i = 0; i < range; i++) {
  80. let pos = Math.round(Math.random() * (arr.length - 1));
  81. str += arr[pos];
  82. }
  83. return str;
  84. }
  85. export function autoSubcrebe() {
  86. return new Promise(resolve => {
  87. wx.getSetting({
  88. withSubscriptions: true,
  89. success: (setting) => {
  90. let hasAuto = false
  91. if (setting && setting.subscriptionsSetting && setting.subscriptionsSetting.itemSettings && setting.subscriptionsSetting.itemSettings[subId]) {
  92. subcribe()
  93. hasAuto = true
  94. }
  95. resolve(hasAuto)
  96. }
  97. })
  98. })
  99. }
  100. export function isDef(value) {
  101. return value !== undefined && value !== null;
  102. }
  103. export function isObj(x) {
  104. const type = typeof x;
  105. return x !== null && (type === 'object' || type === 'function');
  106. }
  107. export function isNumber(value) {
  108. return /^\d+(\.\d+)?$/.test(value);
  109. }
  110. export function range(num, min, max) {
  111. return Math.min(Math.max(num, min), max);
  112. }
  113. export function nextTick(fn) {
  114. setTimeout(() => {
  115. fn();
  116. }, 1000 / 30);
  117. }
  118. let systemInfo = null;
  119. export function getSystemInfoSync() {
  120. if (systemInfo == null) {
  121. systemInfo = wx.getSystemInfoSync();
  122. }
  123. return systemInfo;
  124. }
  125. export function addUnit(value) {
  126. if (!isDef(value)) {
  127. return undefined;
  128. }
  129. value = String(value);
  130. return isNumber(value) ? `${value}px` : value;
  131. }