App.vue 2.5 KB

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