App.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if (!store.token) {
  12. let token = uni.getStorageSync("token");
  13. if (token) {
  14. store.setToken(token);
  15. let userInfo = uni.getStorageSync("userInfo");
  16. if (userInfo) {
  17. if (userInfo.userId) {
  18. store.setUserInfo(userInfo);
  19. }
  20. }
  21. }
  22. }
  23. // 移动端触摸事件处理函数
  24. touchHandler = (e) => {
  25. playClickSound();
  26. };
  27. // 添加移动端触摸事件监听 - 使用条件编译区分不同平台
  28. // #ifdef H5
  29. document.addEventListener("touchstart", touchHandler);
  30. // #endif
  31. // #ifdef APP-PLUS
  32. // 在APP环境中使用uni的API监听触摸事件
  33. var globalEvent = uni.requireNativePlugin("globalEvent");
  34. globalEvent.addEventListener("touchstart", touchHandler);
  35. // 初始化扫码模块
  36. useInit();
  37. // #endif
  38. });
  39. onShow(() => {});
  40. // 在应用销毁时移除点击事件监听
  41. onUnload(() => {
  42. // 移除移动端触摸事件监听 - 使用条件编译区分不同平台
  43. // #ifdef H5
  44. if (touchHandler) {
  45. document.removeEventListener("touchstart", touchHandler);
  46. touchHandler = null;
  47. }
  48. // #endif
  49. // #ifdef APP-PLUS
  50. if (touchHandler) {
  51. var globalEvent = uni.requireNativePlugin("globalEvent");
  52. globalEvent.removeEventListener("click", touchHandler);
  53. touchHandler = null;
  54. }
  55. // #endif
  56. });
  57. </script>
  58. <style lang="scss">
  59. @import "@/uni_modules/uview-plus/index.scss";
  60. @import "@/static/css/mystyle.css";
  61. @import "@/static/css/common.scss";
  62. /*每个页面公共css */
  63. page {
  64. background: #f5f6fa;
  65. height: 100%;
  66. }
  67. </style>