App.vue 3.0 KB

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