check-update.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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: uniUpgradeCenterResult.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. }
  88. else if (code < 0) {
  89. console.error(message)
  90. return reject(uniUpgradeCenterResult)
  91. }
  92. return resolve(uniUpgradeCenterResult)
  93. }).catch((err) => {
  94. reject(err)
  95. })
  96. });
  97. }
  98. /**
  99. * 使用 uni.showModal 升级
  100. */
  101. function updateUseModal(packageInfo : UniUpgradeCenterResult) : void {
  102. // #ifdef APP
  103. const {
  104. title, // 标题
  105. contents, // 升级内容
  106. is_mandatory, // 是否强制更新
  107. url, // 安装包下载地址
  108. type,
  109. platform
  110. } = packageInfo;
  111. let isWGT = type === 'wgt'
  112. let isiOS = !isWGT ? platform.includes(platform_iOS) : false;
  113. // #ifndef UNI-APP-X
  114. let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'
  115. // #endif
  116. // #ifdef UNI-APP-X
  117. let confirmText = '立即下载更新'
  118. // #endif
  119. return uni.showModal({
  120. title,
  121. content: contents,
  122. showCancel: !is_mandatory,
  123. confirmText,
  124. success: res => {
  125. if (res.cancel) return;
  126. if (isiOS) {
  127. // iOS 平台跳转 AppStore
  128. // #ifndef UNI-APP-X
  129. plus.runtime.openURL(url);
  130. // #endif
  131. // #ifdef UNI-APP-X
  132. openSchema(url)
  133. // #endif
  134. return;
  135. }
  136. uni.showToast({
  137. title: '后台下载中……',
  138. duration: 1000
  139. });
  140. // wgt 和 安卓下载更新
  141. uni.downloadFile({
  142. url,
  143. success: res => {
  144. if (res.statusCode !== 200) {
  145. console.error('下载安装包失败');
  146. return;
  147. }
  148. // 下载好直接安装,下次启动生效
  149. // uni-app x 项目没有 plus5+ 故使用条件编译
  150. // #ifndef UNI-APP-X
  151. plus.runtime.install(res.tempFilePath, {
  152. force: false
  153. }, () => {
  154. if (is_mandatory) {
  155. //更新完重启app
  156. // #ifdef APP-PLUS
  157. plus.runtime.restart();
  158. // #endif
  159. // #ifdef APP-HARMONY
  160. uni.showModal({
  161. title: '安装成功',
  162. content: '请手动重启应用',
  163. showCancel: false,
  164. success: res => {
  165. plus.runtime.quit();
  166. }
  167. });
  168. // #endif
  169. return;
  170. }
  171. uni.showModal({
  172. title: '安装成功是否重启?',
  173. success: res => {
  174. if (res.confirm) {
  175. //更新完重启app
  176. // #ifdef APP-PLUS
  177. plus.runtime.restart();
  178. // #endif
  179. // #ifdef APP-HARMONY
  180. plus.runtime.quit();
  181. // #endif
  182. }
  183. }
  184. });
  185. }, err => {
  186. uni.showModal({
  187. title: '更新失败',
  188. content: err
  189. .message,
  190. showCancel: false
  191. });
  192. });
  193. // #endif
  194. // #ifdef UNI-APP-X
  195. uni.installApk({
  196. filePath: res.tempFilePath,
  197. success: () => {
  198. uni.showModal({
  199. title: '安装成功请手动重启'
  200. });
  201. },
  202. fail: err => {
  203. uni.showModal({
  204. title: '更新失败',
  205. content: err.errMsg,
  206. showCancel: false
  207. });
  208. }
  209. });
  210. // #endif
  211. }
  212. });
  213. }
  214. });
  215. // #endif
  216. }