upApp.js 2.9 KB

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