App.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <script>
  2. import {
  3. config
  4. } from '@/common/config.js';
  5. export default {
  6. data() {
  7. return {
  8. downloadUrl: ''
  9. }
  10. },
  11. onLaunch: function() {
  12. console.log('App Launch')
  13. },
  14. onShow: function() {
  15. console.log('App Show')
  16. this.getDownloadUrl();
  17. },
  18. methods: {
  19. /**
  20. * 通过参数设置获取最新版本信息
  21. */
  22. getVersion() {
  23. uni.$u.api.getParameterApi({
  24. key: 'parking.operation.version'
  25. }).then(res => {
  26. if (res.code === 200) {
  27. if (res.msg !== config.version) {
  28. uni.showModal({
  29. title: '版本发布更新',
  30. content: '发现新版本,请进行更新',
  31. confirmText: '更新',
  32. confirmColor: '#EE8F57',
  33. success: (res) => {
  34. if (res.confirm) {
  35. this.downloadAppFile()
  36. }
  37. }
  38. })
  39. }
  40. }
  41. })
  42. },
  43. /**
  44. * 通过参数设置获取最新下载链接
  45. */
  46. getDownloadUrl() {
  47. uni.$u.api.getParameterApi({
  48. key: 'parking.operation.package'
  49. }).then(res => {
  50. if (res.code === 200) {
  51. this.downloadUrl = res?.msg
  52. this.getVersion();
  53. }
  54. })
  55. },
  56. /**
  57. * 下载文件
  58. */
  59. downloadAppFile() {
  60. uni.showLoading({
  61. mask: true,
  62. title: '下载中...'
  63. })
  64. let downloadTask = uni.downloadFile({ // 执行下载
  65. url: this.downloadUrl,
  66. success: downloadResult => {
  67. // 下载成功
  68. if (downloadResult.statusCode === 200) {
  69. uni.showModal({
  70. title: '',
  71. content: '下载成功,确定现在更新吗?',
  72. confirmText: '更新',
  73. confirmColor: '#EE8F57',
  74. success: function(res) {
  75. if (res.confirm) {
  76. plus.runtime.install( // 安装
  77. downloadResult.tempFilePath, {
  78. force: true
  79. },
  80. function() {
  81. plus.runtime.restart();
  82. uni.navigateBack()
  83. },
  84. function(e) {
  85. utils.showToast('更新失败');
  86. }
  87. );
  88. }
  89. }
  90. });
  91. }
  92. },
  93. complete: (res) => {
  94. uni.hideLoading();
  95. }
  96. });
  97. downloadTask.onProgressUpdate((res) => {
  98. if (res.progress === 100) {
  99. uni.hideLoading();
  100. }
  101. });
  102. }
  103. },
  104. onHide: function() {
  105. console.log('App Hide')
  106. },
  107. }
  108. </script>
  109. <style lang="scss">
  110. /*每个页面公共css */
  111. /*每个页面公共css */
  112. @import "@/uni_modules/uview-ui/index.scss";
  113. </style>