check-update.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import callCheckVersion, { UniUpgradeCenterResult } from "./call-check-version"
  2. import { platform_iOS } from './utils'
  3. // #ifdef UNI-APP-X
  4. import { openSchema } from '@/uni_modules/uts-openSchema'
  5. // #endif
  6. // 推荐再App.vue中使用
  7. const PACKAGE_INFO_KEY = '__package_info__'
  8. // #ifdef APP-HARMONY
  9. export default function (component?: any) : Promise<UniUpgradeCenterResult> {
  10. // #endif
  11. // #ifndef APP-HARMONY
  12. export default function () : Promise<UniUpgradeCenterResult> {
  13. // #endif
  14. return new Promise<UniUpgradeCenterResult>((resolve, reject) => {
  15. callCheckVersion().then(async (uniUpgradeCenterResult) => {
  16. // NOTE uni-app x 3.96 解构有问题
  17. const code = uniUpgradeCenterResult.code
  18. const message = uniUpgradeCenterResult.message
  19. const url = uniUpgradeCenterResult.url // 安装包下载地址
  20. // 此处逻辑仅为示例,可自行编写
  21. if (code > 0) {
  22. // 腾讯云获取下载链接
  23. if (/^cloud:\/\//.test(url)) {
  24. const tcbRes = await uniCloud.getTempFileURL({ fileList: [url] });
  25. if (typeof tcbRes.fileList[0].tempFileURL !== 'undefined') uniUpgradeCenterResult.url = tcbRes.fileList[0].tempFileURL;
  26. }
  27. /**
  28. * 提示升级一
  29. * 使用 uni.showModal
  30. */
  31. // return updateUseModal(uniUpgradeCenterResult)
  32. // #ifndef UNI-APP-X
  33. // 静默更新,只有wgt有
  34. if (uniUpgradeCenterResult.is_silently) {
  35. uni.downloadFile({
  36. url,
  37. success: res => {
  38. if (res.statusCode == 200) {
  39. // 下载好直接安装,下次启动生效
  40. plus.runtime.install(res.tempFilePath, {
  41. force: false
  42. });
  43. }
  44. }
  45. });
  46. return;
  47. }
  48. // #endif
  49. /**
  50. * 提示升级二
  51. * 官方适配的升级弹窗,可自行替换资源适配UI风格
  52. */
  53. // #ifndef UNI-APP-X
  54. // #ifdef APP-PLUS
  55. uni.setStorageSync(PACKAGE_INFO_KEY, uniUpgradeCenterResult)
  56. uni.navigateTo({
  57. url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
  58. fail: (err) => {
  59. console.error('更新弹框跳转失败', err)
  60. uni.removeStorageSync(PACKAGE_INFO_KEY)
  61. }
  62. })
  63. // #endif
  64. // #ifdef APP-HARMONY
  65. if (component) {
  66. component.show(true, uniUpgradeCenterResult)
  67. } else {
  68. reject({
  69. code: -1,
  70. message: '在 HarmonyOS Next 平台请传递组件使用'
  71. })
  72. }
  73. // #endif
  74. // #endif
  75. // #ifdef UNI-APP-X
  76. uni.setStorageSync(PACKAGE_INFO_KEY, uniUpgradeCenterResult)
  77. uni.openDialogPage({
  78. url: `/uni_modules/uni-upgrade-center-app/pages/uni-app-x/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
  79. disableEscBack: true,
  80. fail: (err) => {
  81. console.error('更新弹框跳转失败', err)
  82. uni.removeStorageSync(PACKAGE_INFO_KEY)
  83. }
  84. })
  85. // #endif
  86. return resolve(uniUpgradeCenterResult)
  87. } else if (code < 0) {
  88. console.error(message)
  89. return reject(uniUpgradeCenterResult)
  90. }
  91. return resolve(uniUpgradeCenterResult)
  92. }).catch((err) => {
  93. reject(err)
  94. })
  95. });
  96. }
  97. /**
  98. * 使用 uni.showModal 升级
  99. */
  100. function updateUseModal(packageInfo : UniUpgradeCenterResult) : void {
  101. // #ifdef APP
  102. const {
  103. title, // 标题
  104. contents, // 升级内容
  105. is_mandatory, // 是否强制更新
  106. url, // 安装包下载地址
  107. type,
  108. platform
  109. } = packageInfo;
  110. let isWGT = type === 'wgt'
  111. let isiOS = !isWGT ? platform.includes(platform_iOS) : false;
  112. // #ifndef UNI-APP-X
  113. let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'
  114. // #endif
  115. // #ifdef UNI-APP-X
  116. let confirmText = '立即下载更新'
  117. // #endif
  118. return uni.showModal({
  119. title,
  120. content: contents,
  121. showCancel: !is_mandatory,
  122. confirmText,
  123. success: res => {
  124. if (res.cancel) return;
  125. if (isiOS) {
  126. // iOS 平台跳转 AppStore
  127. // #ifndef UNI-APP-X
  128. plus.runtime.openURL(url);
  129. // #endif
  130. // #ifdef UNI-APP-X
  131. openSchema(url)
  132. // #endif
  133. return;
  134. }
  135. uni.showToast({
  136. title: '后台下载中……',
  137. duration: 1000
  138. });
  139. // wgt 和 安卓下载更新
  140. uni.downloadFile({
  141. url,
  142. success: res => {
  143. if (res.statusCode !== 200) {
  144. console.error('下载安装包失败');
  145. return;
  146. }
  147. // 下载好直接安装,下次启动生效
  148. // uni-app x 项目没有 plus5+ 故使用条件编译
  149. // #ifndef UNI-APP-X
  150. plus.runtime.install(res.tempFilePath, {
  151. force: false
  152. }, () => {
  153. if (is_mandatory) {
  154. //更新完重启app
  155. // #ifdef APP-PLUS
  156. plus.runtime.restart();
  157. // #endif
  158. // #ifdef APP-HARMONY
  159. uni.showModal({
  160. title: '安装成功',
  161. content: '请手动重启应用',
  162. showCancel: false,
  163. success: res => {
  164. plus.runtime.quit();
  165. }
  166. });
  167. // #endif
  168. return;
  169. }
  170. uni.showModal({
  171. title: '安装成功是否重启?',
  172. success: res => {
  173. if (res.confirm) {
  174. //更新完重启app
  175. // #ifdef APP-PLUS
  176. plus.runtime.restart();
  177. // #endif
  178. // #ifdef APP-HARMONY
  179. plus.runtime.quit();
  180. // #endif
  181. }
  182. }
  183. });
  184. }, err => {
  185. uni.showModal({
  186. title: '更新失败',
  187. content: err
  188. .message,
  189. showCancel: false
  190. });
  191. });
  192. // #endif
  193. // #ifdef UNI-APP-X
  194. uni.installApk({
  195. filePath: res.tempFilePath,
  196. success: () => {
  197. uni.showModal({
  198. title: '安装成功请手动重启'
  199. });
  200. },
  201. fail: err => {
  202. uni.showModal({
  203. title: '更新失败',
  204. content: err.errMsg,
  205. showCancel: false
  206. });
  207. }
  208. });
  209. // #endif
  210. }
  211. });
  212. }
  213. });
  214. // #endif
  215. }