upApp.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import store from '../store/index.js'
  2. const getUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=1524593346'
  3. const iosAppStroeUrl =
  4. 'https://apps.apple.com/cn/app/%E6%BB%A1%E5%9B%AD%E6%98%A5%E7%BA%BF%E4%B8%8A%E5%95%86%E5%9F%8E/id1524593346';
  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: 'POST',
  23. data: {},
  24. success: res => {
  25. let r = res.data;
  26. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  27. // 保存线上版本号
  28. let version = r.results[0].version;
  29. // 获取线上版本
  30. const arr = r.results[0].version.split('.');
  31. // 获取当前系统
  32. const arr1 = wgtinfo.version.split('.');
  33. if (hj === 'android') {
  34. for (let i = 0; i < arr.length; i++) {
  35. // 线上版本号
  36. const x = +arr[i];
  37. // 线下版本号
  38. const y = +arr1[i];
  39. console.log(arr, arr1, "版本号")
  40. // 判断当前版本是否为小于等于线上版本
  41. if (x >= y) {
  42. console.log('dashabi2020')
  43. console.log(x, y)
  44. // 当版本较低时更新数据
  45. if (x > y) {
  46. uni.showModal({
  47. title: '提示',
  48. content: '请更新应用',
  49. showCancel: false,
  50. success(e) {
  51. if (hj === 'android') {
  52. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  53. downApp(version);
  54. }
  55. }
  56. });
  57. return;
  58. }
  59. }
  60. }
  61. }
  62. if (hj === 'ios') {
  63. for (let i = 0; i < iosarr.length; i++) {
  64. // 线上版本号
  65. const x = +iosarr[i];
  66. // 线下版本号
  67. const y = +arr1[i];
  68. console.log(iosarr, arr1, "版本号")
  69. // 判断当前版本是否为小于等于线上版本
  70. if (x >= y) {
  71. console.log(x, y)
  72. // 只有ios才需要显示
  73. if (hj === 'ios') {
  74. // 设置显示数据
  75. app = true;
  76. store.commit('changeState', {
  77. name: 'app',
  78. value: app
  79. });
  80. }
  81. // 当版本较低时更新数据
  82. if (x > y) {
  83. uni.showModal({
  84. title: '提示',
  85. content: '请更新应用',
  86. showCancel: false,
  87. success(e) {
  88. if (hj === 'ios') {
  89. plus.runtime.openURL(
  90. iosAppStroeUrl
  91. );
  92. }
  93. }
  94. });
  95. return;
  96. }
  97. }
  98. }
  99. }
  100. });
  101. },
  102. });
  103. }
  104. // 下载app
  105. export function downApp(version) {
  106. plus.nativeUI.showWaiting('下载升级包...');
  107. plus.downloader
  108. .createDownload('http://lxscimg.liuniu946.com/lxscV' + version + '.apk', {}, (d, status) => {
  109. if (status == 200) {
  110. installApp(d.filename); // 安装app
  111. } else {
  112. plus.nativeUI.alert('下载升级包失败!');
  113. }
  114. plus.nativeUI.closeWaiting();
  115. })
  116. .start();
  117. }
  118. // 安装app
  119. export function installApp(path) {
  120. plus.nativeUI.showWaiting('安装升级包...');
  121. plus.runtime.install(
  122. path, {},
  123. function() {
  124. plus.nativeUI.closeWaiting();
  125. uni.showToast({
  126. icon: 'none',
  127. title: '升级完成,准备重新载入'
  128. });
  129. setTimeout(_ => {
  130. uni.hideToast();
  131. plus.runtime.restart();
  132. }, 1000);
  133. },
  134. function(e) {
  135. plus.nativeUI.closeWaiting();
  136. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  137. }
  138. );
  139. }