upApp.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import store from '../store/index.js'
  2. // const getUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=1524593346'
  3. const getUpAppUrl = store.state.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. function compareVersions(version1, version2) {
  7. const v1 = version1.split('.');
  8. const v2 = version2.split('.');
  9. for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
  10. const num1 = parseInt(v1[i] || 0);
  11. const num2 = parseInt(v2[i] || 0);
  12. if (num1 < num2) {
  13. return -1;
  14. } else if (num1 > num2) {
  15. return 1;
  16. }
  17. }
  18. return 0;
  19. }
  20. // 获取app是否需要升级
  21. export function getUpApp () {
  22. // 获取当前运行系统
  23. let hj = uni.getSystemInfoSync().platform;
  24. // 获取仓库app数据对象
  25. let app = store.state.isShowIllegality;
  26. // 当前系统为安卓则显示数据
  27. if (hj === 'android') {
  28. // 设置默认显示数据
  29. app = true;
  30. // store.commit('changeState', {
  31. // name: 'app',
  32. // value: app
  33. // });
  34. }
  35. uni.request({
  36. url: getUpAppUrl,
  37. method: 'GET',
  38. data: {},
  39. success: res => {
  40. let r = res.data.data;
  41. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  42. // 保存线上版本号
  43. //{"app_down":"www.baidu.com","app_version":"1.0.1"} , app
  44. let version = r.app_down;
  45. // 获取线上版本
  46. const arr = r.app_version.split('.');
  47. console.log(arr,'app_version');
  48. // 获取当前系统
  49. const arr1 = wgtinfo.version.split('.');
  50. if(compareVersions(wgtinfo.version,r.app_version) < 0) {
  51. if (hj === 'android') {
  52. uni.navigateTo({
  53. url:'/pages/index/upApp?downurl=' + version
  54. })
  55. }
  56. }
  57. });
  58. },
  59. });
  60. }
  61. // 下载app
  62. export function downApp (version) {
  63. plus.nativeUI.showWaiting('下载升级包...');
  64. plus.downloader
  65. .createDownload('http://lxscimg.liuniu946.com/lxscV' + version + '.apk', {}, (d, status) => {
  66. if (status == 200) {
  67. installApp(d.filename); // 安装app
  68. } else {
  69. plus.nativeUI.alert('下载升级包失败!');
  70. }
  71. plus.nativeUI.closeWaiting();
  72. })
  73. .start();
  74. }
  75. // 安装app
  76. export function installApp (path) {
  77. plus.nativeUI.showWaiting('安装升级包...');
  78. plus.runtime.install(
  79. path, {},
  80. function() {
  81. plus.nativeUI.closeWaiting();
  82. uni.showToast({
  83. icon: 'none',
  84. title: '升级完成,准备重新载入'
  85. });
  86. setTimeout(_ => {
  87. uni.hideToast();
  88. plus.runtime.restart();
  89. }, 1000);
  90. },
  91. function(e) {
  92. plus.nativeUI.closeWaiting();
  93. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  94. }
  95. );
  96. }