upApp.js 2.8 KB

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