App.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <script>
  2. import { config } from '@/common/config.js';
  3. export default {
  4. data() {
  5. return {
  6. downloadUrl: '',
  7. version: ''
  8. };
  9. },
  10. onLaunch: function() {
  11. console.log('App Launch');
  12. },
  13. onShow: function() {
  14. console.log('App Show');
  15. this.getDownloadUrl();
  16. },
  17. methods: {
  18. /**
  19. * 通过参数设置获取最新版本信息
  20. */
  21. getVersion() {
  22. const _this = this;
  23. uni.$u.api
  24. .getParameterApi({
  25. key: 'parking.operation.version'
  26. })
  27. .then(res => {
  28. if (res.code === 200) {
  29. this.version = res.msg
  30. if (res.msg !== config.version) {
  31. _this.downloadAppFileByBackstage()
  32. // uni.getNetworkType({
  33. // success: function(res) {
  34. // if (res.networkType === 'wifi') {
  35. // _this.downloadAppFileByBackstage()
  36. // } else {
  37. // uni.showModal({
  38. // title: '提示',
  39. // content: '当前网络非WIFI,继续更新可能会产生流量,确认要更新吗?',
  40. // confirmText: '更新',
  41. // confirmColor: '#EE8F57',
  42. // success: res => {
  43. // if (res.confirm) {
  44. // _this.downloadAppFileByBackstage()
  45. // }
  46. // }
  47. // });
  48. // }
  49. // }
  50. // });
  51. }
  52. }
  53. });
  54. },
  55. /**
  56. * 通过参数设置获取最新下载链接
  57. */
  58. getDownloadUrl() {
  59. uni.$u.api
  60. .getParameterApi({
  61. key: 'parking.operation.package'
  62. })
  63. .then(res => {
  64. if (res.code === 200) {
  65. this.downloadUrl = res?.msg;
  66. this.getVersion();
  67. }
  68. });
  69. },
  70. /**
  71. * 下载app通过downloadFile
  72. */
  73. downloadAppFile() {
  74. const _this = this
  75. uni.showLoading({
  76. mask: true,
  77. title: '下载中...'
  78. })
  79. let downloadTask = uni.downloadFile({ // 执行下载
  80. url: _this.downloadUrl,
  81. success: downloadResult => {
  82. // 下载成功
  83. if (downloadResult.statusCode === 200) {
  84. uni.showModal({
  85. title: '',
  86. content: '下载成功,确定现在安装吗?',
  87. confirmText: '安装',
  88. confirmColor: '#EE8F57',
  89. success: function(res) {
  90. if (res.confirm) {
  91. plus.runtime.install( // 安装
  92. downloadResult.tempFilePath, {
  93. force: true
  94. },
  95. function() {
  96. plus.runtime.restart();
  97. uni.navigateBack()
  98. },
  99. function(e) {
  100. utils.showToast('安装失败');
  101. }
  102. );
  103. }
  104. }
  105. });
  106. }
  107. },
  108. complete: (res) => {
  109. uni.hideLoading();
  110. }
  111. });
  112. downloadTask.onProgressUpdate((res) => {
  113. if (res.progress === 100) {
  114. uni.hideLoading();
  115. }
  116. });
  117. },
  118. /**
  119. * 下载app通过后台任务栏
  120. */
  121. downloadAppFileByBackstage() {
  122. uni.$u.route({
  123. url: 'pages/appUpdate/appUpdate',
  124. params: {
  125. downloadUrl: this.downloadUrl,
  126. version: this.version
  127. }
  128. })
  129. },
  130. /**
  131. * 下载app通过浏览器
  132. */
  133. downloadAppFileByBrowser() {
  134. // #ifdef APP-PLUS
  135. plus.runtime.openURL(this.downloadUrl);
  136. // #endif
  137. }
  138. },
  139. onHide: function() {
  140. console.log('App Hide');
  141. }
  142. };
  143. </script>
  144. <style lang="scss">
  145. /*每个页面公共css */
  146. @import '@/uni_modules/uview-ui/index.scss';
  147. </style>