upgrade.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <uni-popup
  3. class="upgrade-unipopup"
  4. ref="popup"
  5. type="center"
  6. :animation="false"
  7. :mask-click="false"
  8. style="z-index: 999">
  9. <view class="upgrade-popup">
  10. <image
  11. class="header-bg"
  12. src="../static/common/upgrade_bg.png"
  13. mode="widthFix"></image>
  14. <view class="main">
  15. <view class="version"
  16. >{{ t("version.newVersion") }}{{ versionName }}</view
  17. >
  18. <view class="content" v-if="versionDesc">
  19. <text class="title">{{ t("version.updateInfo") }}</text>
  20. <view class="desc" v-html="versionDesc"></view>
  21. </view>
  22. <!--下载状态-进度条显示 -->
  23. <view class="footer" v-if="isStartDownload">
  24. <view class="progress-view" @click="handleInstallApp">
  25. <view style="height: 100%">
  26. <view class="txt">{{ percentText }}</view>
  27. <view class="progress" :style="setProStyle"></view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 强制更新 -->
  32. <view class="footer" v-else-if="isForceUpdate">
  33. <view class="btn upgrade force" @click="handleUpgrade">{{
  34. t("version.updateNow")
  35. }}</view>
  36. </view>
  37. <!-- 可选择更新 -->
  38. <view class="footer" v-else>
  39. <view class="btn close" @click="handleClose">{{
  40. t("version.updateLater")
  41. }}</view>
  42. <view class="btn upgrade" @click="handleUpgrade">{{
  43. t("version.updateNow")
  44. }}</view>
  45. </view>
  46. </view>
  47. </view>
  48. </uni-popup>
  49. </template>
  50. <script>
  51. import {
  52. consumePendingAppUpgradeCheck,
  53. isHotUpdateRunning,
  54. HOT_UPDATE_FALLBACK_APP_CHECK_EVENT,
  55. } from "@/utils/hot-update.js";
  56. import { getAppVersion } from "@/api/app.js";
  57. import { checkVersion, downloadApp, installApp } from "@/utils/upgrade.js";
  58. // 引用全局变量$t
  59. import { getCurrentInstance } from "vue";
  60. const isApkPackageUrl = (url = "") => {
  61. const normalizedUrl = String(url)
  62. .trim()
  63. .split("#")[0]
  64. .split("?")[0]
  65. .toLowerCase();
  66. return normalizedUrl.endsWith(".apk");
  67. };
  68. export default {
  69. data() {
  70. return {
  71. isForceUpdate: true, //是否强制更新
  72. versionName: "", //版本名称
  73. versionDesc: "", //更新说明
  74. downloadUrl: "", //APP下载链接
  75. isDownloadFinish: false, //是否下载完成
  76. hasProgress: false, //是否能显示进度条
  77. currentPercent: 0, //当前下载百分比
  78. isStartDownload: false, //是否开始下载
  79. fileName: "", //下载后app本地路径名称
  80. hasTriggeredStartupAppCheck: false,
  81. pendingHotUpdateFallbackAppCheck: false,
  82. t: null, // 全局翻译函数
  83. };
  84. },
  85. computed: {
  86. //设置进度条样式,实时更新进度位置
  87. setProStyle() {
  88. return {
  89. width: (510 * this.currentPercent) / 100 + "rpx", //510:按钮进度条宽度
  90. };
  91. },
  92. //百分比文字
  93. percentText() {
  94. let percent = this.currentPercent;
  95. if (typeof percent !== "number" || isNaN(percent))
  96. return this.t("version.downloading"); // 下载中
  97. if (percent < 100) return `${this.t("version.download")}${percent}%`;
  98. return this.t("version.install"); // 下载完成
  99. },
  100. },
  101. onBackPress(options) {
  102. // 禁用返回
  103. if (options.from == "backbutton") {
  104. return true;
  105. }
  106. },
  107. mounted() {
  108. const { appContext } = getCurrentInstance();
  109. const t = appContext.config.globalProperties.$t;
  110. this.t = t;
  111. //检测版本
  112. // #ifdef APP
  113. /**
  114. * 这里保留原来的页面级整包更新检查,但增加一个非常轻量的互斥判断。
  115. *
  116. * 为什么要加这层判断:
  117. * 1. 当前项目原本就在登录页和首页自动挂载这个组件。
  118. * 2. 本次新增的 .wgt 热更新改到了 App 启动阶段,如果这里还无条件发起旧检查,
  119. * 启动时就可能同时出现“热更新弹窗”和“整包升级弹窗”。
  120. * 3. 这不是为了废掉原有逻辑,而是为了让原有逻辑在“启动热更新正在执行”时先让路,
  121. * 避免两个升级流程互相打架。
  122. *
  123. * 不加这段代码会怎样:
  124. * - App.vue onLaunch 触发热更新检查
  125. * - 页面 mounted 又触发旧的整包升级检查
  126. * - 用户可能连续看到两个升级提示,甚至两个流程同时下载,体验会非常差
  127. */
  128. if (this.pendingHotUpdateFallbackAppCheck) {
  129. this.triggerStartupAppCheckOnce();
  130. } else if (!isHotUpdateRunning()) {
  131. this.triggerStartupAppCheckOnce();
  132. } else {
  133. console.log(
  134. "skip page upgrade check because startup hot update is running"
  135. );
  136. }
  137. // #endif
  138. },
  139. created() {
  140. this.pendingHotUpdateFallbackAppCheck = consumePendingAppUpgradeCheck();
  141. uni.$on("upgrade-app", this.bindEmit);
  142. uni.$on(
  143. HOT_UPDATE_FALLBACK_APP_CHECK_EVENT,
  144. this.handleHotUpdateFallbackAppCheck
  145. );
  146. },
  147. beforeDestroy() {
  148. uni.$off("upgrade-app", this.bindEmit);
  149. uni.$off(
  150. HOT_UPDATE_FALLBACK_APP_CHECK_EVENT,
  151. this.handleHotUpdateFallbackAppCheck
  152. );
  153. },
  154. methods: {
  155. triggerStartupAppCheckOnce() {
  156. if (this.hasTriggeredStartupAppCheck) return;
  157. this.hasTriggeredStartupAppCheck = true;
  158. this.appCheckVersion();
  159. },
  160. handleHotUpdateFallbackAppCheck() {
  161. consumePendingAppUpgradeCheck();
  162. if (!this.t) {
  163. this.pendingHotUpdateFallbackAppCheck = true;
  164. return;
  165. }
  166. this.pendingHotUpdateFallbackAppCheck = false;
  167. this.triggerStartupAppCheckOnce();
  168. },
  169. async appCheckVersion() {
  170. const { data: remote } = await getAppVersion();
  171. // 后端找不到 APK 时会返回 data: null,此时不需要整包更新。
  172. if (!remote?.appVersion || !remote?.url) return;
  173. // 整包更新入口只处理 APK,避免将 WGT 误送到 APK 安装流程。
  174. if (!isApkPackageUrl(remote.url)) {
  175. console.warn("[app-upgrade] 忽略非 APK 更新地址", remote.url);
  176. return;
  177. }
  178. const up = await checkVersion({
  179. name: remote.appVersion, //最新版本名称
  180. code: remote.appVersion, //最新版本号
  181. content: "", //更新内容
  182. url: remote.url, //下载链接
  183. forceUpdate: true, //是否强制升级
  184. });
  185. if (up) {
  186. this.open();
  187. }
  188. },
  189. open() {
  190. //打开升级弹窗
  191. console.log("open upgrade popup");
  192. this.$refs.popup.open();
  193. },
  194. bindEmit(e) {
  195. let { name, content, url, forceUpdate } = e;
  196. this.isForceUpdate = forceUpdate;
  197. this.versionName = name;
  198. this.versionDesc = content;
  199. this.downloadUrl = url;
  200. },
  201. //更新
  202. handleUpgrade() {
  203. if (this.downloadUrl) {
  204. this.isStartDownload = true;
  205. this.currentPercent = 0; // 初始化进度
  206. downloadApp(this.downloadUrl, (current, downloadedSize, totalSize) => {
  207. // 始终显示进度条,无需hasProgress判断
  208. this.currentPercent = current;
  209. })
  210. .then((fileName) => {
  211. this.isDownloadFinish = true;
  212. this.fileName = fileName;
  213. this.currentPercent = 100; // 确保最终显示100%
  214. if (fileName) {
  215. // 下载完成后,自动安装
  216. console.log("🚀 ~ handleUpgrade ~ fileName:", fileName);
  217. this.handleInstallApp();
  218. }
  219. })
  220. .catch((e) => {
  221. console.error("🚀 ~ handleUpgrade ~ e:", e);
  222. this.currentPercent = 0; // 失败时重置
  223. });
  224. } else {
  225. uni.showToast({
  226. title: this.t("version.downloadLinkNotExist"),
  227. icon: "none",
  228. });
  229. }
  230. },
  231. //安装app
  232. handleInstallApp() {
  233. //下载完成才能安装,防止下载过程中点击
  234. if (this.isDownloadFinish && this.fileName) {
  235. installApp(this.fileName, () => {
  236. this.$refs.popup.close();
  237. //安装成功,关闭升级弹窗
  238. // uni.navigateBack()
  239. });
  240. }
  241. },
  242. //关闭返回
  243. handleClose() {
  244. // uni.navigateBack()
  245. this.$refs.popup.close();
  246. },
  247. },
  248. };
  249. </script>
  250. <style>
  251. page {
  252. background: rgba(0, 0, 0, 0.5);
  253. /**设置窗口背景半透明*/
  254. }
  255. </style>
  256. <style lang="scss" scoped>
  257. :deep(.upgrade-unipopup) {
  258. z-index: 9999 !important;
  259. }
  260. .upgrade-popup {
  261. width: 580rpx;
  262. height: auto;
  263. position: fixed;
  264. top: 50%;
  265. left: 50%;
  266. transform: translate(-50%, -50%);
  267. background: #fff;
  268. border-radius: 20rpx;
  269. box-sizing: border-box;
  270. border: 1px solid #eee;
  271. }
  272. .header-bg {
  273. width: 100%;
  274. margin-top: -112rpx;
  275. }
  276. .main {
  277. padding: 10rpx 30rpx 30rpx;
  278. box-sizing: border-box;
  279. .version {
  280. font-size: 36rpx;
  281. color: #026df7;
  282. font-weight: 700;
  283. width: 100%;
  284. text-align: center;
  285. overflow: hidden;
  286. text-overflow: ellipsis;
  287. white-space: nowrap;
  288. letter-spacing: 1px;
  289. }
  290. .content {
  291. margin-top: 60rpx;
  292. .title {
  293. font-size: 28rpx;
  294. font-weight: 700;
  295. color: #000000;
  296. }
  297. .desc {
  298. box-sizing: border-box;
  299. margin-top: 20rpx;
  300. font-size: 28rpx;
  301. color: #6a6a6a;
  302. max-height: 40vh;
  303. overflow-y: auto;
  304. }
  305. }
  306. .footer {
  307. width: 100%;
  308. display: flex;
  309. justify-content: center;
  310. align-items: center;
  311. position: relative;
  312. flex-shrink: 0;
  313. margin-top: 100rpx;
  314. .btn {
  315. width: 246rpx;
  316. display: flex;
  317. justify-content: center;
  318. align-items: center;
  319. position: relative;
  320. z-index: 999;
  321. height: 96rpx;
  322. box-sizing: border-box;
  323. font-size: 32rpx;
  324. border-radius: 10rpx;
  325. letter-spacing: 2rpx;
  326. &.force {
  327. width: 500rpx;
  328. }
  329. &.close {
  330. border: 1px solid #e0e0e0;
  331. margin-right: 25rpx;
  332. color: #000;
  333. }
  334. &.upgrade {
  335. background-color: #026df7;
  336. color: white;
  337. }
  338. }
  339. .progress-view {
  340. width: 510rpx;
  341. height: 90rpx;
  342. display: flex;
  343. position: relative;
  344. align-items: center;
  345. border-radius: 6rpx;
  346. background-color: #dcdcdc;
  347. display: flex;
  348. justify-content: flex-start;
  349. padding: 0px;
  350. box-sizing: border-box;
  351. border: none;
  352. overflow: hidden;
  353. &.active {
  354. background-color: #026df7;
  355. }
  356. .progress {
  357. height: 100%;
  358. background-color: #026df7;
  359. padding: 0px;
  360. box-sizing: border-box;
  361. border: none;
  362. border-top-left-radius: 10rpx;
  363. border-bottom-left-radius: 10rpx;
  364. transition: width 0.3s ease; // 添加平滑过渡动画
  365. }
  366. .txt {
  367. font-size: 28rpx;
  368. position: absolute;
  369. top: 50%;
  370. left: 50%;
  371. transform: translate(-50%, -50%);
  372. color: #fff;
  373. }
  374. }
  375. }
  376. }
  377. </style>