upapp.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import Request from '@/library/Request.js'
  2. const getStore =() => {
  3. try {
  4. const app = getApp()
  5. if (app.$store) {
  6. return Promise.resolve(app.$store)
  7. }
  8. } catch (err) {
  9. setTimeout(() => {
  10. getStore()
  11. }, 200)
  12. }
  13. }
  14. export default function initSys() {
  15. const appType = uni.getSystemInfoSync().platform;
  16. if (plus != null) {
  17. plus.runtime.getProperty(plus.runtime.appid, async (wgtinfo) => {
  18. try {
  19. let versionCode = wgtinfo.versionCode;
  20. let arr1 = wgtinfo.version.split('.');
  21. const sotre = await getStore();
  22. // console.log(sotre,'sotre')
  23. sotre.commit('chnangeSshowData', false);
  24. const res = await Request.post("sysInit", {
  25. code: versionCode
  26. })
  27. if (res.code == 200) {
  28. if (appType != "ios") {
  29. if (wgtinfo.versionCode < res.data.app_code) {
  30. sotre.commit('chnangeSshowData', false);
  31. const upDater = uni.requireNativePlugin("CL-UpDater");
  32. let options = {
  33. title: "升级",
  34. con: res.data.app_update,
  35. downUrl: encodeURI(res.data.android_url), //必填
  36. hidCancelbtn: false, //是否隐藏取消按钮;
  37. btnBgColor: "#ff3300", //设置按钮背景色颜色
  38. updateBtnText: "升级", //升级按钮文字,默认为立即升级
  39. topImgBg: "", //非必填,用于自定义;自定义方法请参考demo,
  40. downMsgTip: "资源下载中,请稍后...", //可选(android)
  41. };
  42. if (res.data.android_url.indexOf('.wgt') > -1) {
  43. plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(fobject) {
  44. //设置文件下载根路径;
  45. let rootPath = fobject.root.fullPath;
  46. upDater.wgtUpdate(options, rootPath, result => {
  47. if (result) {
  48. var pathNew = plus.io
  49. .convertAbsoluteFileSystem(result);
  50. plus.runtime.install(pathNew, {
  51. force: false
  52. }, function() {
  53. //进行重新启动;
  54. plus.runtime.restart();
  55. }, (e) => {
  56. uni.showToast({
  57. title: '安装升级包失败' +
  58. JSON
  59. .stringify(
  60. e),
  61. icon: 'none'
  62. })
  63. });
  64. }
  65. }, () => {
  66. console.log("弹框关闭了");
  67. });
  68. })
  69. } else {
  70. options.hidBackBtn = true
  71. options.verCode = res.data
  72. .app_version //最新apk版本号 可选 用于Android整包升级,避免用户取消安装apk时,重复下载问题,如果不传的话,apk将会重新下载,
  73. options.goBackBtnText = "进入后台" //可选(android)
  74. //必需提供下载地址;
  75. upDater.startUpdate(options, () => {});
  76. }
  77. } else if (wgtinfo.versionCode > res.data.app_code) {
  78. sotre.commit('chnangeSshowData', false);
  79. } else {
  80. sotre.commit('chnangeSshowData', true);
  81. }
  82. }
  83. }
  84. if (appType == "ios") {
  85. const getIosUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=6474850968'
  86. const iosAppStroeUrl =
  87. 'https://apps.apple.com/cn/app/%E6%98%93%E8%B6%A3cbb/id6474850968';
  88. uni.request({
  89. url: getIosUpAppUrl,
  90. method: 'POST',
  91. data: {},
  92. success: res => {
  93. let arr = res.data.results[0].version.split('.');
  94. for (let i = 0; i < arr.length; i++) {
  95. // 线上版本号
  96. const x = +arr[i];
  97. // 本地版本号
  98. const y = +arr1[i];
  99. // 判断线上版本是否小于本地版本
  100. if (x < y) {
  101. // 设置显示数据
  102. sotre.commit('chnangeSshowData', false);
  103. return
  104. }
  105. // 判断线上版本是否大于本地版本
  106. else if (x > y) {
  107. sotre.commit('chnangeSshowData', false);
  108. uni.showModal({
  109. title: '提示',
  110. content: '请更新应用',
  111. showCancel: false,
  112. success(e) {
  113. plus.runtime.openURL(
  114. iosAppStroeUrl
  115. );
  116. }
  117. });
  118. return
  119. }
  120. // 判断是否本地版本等于线上版本
  121. else if (x == y && i == arr.length - 1) {
  122. sotre.commit('chnangeSshowData', true);
  123. return
  124. }
  125. }
  126. },
  127. fail: res => {
  128. console.log(res, 'shib');
  129. }
  130. });
  131. }
  132. } catch (e) {
  133. console.log(e)
  134. }
  135. });
  136. }
  137. }