uni.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* eslint-disable no-undef */
  2. import { isMpAlipay } from "./env";
  3. import { defaultTo } from "./helpers";
  4. export function getDeviceInfo() {
  5. if (uni.canIUse("getDeviceInfo") || uni.getDeviceInfo) {
  6. return uni.getDeviceInfo();
  7. } else {
  8. return uni.getSystemInfoSync();
  9. }
  10. }
  11. export function getWindowInfo() {
  12. if (uni.canIUse("getWindowInfo") || uni.getWindowInfo) {
  13. return uni.getWindowInfo();
  14. } else {
  15. return uni.getSystemInfoSync();
  16. }
  17. }
  18. export function getAppBaseInfo() {
  19. if (uni.canIUse("getAppBaseInfo") || uni.getAppBaseInfo) {
  20. return uni.getAppBaseInfo();
  21. } else {
  22. return uni.getSystemInfoSync();
  23. }
  24. }
  25. export function getVersion() {
  26. if (isMpAlipay) {
  27. return my.SDKVersion;
  28. }
  29. return getAppBaseInfo().SDKVersion;
  30. }
  31. export function compareVersion(v1, v2) {
  32. const s1 = v1.split(".");
  33. const s2 = v2.split(".");
  34. for (let i = 0; i < Math.max(s1.length, s2.length); i += 1) {
  35. const num1 = Number.parseInt(defaultTo(s1[i], "0"));
  36. const num2 = Number.parseInt(defaultTo(s2[i], "0"));
  37. if (num1 > num2) {
  38. return 1;
  39. } else if (num1 < num2) {
  40. return -1;
  41. }
  42. }
  43. return 0;
  44. }
  45. // function gte(version) {
  46. // return compareVersion(getVersion(), version) >= 0;
  47. // }
  48. export function querySelect(component, selector, fields) {
  49. return new Promise((resolve, reject) => {
  50. uni.createSelectorQuery()
  51. .in(component)
  52. .select(selector)
  53. .fields(fields, () => {})
  54. .exec(([node]) => {
  55. if (node) {
  56. resolve(node);
  57. } else {
  58. reject();
  59. }
  60. });
  61. });
  62. }