upApp.js 3.6 KB

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