App.vue 3.1 KB

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