App.uvue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <script lang="uts">
  2. // #ifdef APP-ANDROID || APP-HARMONY
  3. let firstBackTime = 0
  4. // #endif
  5. export default {
  6. onLaunch() {
  7. console.log('App Launch')
  8. // 1. 从本地缓存读取 token(或用户信息)
  9. const token = uni.getStorageSync('token');
  10. console.log(token, 'token');
  11. // 2. 判断登录态
  12. if (token === '' || token === null || token === 'undefined') {
  13. // 未登录:跳转到默认登录页
  14. uni.reLaunch({ url: '/pages/login/login' })
  15. } else {
  16. // 已登录:正常进入首页
  17. uni.switchTab({ url: '/pages/homepage/console' })
  18. }
  19. },
  20. onShow() {
  21. console.log('App Show')
  22. },
  23. onHide() {
  24. console.log('App Hide')
  25. },
  26. // #ifdef APP-ANDROID || APP-HARMONY
  27. onLastPageBackPress() {
  28. console.log('App LastPageBackPress')
  29. if (firstBackTime == 0) {
  30. uni.showToast({
  31. title: '再按一次退出应用',
  32. position: 'bottom',
  33. })
  34. firstBackTime = Date.now()
  35. setTimeout(() => {
  36. firstBackTime = 0
  37. }, 2000)
  38. } else if (Date.now() - firstBackTime < 2000) {
  39. firstBackTime = Date.now()
  40. uni.exit()
  41. }
  42. },
  43. // #endif
  44. onExit() {
  45. console.log('App Exit')
  46. },
  47. }
  48. </script>
  49. <style lang="scss">
  50. /*每个页面公共css */
  51. .uni-row {
  52. flex-direction: row;
  53. }
  54. .uni-column {
  55. flex-direction: column;
  56. }
  57. </style>