upapp.js 4.2 KB

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