upApp.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/%E6%BB%A1%E5%9B%AD%E6%98%A5%E7%BA%BF%E4%B8%8A%E5%95%86%E5%9F%8E/id1524593346';
  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: 'POST',
  24. data: {},
  25. success: res => {
  26. let r = res.data;
  27. console.log(r.data,'23')
  28. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  29. // 保存线上版本号
  30. let version = r.data.apk;
  31. // 获取线上版本
  32. const arr = r.data.version_code.split('.');
  33. // 获取当前系统
  34. const arr1 = wgtinfo.version.split('.');
  35. for (let i = 0; i < arr.length; i++) {
  36. // 线上版本号
  37. const x = +arr[i];
  38. // 线下版本号
  39. const y = +arr1[i];
  40. console.log(arr,arr1,"版本号")
  41. // 判断当前版本是否为小于等于线上版本
  42. if (x >= y) {
  43. console.log('dashabi2020')
  44. console.log(x,y)
  45. // 只有ios才需要显示
  46. if (hj === 'ios') {
  47. // 设置显示数据
  48. app = true;
  49. store.commit('changeState', {
  50. name: 'app',
  51. value: app
  52. });
  53. }
  54. // 当版本较低时更新数据
  55. if (x > y) {
  56. uni.showModal({
  57. title: '提示',
  58. content: '请更新应用',
  59. showCancel: false,
  60. success(e) {
  61. if (hj === 'ios') {
  62. plus.runtime.openURL(
  63. iosAppStroeUrl
  64. );
  65. }
  66. if (hj === 'android') {
  67. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  68. downApp(version);
  69. }
  70. }
  71. });
  72. return;
  73. }
  74. }
  75. }
  76. });
  77. },
  78. fail: res =>{
  79. console.log(res,'shib');
  80. }
  81. });
  82. }
  83. // 下载app
  84. export function downApp (version) {
  85. console.log(version)
  86. plus.nativeUI.showWaiting('下载升级包...');
  87. plus.downloader
  88. .createDownload(baseurl + version, {}, (d, status) => {
  89. if (status == 200) {
  90. installApp(d.filename); // 安装app
  91. } else {
  92. plus.nativeUI.alert('下载升级包失败!');
  93. }
  94. plus.nativeUI.closeWaiting();
  95. })
  96. .start();
  97. }
  98. // 安装app
  99. export function installApp (path) {
  100. plus.nativeUI.showWaiting('安装升级包...');
  101. plus.runtime.install(
  102. path, {},
  103. function() {
  104. plus.nativeUI.closeWaiting();
  105. uni.showToast({
  106. icon: 'none',
  107. title: '升级完成,准备重新载入'
  108. });
  109. setTimeout(_ => {
  110. uni.hideToast();
  111. plus.runtime.restart();
  112. }, 1000);
  113. },
  114. function(e) {
  115. plus.nativeUI.closeWaiting();
  116. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  117. }
  118. );
  119. }