upApp.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import store from '../store/index.js'
  2. // const baseurl = store.state.baseURL
  3. const getUpAppUrl = 'https://ygs.hqgjsmc.com/api/version'
  4. const iosAppStroeUrl =
  5. 'https://apps.apple.com/cn/app/%E6%98%93%E5%8D%9A%E4%BC%98%E8%B4%AD/id1642267825';
  6. // 获取app是否需要升级
  7. export function getUpApp() {
  8. // 获取当前运行系统
  9. let hj = uni.getSystemInfoSync().platform;
  10. // 获取仓库app数据对象
  11. let app = store.state.isShowIllegality;
  12. // 当前系统为安卓则显示数据
  13. if (hj === 'android') {
  14. // 设置默认显示数据
  15. app = true;
  16. // store.commit('changeState', {
  17. // name: 'app',
  18. // value: app
  19. // });
  20. }
  21. uni.request({
  22. url: getUpAppUrl,
  23. method: 'GET',
  24. data: {},
  25. success: res => {
  26. let r = res.data.data;
  27. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  28. // 保存线上版本号
  29. let version = r.apk;
  30. // 获取线上版本
  31. console.log(r.apk, '+++++++++++')
  32. const arr = r.version.split('.');
  33. // 获取当前系统
  34. const arr1 = wgtinfo.version.split('.');
  35. console.log(arr,arr1);
  36. console.log(arr, arr1);
  37. for (let i = 0; i < arr.length; i++) {
  38. // 线上版本号
  39. const x = +arr[i];
  40. // 线下版本号
  41. const y = +arr1[i];
  42. console.log(arr, arr1, "版本号")
  43. // 判断当前版本是否为小于等于线上版本
  44. if (x >= y) {
  45. console.log('dashabi2020')
  46. console.log(x, y)
  47. // 只有ios才需要显示
  48. if (hj === 'ios') {
  49. // 设置显示数据
  50. app = true;
  51. store.commit('changeState', {
  52. name: 'app',
  53. value: app
  54. });
  55. }
  56. // 当版本较低时更新数据
  57. if (x > y) {
  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. }