App.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <script setup>
  2. import { onLaunch, onShow, onHide, onUnload } from "@dcloudio/uni-app";
  3. import { store } from "@/store/index.js";
  4. import { useGlobalEventRemove, useInit, updateActivePageOnShow } from "@/utils/useBarcodeModule.js";
  5. import { useRouteMonitor } from "@/utils/useRouteMonitor.js";
  6. import { playClickSound } from "@/config/audioUtil.js";
  7. // 存储移动端触摸事件处理函数的引用
  8. let touchHandler;
  9. onLaunch(() => {
  10. // 尝试去获取本地的 token 和用户信息,填充到 store 中
  11. let token = uni.getStorageSync("token");
  12. if (token) {
  13. let userInfo = uni.getStorageSync("userInfo");
  14. if (userInfo) {
  15. if (userInfo.userId) {
  16. store.setUserInfo(userInfo);
  17. }
  18. }
  19. }
  20. // 移动端触摸事件处理函数
  21. touchHandler = (e) => {
  22. playClickSound();
  23. };
  24. // 添加移动端触摸事件监听 - 使用条件编译区分不同平台
  25. // #ifdef H5
  26. document.addEventListener("touchstart", touchHandler);
  27. // #endif
  28. // #ifdef APP-PLUS
  29. // 在APP环境中使用uni的API监听触摸事件
  30. var globalEvent = uni.requireNativePlugin("globalEvent");
  31. globalEvent.addEventListener("touchstart", touchHandler);
  32. // 初始化扫码模块
  33. useInit();
  34. // #endif
  35. });
  36. onShow(() => {});
  37. // 在应用销毁时移除点击事件监听
  38. onUnload(() => {
  39. // 移除移动端触摸事件监听 - 使用条件编译区分不同平台
  40. // #ifdef H5
  41. if (touchHandler) {
  42. document.removeEventListener("touchstart", touchHandler);
  43. touchHandler = null;
  44. }
  45. // #endif
  46. // #ifdef APP-PLUS
  47. if (touchHandler) {
  48. var globalEvent = uni.requireNativePlugin("globalEvent");
  49. globalEvent.removeEventListener("click", touchHandler);
  50. touchHandler = null;
  51. }
  52. // #endif
  53. });
  54. </script>
  55. <style lang="scss">
  56. @import "@/uni_modules/uview-plus/index.scss";
  57. @import "@/static/css/mystyle.css";
  58. @import "@/static/css/common.scss";
  59. /*每个页面公共css */
  60. page {
  61. background: #f5f6fa;
  62. height: 100%;
  63. }
  64. </style>