App.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * 下载文件
  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. // #ifdef APP-PLUS
  127. const dtask = plus.downloader.createDownload(_this.downloadUrl, { force: true }, function(d, status) {
  128. // 下载完成
  129. if (status == 200) {
  130. var path = d.filename; //文件安装路径
  131. plus.runtime.install(
  132. path,
  133. {},
  134. function() {
  135. plus.nativeUI.closeWaiting(); //关闭系统等待对话框
  136. if (name == 'wgt') {
  137. console.log('安装wgt文件成功!');
  138. } else {
  139. console.log('安装apk文件成功!');
  140. }
  141. plus.nativeUI.alert('安装成功!', function() {
  142. plus.runtime.restart();
  143. });
  144. },
  145. function(e) {
  146. plus.nativeUI.closeWaiting(); //关闭系统等待对话框
  147. if (name == 'wgt') {
  148. console.log('安装wgt文件失败[' + e.code + ']:' + e.message);
  149. plus.nativeUI.alert('安装wgt文件失败[' + e.code + ']:' + e.message);
  150. } else {
  151. console.log('安装apk文件失败[' + e.code + ']:' + e.message);
  152. plus.nativeUI.alert('安装apk文件失败[' + e.code + ']:' + e.message);
  153. }
  154. }
  155. );
  156. } else {
  157. alert('下载失败:' + status);
  158. }
  159. });
  160. // #endif
  161. },
  162. /**
  163. * 下载app通过浏览器
  164. */
  165. downloadAppFileByBrowser() {
  166. // #ifdef APP-PLUS
  167. plus.runtime.openURL(this.downloadUrl);
  168. // #endif
  169. }
  170. },
  171. onHide: function() {
  172. console.log('App Hide');
  173. }
  174. };
  175. </script>
  176. <style lang="scss">
  177. /*每个页面公共css */
  178. @import '@/uni_modules/uview-ui/index.scss';
  179. </style>