check-update-nvue.js 4.0 KB

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