upgrade.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Member from "../api/member.js"
  2. import vue from 'vue'
  3. // 缓存数据避免重复请求
  4. let localVersion, onlineVersionObj
  5. // import router from '@/router'
  6. /* eslint-disable */
  7. let upgrade = {
  8. // 检测是否需要升级并返回升级信息
  9. async isUpdate(call, notCall) {
  10. // #ifdef H5
  11. notCall && notCall()
  12. // #endif
  13. // #ifdef APP-PLUS
  14. let currentVersion = await this.getAppVersion();
  15. let { data } = await this.getNewestVersion()
  16. // data.android.version data.ios.version
  17. if (plus.os.name == "Android") {
  18. if (this.getNum(data.android.version) > this.getNum(currentVersion)) {
  19. call && call(data)
  20. } else {
  21. notCall && notCall()
  22. }
  23. } else if (plus.os.name == "iOS") {
  24. if (this.getNum(data.ios.version) > this.getNum(currentVersion)) {
  25. call && call(data)
  26. } else {
  27. notCall && notCall()
  28. }
  29. }
  30. // #endif
  31. },
  32. // 获取本地app版本
  33. getAppVersion() {
  34. return new Promise((success, error) => {
  35. if (localVersion) {
  36. success(localVersion)
  37. } else {
  38. plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
  39. success(widgetInfo.version);
  40. localVersion = widgetInfo.version
  41. });
  42. }
  43. });
  44. },
  45. // 获取平台名称
  46. osName() {
  47. return plus.os.name
  48. },
  49. // 获取线上版本
  50. getNewestVersion() {
  51. if (onlineVersionObj) {
  52. return onlineVersionObj
  53. }
  54. onlineVersionObj = Member.getNewestVersion()
  55. return onlineVersionObj
  56. },
  57. // 获取纯数字(无小数点)
  58. getNum(str) {
  59. str += "";
  60. return str.replace(/[^0-9]/gi, "") * 1;
  61. },
  62. // 提示升级
  63. toUpgrade() {
  64. console.log('去升级')
  65. },
  66. // 下载文件
  67. downloadFile({ url, update, before, after }) {
  68. return new Promise((success, error) => {
  69. before && before()
  70. let downloadTask = plus.downloader.createDownload(url, {}, (res, status) => {
  71. if (status == 200) {
  72. success(res.filename)
  73. }
  74. after && after()
  75. })
  76. let onStateChanged = (e) => {
  77. update && update(parseInt(e.downloadedSize / e.totalSize * 100) || 0)
  78. }
  79. downloadTask.addEventListener("statechanged", onStateChanged, false);
  80. downloadTask.start();
  81. });
  82. },
  83. // 打开文件(安装)
  84. install(path) {
  85. if (plus.os.name == 'Android') {
  86. plus.runtime.install(
  87. path,
  88. {
  89. force: false,
  90. },
  91. () => {
  92. plus.runtime.restart();
  93. vue.prototype.$toast("下载成功,正在安装。。。");
  94. },
  95. (e) => {
  96. vue.prototype.$toast("安装失败,请尝试重新下载");
  97. }
  98. );
  99. } else if (plus.os.name == "iOS") {
  100. plus.runtime.openURL(path)
  101. }
  102. },
  103. ready(call) {
  104. if (window.plus) {
  105. call();
  106. } else {
  107. document.addEventListener('plusready', call, false);
  108. }
  109. }
  110. }
  111. /* eslint-disable */
  112. export default upgrade