zhengyiming
1 天以前 1327b99efda99ada27a956981648950f580314b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Taro from '@tarojs/taro';
import { defineStore } from 'pinia';
import { store } from '@/stores';
import { isWeb } from '@/utils/env';
 
export interface Options {
  path: string;
  scene: number;
  query: object;
  shareTicket: string;
  referrerInfo: object;
  tabIndex: number;
  IPhoneXPadding: number;
  navHeight: number;
  safeAreaTop: number;
  buttonBottom: number;
 
  redirectPath: string;
  isTabSwitch: boolean;
  isFirstEnter: boolean;
}
 
export const useSystemStore = defineStore({
  id: 'app-system',
  /** 状态 */
  state: () => ({
    options: {} as Options,
 
    activeTab: 0,
 
    info: {} as Taro.getSystemInfoSync.Result,
 
    designWidth: 750,
 
    screenHeight: 0,
    screenWidth: 0,
    pageHeightWithTab: 0,
    pageHeight: 0,
 
    IPhoneXPadding: 0,
 
    navHeight: 0,
    bottomNavHeight: 0,
 
    safeAreaTop: 0,
 
    navigationBarHeight: 0,
    //右上角胶囊按钮高度
    menuButtonHeight: 0,
    menuButtonWidth: 0,
    menuButtonRightDistance: 0,
    menuButtonTop: 0,
 
    isFirstEnter: true,
  }),
 
  actions: {
    init(options: Options) {
      this.options = options; // 放一下页面相关的数据例如scene等 页面高度之类的不存!
    },
 
    setInfo(info: Taro.getSystemInfoSync.Result) {
      this.info = info;
      console.log('info: ', info);
 
      this.setNavigationBarHeight(info);
 
      this.screenHeight = info.screenHeight;
      this.screenWidth = info.screenWidth;
 
      this.IPhoneXPadding = info.screenHeight - info.safeArea.bottom;
      this.navHeight = (info.statusBarHeight || 0) + this.navigationBarHeight;
 
      this.safeAreaTop = info.safeArea.top;
 
      const scale = info.screenWidth / this.designWidth;
 
      /**
       * 100为底部tabber的高度
       */
      this.bottomNavHeight = 118 * scale;
 
      this.pageHeight = info.screenHeight - this.navHeight - (isWeb ? 0 : this.IPhoneXPadding); // 无底部tabber
      this.pageHeightWithTab = this.pageHeight - this.bottomNavHeight; // 有底部tabber
    },
 
    setNavigationBarHeight(systemInfo: Taro.getSystemInfoSync.Result) {
      try {
        if (!isWeb) {
          const { right, height, width, top } = Taro.getMenuButtonBoundingClientRect();
 
          this.menuButtonHeight = height;
          this.menuButtonWidth = width;
          this.menuButtonTop = top;
          this.menuButtonRightDistance = systemInfo.screenWidth - right;
        }
        let navigationBarHeight = 0;
 
        if (systemInfo.platform === 'android') {
          navigationBarHeight = 48;
        } else {
          navigationBarHeight = 44;
        }
        this.navigationBarHeight = navigationBarHeight;
      } catch (error) {}
    },
 
    setTabIndex(tabIndex: number) {
      this.activeTab = tabIndex;
    },
 
    setIsFirstEnter(isFirstEnter: boolean) {
      this.isFirstEnter = isFirstEnter;
    },
  },
});
 
// Need to be used outside the setup
export function useSystemStoreWithOut() {
  return useSystemStore(store);
}