App.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <script>
  2. import config from './config'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. export default {
  6. onLaunch: function() {
  7. this.initApp()
  8. },
  9. methods: {
  10. // 初始化应用
  11. initApp() {
  12. // 初始化应用配置
  13. this.initConfig()
  14. // 检查用户登录状态
  15. //#ifdef H5
  16. // this.checkLogin()
  17. //#endif
  18. this.updateManager();
  19. },
  20. initConfig() {
  21. this.globalData.config = config
  22. },
  23. /**
  24. * 小程序主动更新
  25. */
  26. updateManager() {
  27. const updateManager = uni.getUpdateManager();
  28. updateManager.onCheckForUpdate(res => {
  29. // 请求完新版本信息的回调
  30. // console.log(res.hasUpdate)
  31. })
  32. updateManager.onUpdateReady(() => {
  33. uni.showModal({
  34. title: '更新提示',
  35. content: '新版本已经准备好,即将重启应用',
  36. showCancel: false,
  37. success(res) {
  38. if (res.confirm) {
  39. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  40. updateManager.applyUpdate()
  41. }
  42. }
  43. })
  44. })
  45. updateManager.onUpdateFailed(() => {
  46. // 新的版本下载失败
  47. uni.showModal({
  48. title: '更新提示',
  49. content: '新版本下载失败',
  50. showCancel: false
  51. })
  52. })
  53. },
  54. checkLogin() {
  55. const pages = uni.getCurrentPages();
  56. const currentPage = pages[pages.length - 1]; // 当前页面
  57. const currentPagePath = currentPage.$route.path; // 当前页面的路径
  58. if (!getToken()) {
  59. this.$tab.reLaunch('/pages/login')
  60. }
  61. }
  62. }
  63. }
  64. </script>
  65. <style lang="scss">
  66. @import "@/static/iconfont.css";
  67. @import '@/static/scss/index.scss'
  68. </style>