App.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <script>
  2. import config from './config'
  3. import store from '@/store'
  4. import {
  5. getToken
  6. } from '@/utils/auth'
  7. import silenceUpdate from '@/uni_modules/rt-uni-update/js_sdk/silence-update.js' //引入静默更新
  8. import {
  9. latestEdition,
  10. download
  11. } from '@/api/wms/report.js'
  12. export default {
  13. data() {
  14. return {
  15. timer: null, // 定义一个全局的定时器变量
  16. aaa:123,
  17. };
  18. },
  19. onLaunch: function() {
  20. this.initApp()
  21. // 设置默认的 toast 显示时间为 3000ms (3秒)
  22. uni.setStorageSync('toastDuration', '5000')
  23. },
  24. mounted() {},
  25. beforeDestroy() {
  26. this.clearTimer(); // 组件销毁前清除定时器
  27. },
  28. onShow() {
  29. this.initTimer(); // 解锁时重新初始化定时器
  30. },
  31. onHide() {
  32. this.clearTimer(); // 锁屏时清除定时器
  33. },
  34. methods: {
  35. // 初始化应用
  36. initApp() {
  37. // 初始化应用配置
  38. this.initConfig()
  39. // 检查用户登录状态
  40. //#ifdef H5
  41. this.checkLogin()
  42. //#endif
  43. },
  44. initConfig() {
  45. this.globalData.config = config
  46. },
  47. checkLogin() {
  48. if (!getToken()) {
  49. this.$tab.reLaunch('/pages/login')
  50. }
  51. },
  52. initTimer() {
  53. this.timer = setInterval(() => {
  54. // 每10分钟查询一次是否新版本
  55. this.version();
  56. }, 600000);
  57. },
  58. clearTimer() {
  59. if (this.timer) {
  60. clearInterval(this.timer);
  61. this.timer = null;
  62. }
  63. },
  64. version() {
  65. //获取版本号,必须为数字
  66. let edition_number = ""
  67. plus.runtime.getProperty(plus.runtime.appid, (inf) => {
  68. edition_number = inf.versionCode
  69. //获取版本号,如果没有缓存的版本号再取app的版本号
  70. //传参
  71. let params = {
  72. edition_number: edition_number
  73. }
  74. //查询新版本接口
  75. latestEdition(params).then(res => {
  76. if (res.code === 200 && res.data) {
  77. let baseUrl = uni.getStorageSync("baseUrl");
  78. res.data.edition_url = res.data.edition_url.replace("${baseUrl}", baseUrl);
  79. if (res.data.package_type == 1 && res.data.edition_silence == 1) {
  80. //调用静默更新方法 传入下载地址
  81. silenceUpdate(res.data)
  82. } else {
  83. //跳转更新页面 (注意!!!如果pages.json第一页的代码里有一打开就跳转其他页面的操作,下面这行代码最好写在setTimeout里面设置延时3到5秒再执行)
  84. uni.navigateTo({
  85. url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
  86. JSON.stringify(res.data)
  87. });
  88. }
  89. } else {
  90. // uni.showToast({
  91. // title: '已是最新版本',
  92. // icon: 'none'
  93. // })
  94. }
  95. });
  96. })
  97. },
  98. }
  99. }
  100. </script>
  101. <style lang="scss">
  102. @import '@/static/scss/index.scss'
  103. </style>
  104. <style>
  105. @import '@/static/iconfont/iconfont-weapp-icon.css'
  106. </style>