App.vue 3.2 KB

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