upApp.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import confige from '@/config/app.js'
  2. const getUpAppUrl =confige.HTTP_REQUEST_URL+ '/api/version'
  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: 'GET',
  23. data: {},
  24. success: res => {
  25. console.log(res,'升级数据');
  26. let r = res.data.data;
  27. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  28. console.log(r.version,'获取版本');
  29. // 保存线上版本号
  30. let version = r.version;
  31. // 获取线上版本
  32. const arr = r.version.split('.');
  33. // 获取当前系统
  34. const arr1 = wgtinfo.version.split('.');
  35. console.log(version,'升级数据2');
  36. console.log(arr,'升级数据3');
  37. console.log(arr1,'升级数据4');
  38. let up = false;
  39. for (let i = 0; i < arr.length; i++) {
  40. // 线上版本号
  41. const x = +arr[i];
  42. // 线下版本号
  43. const y = +arr1[i];
  44. // 判断当前版本是否为小于等于线上版本
  45. console.log(x,y,'循环');
  46. if (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. console.log(x,y);
  58. if(up==true){
  59. return;
  60. }
  61. if (x > y) {
  62. up = true;
  63. uni.showModal({
  64. title: '提示',
  65. content: '请更新应用',
  66. showCancel: false,
  67. success(e) {
  68. // if (hj === 'ios') {
  69. // plus.runtime.openURL(
  70. // iosAppStroeUrl
  71. // );
  72. // }
  73. if (hj === 'android') {
  74. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  75. console.log(r.apk,'下載地址');
  76. downApp(r.apk);
  77. }
  78. }
  79. });
  80. }
  81. }
  82. }
  83. });
  84. },
  85. });
  86. }
  87. // 下载app
  88. export function downApp (version) {
  89. plus.nativeUI.showWaiting('下载升级包...');
  90. plus.downloader
  91. .createDownload(confige.HTTP_REQUEST_URL+version, {}, (d, status) => {
  92. if (status == 200) {
  93. installApp(d.filename); // 安装app
  94. } else {
  95. plus.nativeUI.alert('下载升级包失败!');
  96. }
  97. plus.nativeUI.closeWaiting();
  98. })
  99. .start();
  100. }
  101. // 安装app
  102. export function installApp (path) {
  103. plus.nativeUI.showWaiting('安装升级包...');
  104. plus.runtime.install(
  105. path, {},
  106. function() {
  107. plus.nativeUI.closeWaiting();
  108. uni.showToast({
  109. icon: 'none',
  110. title: '升级完成,准备重新载入'
  111. });
  112. setTimeout(_ => {
  113. uni.hideToast();
  114. plus.runtime.restart();
  115. }, 1000);
  116. },
  117. function(e) {
  118. plus.nativeUI.closeWaiting();
  119. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  120. }
  121. );
  122. }