check-update.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // #ifdef APP-PLUS
  2. import callCheckVersion from './call-check-version'
  3. // 推荐再App.vue中使用
  4. const PACKAGE_INFO_KEY = '__package_info__'
  5. export default function() {
  6. callCheckVersion().then(async (e) => {
  7. if (!e.result) return;
  8. const {
  9. code,
  10. message,
  11. is_silently, // 是否静默更新
  12. url, // 安装包下载地址
  13. platform, // 安装包平台
  14. type // 安装包类型
  15. } = e.result;
  16. // 此处逻辑仅为实例,可自行编写
  17. if (code > 0) {
  18. // 腾讯云和阿里云下载链接不同,需要处理一下,阿里云会原样返回
  19. const {
  20. fileList
  21. } = await uniCloud.getTempFileURL({
  22. fileList: [url]
  23. });
  24. e.result.url = fileList[0].tempFileURL
  25. // 静默更新,只有wgt有
  26. if (is_silently) {
  27. uni.downloadFile({
  28. url: e.result.url,
  29. success: res => {
  30. if (res.statusCode == 200) {
  31. // 下载好直接安装,下次启动生效
  32. plus.runtime.install(res.tempFilePath, {
  33. force: false
  34. });
  35. }
  36. }
  37. });
  38. return;
  39. }
  40. /**
  41. * 提示升级一
  42. * 使用 uni.showModal
  43. */
  44. // return updateUseModal(e.result)
  45. /**
  46. * 提示升级二
  47. * 官方适配的升级弹窗,可自行替换资源适配UI风格
  48. */
  49. uni.setStorageSync(PACKAGE_INFO_KEY, e.result)
  50. uni.navigateTo({
  51. url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
  52. fail: (err) => {
  53. console.error('更新弹框跳转失败', err)
  54. uni.removeStorageSync(PACKAGE_INFO_KEY)
  55. }
  56. })
  57. } else if (code < 0) {
  58. // TODO 云函数报错处理
  59. console.error(message)
  60. }
  61. }).catch(err => {
  62. // TODO 云函数报错处理
  63. console.log(err)
  64. console.error(err.message)
  65. })
  66. }
  67. /**
  68. * 使用 uni.showModal 升级
  69. */
  70. function updateUseModal(packageInfo) {
  71. const {
  72. title, // 标题
  73. contents, // 升级内容
  74. is_mandatory, // 是否强制更新
  75. url, // 安装包下载地址
  76. platform, // 安装包平台
  77. type // 安装包类型
  78. } = packageInfo;
  79. let isWGT = type === 'wgt'
  80. let isiOS = !isWGT ? platform.includes('iOS') : false;
  81. let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'
  82. return uni.showModal({
  83. title,
  84. content: contents,
  85. showCancel: !is_mandatory,
  86. confirmText,
  87. success: res => {
  88. if (res.cancel) return;
  89. // 安装包下载
  90. if (isiOS) {
  91. plus.runtime.openURL(url);
  92. return;
  93. }
  94. uni.showToast({
  95. title: '后台下载中……',
  96. duration: 1000
  97. });
  98. // wgt 和 安卓下载更新
  99. downloadTask = uni.downloadFile({
  100. url,
  101. success: res => {
  102. if (res.statusCode !== 200) {
  103. console.error('下载安装包失败', err);
  104. return;
  105. }
  106. // 下载好直接安装,下次启动生效
  107. plus.runtime.install(res.tempFilePath, {
  108. force: false
  109. }, () => {
  110. if (is_mandatory) {
  111. //更新完重启app
  112. plus.runtime.restart();
  113. return;
  114. }
  115. uni.showModal({
  116. title: '安装成功是否重启?',
  117. success: res => {
  118. if (res.confirm) {
  119. //更新完重启app
  120. plus.runtime.restart();
  121. }
  122. }
  123. });
  124. }, err => {
  125. uni.showModal({
  126. title: '更新失败',
  127. content: err
  128. .message,
  129. showCancel: false
  130. });
  131. });
  132. }
  133. });
  134. }
  135. });
  136. }
  137. // #endif