upApp.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. console.log(hj, app, 'apppppppppppp');
  13. // 当前系统为安卓则显示数据
  14. if (hj === 'android') {
  15. // 设置默认显示数据
  16. app = true;
  17. // store.commit('changeState', {
  18. // name: 'app',
  19. // value: app
  20. // });
  21. }
  22. uni.request({
  23. url: getUpAppUrl,
  24. method: 'GET',
  25. data: {},
  26. success: res => {
  27. let r = res.data.data;
  28. console.log(r.data, '234')
  29. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  30. // 保存线上版本号
  31. let version = r.url;
  32. // 获取线上版本
  33. console.log(r, '+++++++++++')
  34. const arr = r.version.split('.');
  35. // 获取当前系统
  36. const arr1 = wgtinfo.version.split('.');
  37. console.log(arr, arr1)
  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. showCancel: false,
  63. success(e) {
  64. if (hj === 'ios') {
  65. plus.runtime.openURL(
  66. iosAppStroeUrl
  67. );
  68. }
  69. if (hj === 'android') {
  70. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  71. downApp(version);
  72. }
  73. }
  74. });
  75. return;
  76. }
  77. }
  78. }
  79. });
  80. },
  81. fail: res => {
  82. console.log(res, 'shib');
  83. }
  84. });
  85. }
  86. // 下载app
  87. export function downApp(version) {
  88. console.log(version, 'ver++++++++++++++++++++++++++++l')
  89. plus.nativeUI.showWaiting('下载升级包...');
  90. plus.downloader
  91. .createDownload(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. }