upapp.js 4.2 KB

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