App.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.getNetworkType({
  29. success: function (res) {
  30. if (res.networkType === 'wifi') {
  31. uni.showModal({
  32. title: '版本发布更新',
  33. content: '发现新版本且您使用的是无限网络,是否进行更新?',
  34. confirmText: '更新',
  35. confirmColor: '#EE8F57',
  36. success: (res) => {
  37. if (res.confirm) {
  38. this.downloadAppFile()
  39. }
  40. }
  41. })
  42. } else {
  43. uni.showModal({
  44. title: '版本发布更新',
  45. content: '发现新版本,是否继续消耗流量进行更新?',
  46. confirmText: '更新',
  47. confirmColor: '#EE8F57',
  48. success: (res) => {
  49. if (res.confirm) {
  50. this.downloadAppFile()
  51. }
  52. }
  53. })
  54. }
  55. }
  56. });
  57. }
  58. }
  59. })
  60. },
  61. /**
  62. * 通过参数设置获取最新下载链接
  63. */
  64. getDownloadUrl() {
  65. uni.$u.api.getParameterApi({
  66. key: 'parking.operation.package'
  67. }).then(res => {
  68. if (res.code === 200) {
  69. this.downloadUrl = res?.msg
  70. this.getVersion();
  71. }
  72. })
  73. },
  74. /**
  75. * 下载文件
  76. */
  77. downloadAppFile() {
  78. uni.showLoading({
  79. mask: true,
  80. title: '下载中...'
  81. })
  82. let downloadTask = uni.downloadFile({ // 执行下载
  83. url: this.downloadUrl,
  84. success: downloadResult => {
  85. // 下载成功
  86. if (downloadResult.statusCode === 200) {
  87. uni.showModal({
  88. title: '',
  89. content: '下载成功,确定现在安装吗?',
  90. confirmText: '安装',
  91. confirmColor: '#EE8F57',
  92. success: function(res) {
  93. if (res.confirm) {
  94. plus.runtime.install( // 安装
  95. downloadResult.tempFilePath, {
  96. force: true
  97. },
  98. function() {
  99. plus.runtime.restart();
  100. uni.navigateBack()
  101. },
  102. function(e) {
  103. utils.showToast('安装失败');
  104. }
  105. );
  106. }
  107. }
  108. });
  109. }
  110. },
  111. complete: (res) => {
  112. uni.hideLoading();
  113. }
  114. });
  115. downloadTask.onProgressUpdate((res) => {
  116. if (res.progress === 100) {
  117. uni.hideLoading();
  118. }
  119. });
  120. }
  121. },
  122. onHide: function() {
  123. console.log('App Hide')
  124. },
  125. }
  126. </script>
  127. <style lang="scss">
  128. /*每个页面公共css */
  129. @import "@/uni_modules/uview-ui/index.scss";
  130. </style>