upApp.js 3.0 KB

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