wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<template>
    <view
        class="cl-page"
        :class="[
            `theme-${app.theme.name}`,
            {
                'is-fullscreen': fullscreen,
            },
        ]"
        :style="{
            padding: parseRpx(padding),
            height,
        }"
    >
        <!-- 加载框 -->
        <cl-loading-mask
            fullscreen
            :loading="loader.loading"
            :text="loader.text"
            :border="loader.border"
        />
 
        <!-- 提示 -->
        <cl-toast :ref="setRefs('toast')" />
 
        <!-- 确认框 -->
        <cl-confirm :ref="setRefs('confirm')" />
 
        <!-- 状态栏 -->
        <!-- #ifndef MP-ALIPAY -->
        <cl-status-bar v-if="statusBar" :background-color="statusBarBackground" />
        <!-- #endif -->
 
        <!-- 内容插槽 -->
        <slot></slot>
 
        <!-- 底部安全区域 -->
        <view class="safe-area-bottom" v-if="!fullscreen"></view>
    </view>
 
    <!-- 背景色 -->
    <view
        class="cl-page__bg"
        :style="{
            background,
        }"
    ></view>
</template>
 
<script lang="ts">
import { computed, defineComponent, reactive, getCurrentInstance, onMounted } from "vue";
import { useApp, useCool } from "/@/cool";
import { parseRpx } from "/@/cool/utils";
import { isString } from "lodash-es";
import { useI18n } from "vue-i18n";
 
export default defineComponent({
    name: "cl-page",
 
    props: {
        // 是否全屏显示,高度100%
        fullscreen: Boolean,
        // 內间距
        padding: {
            type: [Number, String],
            default: 0,
        },
        // 是否显示状态栏
        statusBar: {
            type: Boolean,
            default: true,
        },
        // 状态栏背景色
        statusBarBackground: String,
        // 页面背景色
        backgroundColor: String,
    },
 
    setup(props) {
        const { refs, setRefs, router } = useCool();
        const app = useApp();
        const info = router.info();
        const { t } = useI18n();
        const {
            statusBarHeight = 0,
            screenHeight,
            windowHeight,
            safeAreaInsets,
        } = uni.getSystemInfoSync();
 
        const { proxy }: any = getCurrentInstance();
 
        // 是否显示导航栏
        const statusBar = info?.isCustomNavbar ? props.statusBar : false;
 
        // 背景色
        const background = computed(() => {
            return (
                props.backgroundColor ||
                router.info()?.style?.backgroundColor ||
                router.globalStyle.backgroundColor ||
                "#ffffff"
            );
        });
 
        // 全屏高
        const height = computed(() => {
            if (!props.fullscreen) {
                return "auto";
            }
 
            let h = 0;
 
            // #ifdef H5
            h = windowHeight;
            // #endif
 
            // #ifndef H5
            h = screenHeight - statusBarHeight;
 
            if (!info?.isCustomNavbar) {
                h -= 44;
            }
            // #endif
 
            return h - (safeAreaInsets?.bottom || 0) + "px";
        });
 
        // 加载框配置
        const loader = reactive({
            loading: false,
            border: false,
            text: t("加载中"),
        });
 
        // 显示加载框
        function showLoading(options?: ClLoading.MaskOptions) {
            loader.loading = true;
 
            if (isString(options)) {
                loader.text = options;
            } else {
                Object.assign(loader, options);
            }
        }
 
        // 隐藏加载框
        function hideLoading() {
            loader.loading = false;
        }
 
        // 提示框
        function showToast(options: ClToast.Options) {
            refs.toast?.open(options);
        }
 
        // 确认框
        function showConfirm(options: ClConfirm.Options) {
            refs.confirm?.open(options);
        }
 
        // 提示框
        function showTips(message: string, callback?: () => void) {
            refs.confirm?.open({
                title: t("提示"),
                message,
                showCancelButton: false,
                callback,
            } as ClConfirm.Options);
        }
 
        // 扩展
        const page = router.currentPage();
 
        if (page) {
            page["cl-page"] = {
                loaded: false,
                showLoading,
                hideLoading,
                showToast,
                showConfirm,
                showTips,
            };
 
            onMounted(() => {
                page["cl-page"].loaded = true;
            });
        }
 
        // 滚动事件
        proxy.$root.scrollTo = (top: number) => {
            // 减去自定义导航栏高度
            top -= info?.isCustomNavbar ? statusBarHeight : 0;
 
            // 减去间距
            if (props.padding) {
                const [t] = parseRpx(props.padding).split(" ");
                top -= uni.upx2px(parseInt(t));
            }
 
            uni.createSelectorQuery()
                .in(proxy)
                .select(".cl-page")
                .boundingClientRect((a) => {
                    uni.createSelectorQuery()
                        .in(proxy)
                        .select(".safe-area-bottom")
                        .boundingClientRect((b) => {
                            const scrollTop = top + (a?.height || 0) - (b?.bottom || 0);
 
                            uni.pageScrollTo({
                                scrollTop,
                                duration: 100,
                            });
                        })
                        .exec();
                })
                .exec();
        };
 
        return {
            app,
            background,
            height,
            refs,
            setRefs,
            loader,
            parseRpx,
            statusBar,
        };
    },
});
</script>