| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <script setup>
- import { onLaunch, onShow, onHide, onUnload } from "@dcloudio/uni-app";
- import { store } from "@/store/index.js";
- import { useGlobalEventRemove, useInit } from "@/utils/useBarcodeModule.js";
- import { useRouteMonitor } from "@/utils/useRouteMonitor.js";
- import { playClickSound } from "@/config/audioUtil.js";
- // 存储移动端触摸事件处理函数的引用
- let touchHandler;
- onLaunch(() => {
- console.log("App Launch");
- // 尝试去获取本地的 token 和用户信息,填充到 store 中
- if (!store.token) {
- let token = uni.getStorageSync("token");
- if (token) {
- store.setToken(token);
- let userInfo = uni.getStorageSync("userInfo");
- if (userInfo) {
- if (userInfo.userId) {
- store.setUserInfo(userInfo);
- }
- }
- }
- }
- // 移动端触摸事件处理函数
- touchHandler = (e) => {
- console.log("touchHandler", e);
- playClickSound();
- };
- // 添加移动端触摸事件监听 - 使用条件编译区分不同平台
- // #ifdef H5
- document.addEventListener("touchstart", touchHandler);
- // #endif
- // #ifdef APP-PLUS
- // 在APP环境中使用uni的API监听触摸事件
- var globalEvent = uni.requireNativePlugin("globalEvent");
- globalEvent.addEventListener("touchstart", touchHandler);
- // 初始化扫码模块
- useInit();
- // #endif
- });
- onShow(() => {
- // #ifdef APP-PLUS
- // 页面显示时重新添加全局监听
- useRouteMonitor((url, from) => {
- console.log("onShow", url, from);
- // #ifdef APP-PLUS
- // 页面隐藏时移除全局监听
- useGlobalEventRemove();
- // #endif
- });
- // #endif
- });
- onHide(() => {
- console.log("App Hide");
- });
- // 在应用销毁时移除点击事件监听
- onUnload(() => {
- // 移除移动端触摸事件监听 - 使用条件编译区分不同平台
- // #ifdef H5
- if (touchHandler) {
- document.removeEventListener("touchstart", touchHandler);
- touchHandler = null;
- }
- // #endif
- // #ifdef APP-PLUS
- if (touchHandler) {
- var globalEvent = uni.requireNativePlugin("globalEvent");
- globalEvent.removeEventListener("click", touchHandler);
- touchHandler = null;
- }
- // #endif
- });
- </script>
- <style lang="scss">
- @import "@/uni_modules/uview-plus/index.scss";
- @import "@/static/css/mystyle.css";
- @import "@/static/css/common.scss";
- /*每个页面公共css */
- page {
- background: #f5f6fa;
- height: 100%;
- }
- </style>
|