upApp.js 3.6 KB

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