upApp.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import store from '../store/index.js'
  2. const baseurl = store.state.baseURL
  3. const getUpAppUrl = store.state.baseURL + '/api/version'
  4. const iosAppStroeUrl =
  5. 'https://apps.apple.com/cn/app/%E4%BA%91%E5%8A%9B%E5%95%86%E5%9F%8E/id1625060402';
  6. // 获取app是否需要升级
  7. export function getUpApp() {
  8. let that = this
  9. // 获取当前运行系统
  10. let hj = uni.getSystemInfoSync().platform;
  11. // 获取仓库app数据对象
  12. let app = store.state.isShowIllegality;
  13. // 当前系统为安卓则显示数据
  14. if (hj === 'android') {
  15. // 设置默认显示数据
  16. app = true;
  17. // store.commit('changeState', {
  18. // name: 'app',
  19. // value: app
  20. // });
  21. }
  22. // console.log(getUpAppUrl, '123456');
  23. uni.request({
  24. url: getUpAppUrl,
  25. method: 'GET',
  26. data: {},
  27. success: res => {
  28. let r = res.data;
  29. console.log(r,'dddddd')
  30. console.log(r, '123456');
  31. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  32. // 保存线上版本号
  33. let version = r.data.url;
  34. // 获取线上版本
  35. const arr = r.data.version.split('.');
  36. // 获取当前系统
  37. const arr1 = wgtinfo.version.split('.');
  38. for (let i = 0; i < arr.length; i++) {
  39. // 线上版本号
  40. const x = +arr[i];
  41. // 线下版本号
  42. const y = +arr1[i];
  43. console.log(arr, arr1, "版本号")
  44. // 判断当前版本是否为小于等于线上版本
  45. if (x >= y) {
  46. console.log('dashabi2020')
  47. console.log(x, y)
  48. // 只有ios才需要显示
  49. if (hj === 'ios') {
  50. // 设置显示数据
  51. app = true;
  52. store.commit('changeState', {
  53. name: 'app',
  54. value: app
  55. });
  56. }
  57. // 当版本较低时更新数据
  58. if (x > y) {
  59. uni.showModal({
  60. title: '提示',
  61. content: '请更新应用',
  62. confirmText: '确认',
  63. showCancel: false,
  64. success(e) {
  65. if (hj === 'ios') {
  66. }
  67. if (hj === 'android') {
  68. downApp(version);
  69. }
  70. }
  71. })
  72. // uni.showModal({
  73. // title: this.$t('popup.ts'),
  74. // content: this.$t('popup.qgxyy'),
  75. // confirmText: this.$t('popup.qr'),
  76. // cancelText: this.$t('popup.qx'),
  77. // showCancel: false,
  78. // success(e) {
  79. // if (hj === 'ios') {
  80. // // plus.runtime.openURL(
  81. // // iosAppStroeUrl
  82. // // );
  83. // }
  84. // if (hj === 'android') {
  85. // // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  86. // downApp(version);
  87. // }
  88. // }
  89. // });
  90. return;
  91. }
  92. }
  93. }
  94. });
  95. },
  96. fail: res => {
  97. console.log(res, 'shib');
  98. }
  99. });
  100. }
  101. // 下载app
  102. export function downApp(version) {
  103. console.log(version)
  104. plus.nativeUI.showWaiting('下载升级包中...');
  105. plus.downloader
  106. .createDownload(version, {}, (d, status) => {
  107. if (status == 200) {
  108. installApp(d.filename); // 安装app
  109. } else {
  110. plus.nativeUI.alert('下载升级包失败!');
  111. }
  112. plus.nativeUI.closeWaiting();
  113. })
  114. .start();
  115. }
  116. // 安装app
  117. export function installApp(path) {
  118. plus.nativeUI.showWaiting('安裝升级包...');
  119. plus.runtime.install(
  120. path, {},
  121. function() {
  122. plus.nativeUI.closeWaiting();
  123. uni.showToast({
  124. icon: 'none',
  125. title: '升级完成,准备重新载入'
  126. });
  127. setTimeout(_ => {
  128. uni.hideToast();
  129. plus.runtime.restart();
  130. }, 1000);
  131. },
  132. function(e) {
  133. plus.nativeUI.closeWaiting();
  134. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  135. }
  136. );
  137. }