upApp.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import store from '../store/index.js'
  2. const baseurl = store.state.baseURL
  3. const getUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=1524593346'
  4. const getUpAppUrl1 = baseurl + '/api/version'
  5. const iosAppStroeUrl =
  6. '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';
  7. // 获取app是否需要升级
  8. export function getUpApp() {
  9. // 获取当前运行系统
  10. let hj = uni.getSystemInfoSync().platform;
  11. // 获取仓库app数据对象
  12. let app = store.state.isShowIllegality;
  13. // 当前系统为安卓则显示数据
  14. if (hj === 'android') {
  15. // 设置默认显示数据
  16. app = true;
  17. // store.commit('changeState', {
  18. // name: 'app',
  19. // value: app
  20. // });
  21. }
  22. let token = uni.getStorageSync('token') || '';
  23. uni.request({
  24. url: getUpAppUrl1,
  25. method: 'GET',
  26. header: {
  27. "Authori-zation": 'Bearer ' + token,
  28. },
  29. data: {},
  30. success: res => {
  31. let r = res.data.data;
  32. console.log(r)
  33. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  34. // 保存线上版本号
  35. let version = r.apk;
  36. console.log(version, 'version+++++++++++++++++++++++1+')
  37. // 获取线上版本
  38. const arr = r.version_code.split('.');
  39. // 获取当前系统
  40. const arr1 = wgtinfo.version.split('.');
  41. console.log(wgtinfo.version, 'wgtinfo.version+++++++++++')
  42. for (let i = 0; i < arr.length; i++) {
  43. // 线上版本号
  44. const x = +arr[i];
  45. // 线下版本号
  46. const y = +arr1[i];
  47. console.log(arr, arr1, "版本号")
  48. // 判断当前版本是否为小于等于线上版本
  49. if (x >= y) {
  50. console.log('dashabi2020')
  51. console.log(x, y)
  52. // 只有ios才需要显示
  53. if (hj === 'ios') {
  54. // 设置显示数据
  55. app = true;
  56. store.commit('changeState', {
  57. name: 'app',
  58. value: app
  59. });
  60. }
  61. // 当版本较低时更新数据
  62. if (x > y) {
  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. downApp(version);
  76. }
  77. }
  78. });
  79. return;
  80. }
  81. }
  82. }
  83. });
  84. },
  85. fail: e => {
  86. console.log(e, "失败")
  87. }
  88. });
  89. }
  90. // 下载app
  91. export function downApp(version) {
  92. console.log(version, "版本号")
  93. plus.nativeUI.showWaiting('下载升级包...');
  94. plus.downloader.createDownload( version, {}, (d, status) => {
  95. if (status == 200) {
  96. installApp(d.filename); // 安装app
  97. } else {
  98. plus.nativeUI.alert('下载升级包失败!');
  99. }
  100. plus.nativeUI.closeWaiting();
  101. }).start();
  102. }
  103. // 安装app
  104. export function installApp(path) {
  105. plus.nativeUI.showWaiting('安装升级包...');
  106. plus.runtime.install(
  107. path, {},
  108. function() {
  109. plus.nativeUI.closeWaiting();
  110. uni.showToast({
  111. icon: 'none',
  112. title: '升级完成,准备重新载入'
  113. });
  114. setTimeout(_ => {
  115. uni.hideToast();
  116. plus.runtime.restart();
  117. }, 1000);
  118. },
  119. function(e) {
  120. plus.nativeUI.closeWaiting();
  121. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  122. }
  123. );
  124. }