upApp.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import store from '../store/index.js'
  2. const baseurl = store.state.baseURL
  3. const getUpAppUrl = baseurl + '/api/version';
  4. const getIosUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=6474850968'
  5. // 请求升级获取数据地址
  6. const iosAppStroeUrl =
  7. 'https://apps.apple.com/cn/app/%E6%98%93%E8%B6%A3cbb/id=6474850968';
  8. // 获取app是否需要升级
  9. export function getUpApp() {
  10. // 获取当前运行系统
  11. let hj = uni.getSystemInfoSync().platform;
  12. // 获取仓库app数据对象
  13. if (hj === 'ios') {
  14. uni.request({
  15. url: getIosUpAppUrl,
  16. method: 'POST',
  17. data: {},
  18. success: res => {
  19. let r = res.data;
  20. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  21. // 保存线上版本号
  22. let version = r.results[0].version;
  23. // 获取线上版本
  24. const arr = r.results[0].version.split('.');
  25. // 获取当前系统
  26. const arr1 = wgtinfo.version.split('.');
  27. for (let i = 0; i < arr.length; i++) {
  28. // 线上版本号
  29. const x = +arr[i];
  30. // 本地版本号
  31. const y = +arr1[i];
  32. // 判断当前版本是否为小于等于线上版本
  33. if (x >= y) {
  34. // 设置显示数据
  35. store.commit('changeState', {
  36. name: 'app',
  37. value: true
  38. });
  39. // 当版本较低时更新数据
  40. if (x > y) {
  41. uni.showModal({
  42. title: '提示',
  43. content: '请更新应用',
  44. showCancel: false,
  45. success(e) {
  46. plus.runtime.openURL(
  47. iosAppStroeUrl
  48. );
  49. }
  50. });
  51. }
  52. }
  53. }
  54. });
  55. },
  56. });
  57. // 设置显示数据
  58. return
  59. }
  60. // 当前系统为安卓则显示数据
  61. if (hj === 'android') {
  62. // 设置默认显示数据
  63. store.commit('changeState', {
  64. name: 'app',
  65. value: true
  66. });
  67. uni.request({
  68. url: getUpAppUrl,
  69. method: 'GET',
  70. data: {},
  71. success: res => {
  72. let r = res.data.data;
  73. console.log(r.data, '234')
  74. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  75. // 保存线上版本号
  76. let version = r.url;
  77. console.log(version, 'versionversionversionversion');
  78. // 获取线上版本
  79. console.log(r, '+++++++++++')
  80. const arr = r.version.split('.');
  81. // 获取当前系统
  82. const arr1 = wgtinfo.version.split('.');
  83. console.log(arr, arr1)
  84. for (let i = 0; i < arr.length; i++) {
  85. // 线上版本号
  86. const x = +arr[i];
  87. // 线下版本号
  88. const y = +arr1[i];
  89. console.log(arr, arr1, "版本号")
  90. // 判断当前版本是否为小于等于线上版本
  91. if (x >= y) {
  92. // 只有ios才需要显示
  93. // 当版本较低时更新数据
  94. if (x > y) {
  95. uni.showModal({
  96. title: '提示',
  97. content: '请更新应用',
  98. showCancel: false,
  99. success(e) {
  100. // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk');
  101. downApp(version);
  102. }
  103. });
  104. }
  105. }
  106. }
  107. });
  108. },
  109. fail: res => {
  110. console.log(res, 'shib');
  111. }
  112. });
  113. }
  114. }
  115. // 下载app
  116. export function downApp(version) {
  117. console.log(version)
  118. uni.navigateTo({
  119. url: '/pages/index/downLoad?apk=' + version,
  120. fail(err) {
  121. console.log(err);
  122. }
  123. })
  124. // plus.nativeUI.showWaiting('下载升级包...');
  125. // plus.downloader
  126. // .createDownload(version, {}, (d, status) => {
  127. // if (status == 200) {
  128. // installApp(d.filename); // 安装app
  129. // } else {
  130. // plus.nativeUI.alert('下载升级包失败!');
  131. // }
  132. // plus.nativeUI.closeWaiting();
  133. // })
  134. // .start();
  135. }
  136. // 安装app
  137. export function installApp(path) {
  138. plus.nativeUI.showWaiting('安装升级包...');
  139. plus.runtime.install(
  140. path, {},
  141. function() {
  142. plus.nativeUI.closeWaiting();
  143. uni.showToast({
  144. icon: 'none',
  145. title: '升级完成,准备重新载入'
  146. });
  147. setTimeout(_ => {
  148. uni.hideToast();
  149. plus.runtime.restart();
  150. }, 1000);
  151. },
  152. function(e) {
  153. plus.nativeUI.closeWaiting();
  154. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  155. }
  156. );
  157. }