upApp.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import confige from '@/config/app.js'
  2. import {getAppVersion} from '@/api/api.js'
  3. const getIosUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=6476127203'
  4. const iosAppStroeUrl =
  5. 'https://apps.apple.com/cn/app/%E6%98%93%E8%B6%A3c%E6%9E%9C%E5%9B%AD/id6695741460';
  6. // 获取app是否需要升级
  7. export function getUpApp () {
  8. // 获取当前运行系统
  9. let hj = uni.getSystemInfoSync().platform;
  10. // 隐藏显示内容
  11. uni.setStorageSync("showAppHistary",false)
  12. // 获取仓库app数据对象
  13. if (hj === 'ios') {
  14. uni.request({
  15. url: getIosUpAppUrl,
  16. method: 'POST',
  17. data: {},
  18. success: res => {
  19. console.log(res)
  20. let r = res.data;
  21. isUp(r, hj)
  22. },
  23. fail: res => {
  24. // store.commit('changeState', true);
  25. console.log(res, 'shib');
  26. }
  27. });
  28. // 设置显示数据
  29. return
  30. }
  31. // 当前系统为安卓则显示数据
  32. if (hj === 'android') {
  33. getAppVersion().then((res)=>{
  34. let r = res.data;
  35. console.log(r,'rr')
  36. isUp(r, hj)
  37. }).catch((res)=>{
  38. console.log(res);
  39. })
  40. }
  41. }
  42. function isUp(r, hj) {
  43. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  44. let version = '';
  45. let arr = '';
  46. if (hj == "android") {
  47. // 保存线上版本号
  48. version =r.version;
  49. // store.commit('setappversion', r.version);
  50. // 获取线上版本
  51. arr = r.version.split('.');
  52. }
  53. if (hj == "ios") {
  54. // 保存线上版本号
  55. version = r.results[0].version;
  56. // store.commit('setappversion', version);
  57. // 获取线上版本
  58. arr = r.results[0].version.split('.');
  59. }
  60. // 获取当前系统
  61. const arr1 = wgtinfo.version.split('.');
  62. for (let i = 0; i < arr.length; i++) {
  63. // 线上版本号
  64. const x = +arr[i];
  65. // 本地版本号
  66. const y = +arr1[i];
  67. // 判断线上版本是否小于本地版本
  68. console.log(x, y, '数据');
  69. if (x < y) {
  70. // 设置显示数据
  71. uni.setStorageSync("showAppHistary",false)
  72. console.log('222')
  73. return
  74. }
  75. // 判断线上版本是否大于本地版本
  76. else if (x > y) {
  77. uni.setStorageSync("showAppHistary",true)
  78. console.log('223')
  79. uni.showModal({
  80. title: '提示',
  81. content: '请更新应用',
  82. showCancel: false,
  83. success(e) {
  84. if (hj == "android") {
  85. downApp(r.apk);
  86. }
  87. if (hj == "ios") {
  88. plus.runtime.openURL(
  89. iosAppStroeUrl
  90. );
  91. }
  92. }
  93. });
  94. return
  95. }
  96. // 判断是否本地版本等于线上版本
  97. else if (x == y && i == arr.length - 1) {
  98. uni.setStorageSync("showAppHistary",true)
  99. console.log('===')
  100. // store.commit('changeState', true);
  101. return
  102. }
  103. }
  104. });
  105. }
  106. // 下载app
  107. export function downApp (version) {
  108. let url = '';
  109. if(version.indexOf('http')>-1){
  110. url = version;
  111. }else{
  112. url = confige.HTTP_REQUEST_URL+version
  113. }
  114. console.log(url);
  115. plus.nativeUI.showWaiting('下载升级包...');
  116. plus.downloader
  117. .createDownload(url, {}, (d, status) => {
  118. if (status == 200) {
  119. installApp(d.filename); // 安装app
  120. } else {
  121. plus.nativeUI.alert('下载升级包失败!');
  122. }
  123. plus.nativeUI.closeWaiting();
  124. })
  125. .start();
  126. }
  127. // 安装app
  128. export function installApp (path) {
  129. plus.nativeUI.showWaiting('安装升级包...');
  130. plus.runtime.install(
  131. path, {},
  132. function() {
  133. plus.nativeUI.closeWaiting();
  134. uni.showToast({
  135. icon: 'none',
  136. title: '升级完成,准备重新载入'
  137. });
  138. setTimeout(_ => {
  139. uni.hideToast();
  140. plus.runtime.restart();
  141. }, 1000);
  142. },
  143. function(e) {
  144. plus.nativeUI.closeWaiting();
  145. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  146. }
  147. );
  148. }