| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <script setup>
- import storage from "@/utils/storage.js";
- import privacyUtil from "@/utils/privacy.js";
- import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
- import { useStore } from 'vuex';
- import { getCurrentInstance } from 'vue';
- const store = useStore();
- const instance = getCurrentInstance();
- // 登录状态检查函数
- const checkLoginStatus = () => {
- // 登录状态检查,对敏感页面进行拦截
- const pages = ['pages/device/index'];
-
- uni.addInterceptor('navigateTo', {
- invoke(e) {
- const url = e.url;
- // 检查是否属于需要登录的页面
- const needLogin = pages.some(page => url.indexOf(page) > -1);
-
- if (needLogin && !storage.isLoggedIn()) {
- uni.navigateTo({
- url: '/pages/login/index'
- });
- return false;
- }
- return true;
- }
- });
-
- uni.addInterceptor('switchTab', {
- invoke(e) {
- const url = e.url;
- // 检查是否属于需要登录的页面
- const needLogin = pages.some(page => url.indexOf(page) > -1);
-
- if (needLogin && !storage.isLoggedIn()) {
- uni.navigateTo({
- url: '/pages/login/index'
- });
- return false;
- }
- return true;
- }
- });
- };
- // 初始化其他插件(uview-plus 已在 main.js 中注册)
- const initPlugins = async () => {
- try {
- // #ifdef H5
- // H5 环境加载 jessibuca 插件
- if (typeof window !== 'undefined') {
- const jessibucaModule = await import('./utils/jessibuca-plugin');
- if (instance && instance.appContext && instance.appContext.app) {
- instance.appContext.app.use(jessibucaModule.default);
- }
- }
- // #endif
- } catch (error) {
- console.error('插件初始化失败:', error);
- }
- };
- // 显示隐私协议弹窗
- const showPrivacyDialog = () => {
- // 检查是否需要显示隐私协议(适配多端)
- if (privacyUtil.shouldShowAgreement()) {
- console.log("未同意隐私协议,跳转到隐私协议页面");
- uni.reLaunch({
- url: '/pages/privacy/privacy-agreement'
- });
- return false; // 返回false表示需要先同意隐私协议
- }
-
- return true; // 已同意隐私协议
- };
- // uni-app 生命周期钩子 (保持原有命名)
- onLaunch(() => {
- // console.log('App Launch');
-
- // 首先检查隐私协议同意状态
- const privacyAgreed = showPrivacyDialog();
-
- // 只有在同意隐私协议后才初始化其他功能
- if (privacyAgreed) {
- // 初始化插件
- initPlugins();
- // 初始化 store 状态
- store.dispatch('init');
- // 设置登录状态检查
- checkLoginStatus();
- }
- });
- onShow(() => {
- console.log('App Show');
-
- // 检查隐私协议状态,如果已同意但还未初始化,则进行初始化
- if (privacyUtil.checkAgreement()) {
- // 确保插件和store已初始化
- initPlugins();
- store.dispatch('init');
- checkLoginStatus();
- }
- });
- onHide(() => {
- console.log('App Hide');
- });
- </script>
- <!-- App.vue中不需要template,uni-app会自动处理页面跳转 -->
- <style lang="scss">
- /* 全局基础样式 */
- @import "uview-plus/index.scss";
- page {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
- font-size: 28rpx;
- line-height: 1.5;
- color: #333;
- background-color: #f5f5f5;
- }
- /* 去除按钮默认边框 */
- button::after {
- border: none;
- }
- /* 隐藏滚动条 */
- ::-webkit-scrollbar {
- width: 0;
- height: 0;
- color: transparent;
- }
- /* H5环境特殊样式 */
- /* #ifdef H5 */
- html, body {
- height: 100%;
- width: 100%;
- overflow-x: hidden;
- }
- .uni-page-head {
- display: flex !important;
- }
- /* #endif */
- </style>
|