App.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <script setup>
  2. import { onLaunch, onShow, onHide, onUnload } from "@dcloudio/uni-app";
  3. import { store } from "@/store/index.js";
  4. import { useGlobalEventRemove, useInit } 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. console.log("App Launch");
  11. // 尝试去获取本地的 token 和用户信息,填充到 store 中
  12. if (!store.token) {
  13. let token = uni.getStorageSync("token");
  14. if (token) {
  15. store.setToken(token);
  16. let userInfo = uni.getStorageSync("userInfo");
  17. if (userInfo) {
  18. if (userInfo.userId) {
  19. store.setUserInfo(userInfo);
  20. }
  21. }
  22. }
  23. }
  24. // 移动端触摸事件处理函数
  25. touchHandler = (e) => {
  26. console.log("touchHandler", e);
  27. playClickSound();
  28. };
  29. // 添加移动端触摸事件监听 - 使用条件编译区分不同平台
  30. // #ifdef H5
  31. document.addEventListener("touchstart", touchHandler);
  32. // #endif
  33. // #ifdef APP-PLUS
  34. // 在APP环境中使用uni的API监听触摸事件
  35. var globalEvent = uni.requireNativePlugin("globalEvent");
  36. globalEvent.addEventListener("touchstart", touchHandler);
  37. // 初始化扫码模块
  38. useInit();
  39. // #endif
  40. });
  41. onShow(() => {
  42. // #ifdef APP-PLUS
  43. // 页面显示时重新添加全局监听
  44. useRouteMonitor((url, from) => {
  45. console.log("onShow", url, from);
  46. // #ifdef APP-PLUS
  47. // 页面隐藏时移除全局监听
  48. useGlobalEventRemove();
  49. // #endif
  50. });
  51. // #endif
  52. });
  53. onHide(() => {
  54. console.log("App Hide");
  55. });
  56. // 在应用销毁时移除点击事件监听
  57. onUnload(() => {
  58. // 移除移动端触摸事件监听 - 使用条件编译区分不同平台
  59. // #ifdef H5
  60. if (touchHandler) {
  61. document.removeEventListener("touchstart", touchHandler);
  62. touchHandler = null;
  63. }
  64. // #endif
  65. // #ifdef APP-PLUS
  66. if (touchHandler) {
  67. var globalEvent = uni.requireNativePlugin("globalEvent");
  68. globalEvent.removeEventListener("click", touchHandler);
  69. touchHandler = null;
  70. }
  71. // #endif
  72. });
  73. </script>
  74. <style lang="scss">
  75. @import "@/uni_modules/uview-plus/index.scss";
  76. @import "@/static/css/mystyle.css";
  77. @import "@/static/css/common.scss";
  78. /*每个页面公共css */
  79. page {
  80. background: #f5f6fa;
  81. height: 100%;
  82. }
  83. </style>