upApp.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import store from '../store/index.js'
  2. const baseurl = store.state.baseURL
  3. const getUpAppUrl = baseurl + '/api/version'
  4. const iosAppStroeUrl =
  5. 'https://apps.apple.com/cn/app/%E5%BE%AE%E4%B9%90%E7%A7%8D/id1628702591';
  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. console.log(r.data, '234')
  28. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  29. // 保存线上版本号
  30. let version = r.url;
  31. // 获取线上版本
  32. console.log(r, '+++++++++++')
  33. const arr = r.version.split('.');
  34. // 获取当前系统
  35. const arr1 = wgtinfo.version.split('.');
  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, 'ver++++++++++++++++++++++++++++l')
  88. plus.nativeUI.showWaiting('下载升级包...');
  89. plus.downloader
  90. .createDownload(version, {}, (d, status) => {
  91. if (status == 200) {
  92. installApp(d.filename); // 安装app
  93. } else {
  94. plus.nativeUI.alert('下载升级包失败!');
  95. }
  96. plus.nativeUI.closeWaiting();
  97. })
  98. .start();
  99. }
  100. // 安装app
  101. export function installApp(path) {
  102. plus.nativeUI.showWaiting('安装升级包...');
  103. plus.runtime.install(
  104. path, {},
  105. function() {
  106. plus.nativeUI.closeWaiting();
  107. uni.showToast({
  108. icon: 'none',
  109. title: '升级完成,准备重新载入'
  110. });
  111. setTimeout(_ => {
  112. uni.hideToast();
  113. plus.runtime.restart();
  114. }, 1000);
  115. },
  116. function(e) {
  117. plus.nativeUI.closeWaiting();
  118. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  119. }
  120. );
  121. }