upApp.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. console.log(r,'rrrr')
  42. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  43. // 保存线上版本号
  44. //{"app_down":"www.baidu.com","app_version":"1.0.1"} , app
  45. let version = r.url;
  46. // 获取线上版本
  47. const arr = r.version.split('.');
  48. console.log(arr,'version');
  49. // 获取当前系统
  50. const arr1 = wgtinfo.version.split('.');
  51. if(compareVersions(wgtinfo.version,r.version) < 0) {
  52. if (hj === 'android') {
  53. uni.navigateTo({
  54. url:'/pages/index/upApp?downurl=' + version
  55. })
  56. }
  57. }
  58. });
  59. },
  60. });
  61. }
  62. // 下载app
  63. export function downApp (version) {
  64. plus.nativeUI.showWaiting('下载升级包...');
  65. plus.downloader
  66. .createDownload('http://lxscimg.liuniu946.com/lxscV' + version + '.apk', {}, (d, status) => {
  67. if (status == 200) {
  68. installApp(d.filename); // 安装app
  69. } else {
  70. plus.nativeUI.alert('下载升级包失败!');
  71. }
  72. plus.nativeUI.closeWaiting();
  73. })
  74. .start();
  75. }
  76. // 安装app
  77. export function installApp (path) {
  78. plus.nativeUI.showWaiting('安装升级包...');
  79. plus.runtime.install(
  80. path, {},
  81. function() {
  82. plus.nativeUI.closeWaiting();
  83. uni.showToast({
  84. icon: 'none',
  85. title: '升级完成,准备重新载入'
  86. });
  87. setTimeout(_ => {
  88. uni.hideToast();
  89. plus.runtime.restart();
  90. }, 1000);
  91. },
  92. function(e) {
  93. plus.nativeUI.closeWaiting();
  94. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  95. }
  96. );
  97. }