upApp.js 3.3 KB

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