updateApp.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const UPDATE_URL = 'http://192.168.1.150:8443/updateInfo/'
  2. /**
  3. * 版本号比较
  4. * @param {Object} v1 eg: 11.0.2
  5. * @param {Object} v2 eg: 11.0.1
  6. * @return {Number} -1 => v1 < v2
  7. * 1 => v1 > v2
  8. * 0 => v1 = v2
  9. */
  10. const compareVersion = function(v1, v2) {
  11. if (v1 === v2) return 0
  12. v1 = v1.split('.')
  13. v2 = v2.split('.')
  14. const len = Math.max(v1.length, v2.length)
  15. while (v1.length < len) {
  16. v1.push('0')
  17. }
  18. while (v2.length < len) {
  19. v2.push('0')
  20. }
  21. for (let i = 0; i < len; i++) {
  22. const num1 = parseInt(v1[i])
  23. const num2 = parseInt(v2[i])
  24. if (num1 > num2) {
  25. return 1
  26. } else if (num1 < num2) {
  27. return -1
  28. }
  29. }
  30. return 0
  31. }
  32. export default class UpdateApp {
  33. constructor() {
  34. this.force = false // 是否为强制更新
  35. this.restart = true // 安装完成后是否需要强制重启
  36. }
  37. check() {
  38. const that = this
  39. return new Promise((resolve, reject) => {
  40. plus.runtime.getProperty(
  41. plus.runtime.appid,
  42. function(widgetInfo) {
  43. uni.request({
  44. url: UPDATE_URL,
  45. data: {
  46. version: widgetInfo.version,
  47. appid: plus.runtime.appid
  48. },
  49. success: (result) => {
  50. // force: true, // 是否为强制更新
  51. // restart: true, // 安装完成后是否需要强制重启
  52. // androidWgtUrl: '****.wgt', // android wgt包地址(热更新时使用)
  53. // iosWgtUrl: '', // ios wgt包更新地址(热更新时使用)
  54. // androidPkgUrl: '', // android apk 地址(全量更新时使用)
  55. // iosPkgUrl: '', // ios 应用商店地址(全量更新时使用,用于直接跳转到应用商店)
  56. // remark: '1、用户体验优化\n2、BUG修复'
  57. const res = result.data.data
  58. // version: widgetInfo.version,
  59. // appid: plus.runtime.appid
  60. const updateFlag = compareVersion(res.version, widgetInfo.version)
  61. if (updateFlag === 0) {
  62. resolve({ downloadUrl: null })
  63. return
  64. }
  65. that.force = res.force
  66. that.restart = res.restart
  67. const sys = uni.getSystemInfoSync()
  68. let downloadUrl = ''
  69. if (sys.platform === 'android') {
  70. downloadUrl = res.androidWgtUrl || res.androidPkgUrl
  71. } else if (sys.platform === 'ios') {
  72. downloadUrl = res.iosWgtUrl || res.iosPkgUrl
  73. }
  74. resolve({
  75. ...res,
  76. downloadUrl: downloadUrl
  77. })
  78. }
  79. })
  80. }
  81. )
  82. })
  83. }
  84. /**
  85. * 下载安装更新包
  86. * @param {String} data
  87. */
  88. install(data) {
  89. const sys = uni.getSystemInfoSync()
  90. if (sys.platform === 'ios' && data.iosPkgUrl) {
  91. plus.runtime.openURL(data.iosPkgUrl)
  92. return
  93. }
  94. console.log('install data', data)
  95. uni.downloadFile({
  96. url: data.downloadUrl,
  97. success: function(downloadResult) {
  98. if (downloadResult.statusCode === 200) {
  99. plus.runtime.install(
  100. downloadResult.tempFilePath, {
  101. force: data.force
  102. },
  103. function() {
  104. console.log('install success...')
  105. if (data.restart) {
  106. plus.runtime.restart();
  107. }
  108. },
  109. function(e) {
  110. console.error('install fail...')
  111. plus.runtime.quit()
  112. }
  113. )
  114. }
  115. },
  116. error: function(e) {
  117. console.log('install error', e)
  118. uni.showToast({
  119. position: 'top',
  120. title: '更新失败',
  121. icon: 'none',
  122. duration: 3000
  123. })
  124. }
  125. })
  126. }
  127. }