App.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script lang="ts">
  2. import {body} from "@/utils/https";
  3. import {groupByKey} from "@/utils/common";
  4. export default <any>{
  5. globalData: {
  6. token: "",
  7. user: {},
  8. isLogin: false,
  9. last: {},
  10. device: {},
  11. deviceId: null,
  12. manualLogout: false
  13. },
  14. onLaunch(options: any) {
  15. let device = uni.getWindowInfo();
  16. let initGlobalData = {
  17. token: "",
  18. user: {},
  19. isLogin: false,
  20. last: {},
  21. device: {
  22. windowWidth:device.windowWidth,
  23. windowHeight:device.windowHeight,
  24. screenTop: device.screenTop,
  25. windowTop: device.windowTop,
  26. },
  27. deviceId: null,
  28. manualLogout: false
  29. }
  30. this.globalData = Object.assign({}, initGlobalData)
  31. body(`/dict/list`).then((res: any) => {
  32. if (res) {
  33. let dicts = res.map((k: any) => {
  34. let {code, name, value, color} = k;
  35. return {code, name, value, color}
  36. })
  37. let dictMap = groupByKey(dicts, "code");
  38. uni.setStorage({key: 'dict', data: dictMap})
  39. }
  40. })
  41. // 处理专属优惠活动扫码进入(wxacode.getUnlimited 的 scene 参数)
  42. // #ifdef MP-WEIXIN
  43. const scene = options?.query?.scene;
  44. if (scene) {
  45. // 延迟跳转,确保页面初始化完毕
  46. setTimeout(() => {
  47. uni.navigateTo({
  48. url: `/pages-user/wallet/promotion?promotionToken=${encodeURIComponent(scene)}`,
  49. fail: () => {
  50. // 如果 navigateTo 失败(可能在 tabBar 页面),尝试用 reLaunch
  51. }
  52. });
  53. }, 800);
  54. }
  55. // #endif
  56. // this.globalData.token = fetchToken();
  57. },
  58. onShow(options: any) {
  59. let device = uni.getWindowInfo();
  60. let backup = uni.getStorageSync('backup')
  61. // console.log("backup", backup)
  62. this.globalData = Object.assign({}, this.globalData, {
  63. device: {
  64. windowWidth: device.windowWidth,
  65. windowHeight: device.windowHeight,
  66. screenTop: device.screenTop,
  67. windowTop: device.windowTop,
  68. }
  69. }, backup)
  70. // 从 backup 恢复 token 到 storage,确保 fetchToken() 在冷启动时能读取到
  71. if (backup && backup.token) {
  72. uni.setStorageSync("token", backup.token)
  73. }
  74. },
  75. onHide() {
  76. //backup
  77. let backup = {...getApp<any>().globalData}
  78. uni.setStorageSync('backup', backup)
  79. },
  80. onPageNotFound() {
  81. uni.switchTab({
  82. url: "/pages/index/index",
  83. });
  84. },
  85. };
  86. </script>
  87. <style lang="scss">
  88. @import '@climblee/uv-ui/index.scss';
  89. @import './custom.scss';
  90. </style>