useEnvStore.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {createGlobalState, useEventListener, useStorage} from "@vueuse/core";
  2. import {computed, ref} from "vue";
  3. import mxConst from "@/common/mxConst";
  4. import {sys} from "@/uni_modules/uv-ui-tools/libs/function";
  5. export const useEnvStore = createGlobalState(() => {
  6. // 平台判断,和调用bridge有关,如视频控制、下载等。
  7. const platform = ref(window.platform || "");
  8. const isH5 = computed(() => platform.value === "h5");
  9. const isWap2App = computed(() => platform.value === "wap2app");
  10. const systemInfo = computed(() => {
  11. const {
  12. platform,
  13. model,
  14. deviceId,
  15. deviceModel,
  16. deviceType,
  17. appId,
  18. version,
  19. appVersion,
  20. appVersionCode,
  21. } = sys();
  22. return {
  23. platform,
  24. model,
  25. deviceId,
  26. deviceModel,
  27. deviceType,
  28. appId,
  29. version,
  30. appVersion,
  31. appVersionCode,
  32. };
  33. });
  34. const isAndroid = computed(() => systemInfo.value.platform.toLowerCase() == 'android')
  35. const isIOS = computed(() => systemInfo.value.platform.toLowerCase() == 'ios')
  36. // TODO: isParent 表示这个应用是从微信公众号启动的。
  37. // 但这个判断受限于调用时机,不如上面的 platform 有一定封装来保证和运行环境契合。
  38. const isParent = ref(
  39. window?.location?.href.includes(mxConst.keyParentIdentifier)
  40. );
  41. // 引导页已读标志。
  42. const isGuideRead = useStorage(mxConst.keyGuideRead, false);
  43. // 访问相册权限初次提示标志
  44. const isDisplayedPermission = useStorage(
  45. mxConst.keyDisplayPermission,
  46. false
  47. );
  48. // hook
  49. useEventListener(window, "platformChange", (evt) => {
  50. // @/index.html 中定义的 window.platform 会在不同环境下被赋值,此时需要更新 platform 状态
  51. // 我们在 setPlatform 中暴露了 platformChange 事件用来让 vue 组件监听平台变化
  52. platform.value = window?.platform || "";
  53. });
  54. return {
  55. platform,
  56. isH5,
  57. isWap2App,
  58. isParent,
  59. isGuideRead,
  60. isDisplayedPermission,
  61. systemInfo,
  62. isAndroid,
  63. isIOS
  64. };
  65. });