utils.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 autoSubcrebe() {
  9. return new Promise(resolve => {
  10. wx.getSetting({
  11. withSubscriptions: true,
  12. success: (setting) => {
  13. let hasAuto = false
  14. if (setting && setting.subscriptionsSetting && setting.subscriptionsSetting.itemSettings && setting.subscriptionsSetting.itemSettings[subId]) {
  15. subcribe()
  16. hasAuto = true
  17. }
  18. resolve(hasAuto)
  19. }
  20. })
  21. })
  22. }
  23. export function isDef(value) {
  24. return value !== undefined && value !== null;
  25. }
  26. export function isObj(x) {
  27. const type = typeof x;
  28. return x !== null && (type === 'object' || type === 'function');
  29. }
  30. export function isNumber(value) {
  31. return /^\d+(\.\d+)?$/.test(value);
  32. }
  33. export function range(num, min, max) {
  34. return Math.min(Math.max(num, min), max);
  35. }
  36. export function nextTick(fn) {
  37. setTimeout(() => {
  38. fn();
  39. }, 1000 / 30);
  40. }
  41. let systemInfo = null;
  42. export function getSystemInfoSync() {
  43. if (systemInfo == null) {
  44. systemInfo = wx.getSystemInfoSync();
  45. }
  46. return systemInfo;
  47. }
  48. export function addUnit(value) {
  49. if (!isDef(value)) {
  50. return undefined;
  51. }
  52. value = String(value);
  53. return isNumber(value) ? `${value}px` : value;
  54. }