upApp.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import store from '../store/index.js'
  2. import {
  3. baseURL
  4. } from '../config/app'
  5. const getUpAppUrl = baseURL + '/api/index/version'
  6. const iosAppStroeUrl =
  7. 'https://apps.apple.com/cn/app/%E5%BE%AE%E4%B9%90%E7%A7%8D/id1628702591';
  8. // 获取app是否需要升级
  9. export function getUpApp() {
  10. // 获取当前运行系统
  11. let hj = uni.getSystemInfoSync().platform;
  12. // 获取仓库app数据对象
  13. let app = store.state.isShowIllegality;
  14. // 当前系统为安卓则显示数据
  15. if (hj === 'android') {
  16. // 设置默认显示数据
  17. app = true;
  18. // store.commit('changeState', {
  19. // name: 'app',
  20. // value: app
  21. // });
  22. }
  23. console.log(getUpAppUrl);
  24. uni.request({
  25. url: getUpAppUrl,
  26. method: 'GET',
  27. data: {},
  28. success: res => {
  29. console.log(res, '版本号');
  30. let r = res.data.data;
  31. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  32. // 保存线上版本号
  33. let version = r.app_android;
  34. // 获取线上版本
  35. const arr = r.version.split('.');
  36. // 获取当前系统
  37. const arr1 = wgtinfo.version.split('.');
  38. console.log(arr, arr1)
  39. for (let i = 0; i < arr.length; i++) {
  40. // 线上版本号
  41. const x = +arr[i];
  42. // 线下版本号
  43. const y = +arr1[i];
  44. console.log(arr, arr1, "版本号")
  45. // 判断当前版本是否为小于等于线上版本
  46. if (x >= y) {
  47. console.log('dashabi2020')
  48. console.log(x, y)
  49. // 只有ios才需要显示
  50. if (hj === 'ios') {
  51. // 设置显示数据
  52. app = true;
  53. store.commit('changeState', {
  54. name: 'app',
  55. value: app
  56. });
  57. }
  58. // 当版本较低时更新数据
  59. if (x > y) {
  60. uni.showModal({
  61. title: '提示',
  62. content: '请更新应用',
  63. showCancel: false,
  64. success(e) {
  65. if (hj === 'ios') {
  66. plus.runtime.openURL(
  67. iosAppStroeUrl
  68. );
  69. }
  70. if (hj === 'android') {
  71. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  72. downApp(version);
  73. }
  74. }
  75. });
  76. return;
  77. }
  78. }
  79. }
  80. });
  81. },
  82. fail: res => {
  83. console.log(res, 'shib');
  84. }
  85. });
  86. }
  87. // 下载app
  88. export function downApp(version) {
  89. console.log(version);
  90. uni.navigateTo({
  91. url: '/pages/index/downLoad?apk=' + version,
  92. fail(err) {
  93. console.log(err);
  94. }
  95. })
  96. // dtask = plus.downloader.createDownload(version);
  97. // dtask.addEventListener("statechanged", onStateChanged, false);
  98. // dtask.start();
  99. // uni.showLoading({
  100. // title: '下载中...',
  101. // mask: true
  102. // });
  103. // plus.nativeUI.showWaiting('下载升级包...');
  104. // plus.downloader
  105. // .createDownload(version, {}, (d, status) => {
  106. // if (status == 200) {
  107. // installApp(d.filename); // 安装app
  108. // } else {
  109. // plus.nativeUI.alert('下载升级包失败!');
  110. // }
  111. // plus.nativeUI.closeWaiting();
  112. // })
  113. // .start();
  114. }
  115. // 监听下载任务状态
  116. function onStateChanged(download, status) {
  117. switch (download.state) {
  118. case 1:
  119. // showLoading.setTitle("正在下载");
  120. break;
  121. case 2:
  122. // showLoading.setTitle("已连接到服务器");
  123. break;
  124. case 3:
  125. prg = parseInt( //下载的进度
  126. (parseFloat(download.downloadedSize) / parseFloat(download.totalSize)) * 100
  127. );
  128. // showLoading.setTitle("版本更新,正在下载" + prg + "% ");
  129. this.percent = prg;
  130. console.log(prg);
  131. break;
  132. case 4:
  133. // plus.nativeUI.closeWaiting(); //关闭系统提示框
  134. this.showProcess = false;
  135. //下载完成
  136. break;
  137. }
  138. }
  139. // 安装app
  140. export function installApp(path) {
  141. plus.nativeUI.showWaiting('安装升级包...');
  142. plus.runtime.install(
  143. path, {},
  144. function() {
  145. plus.nativeUI.closeWaiting();
  146. uni.showToast({
  147. icon: 'none',
  148. title: '升级完成,准备重新载入'
  149. });
  150. setTimeout(_ => {
  151. uni.hideToast();
  152. plus.runtime.restart();
  153. }, 1000);
  154. },
  155. function(e) {
  156. plus.nativeUI.closeWaiting();
  157. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  158. }
  159. );
  160. }