upApp.js 4.1 KB

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