upgrade.vue 10 KB

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