upgrade.vue 10 KB

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