App.vue 3.3 KB

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