upApp.js 4.2 KB

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